【原创文章】探究C语言main函数返回值类型和参数的标准写法 |
您所在的位置:网站首页 › c语言typeof函数 › 【原创文章】探究C语言main函数返回值类型和参数的标准写法 |
![]() 这篇文章探究C语言main函数返回类型和参数的标准写法是什么。 1.C语言教材上的写法 在不同的C语言教材上,大家可能看到过各种各样的写法。举几个例子: (1)谭浩强版本的《C语言程序设计》: main(){ enum weekday { sun,mon,tue,wed,thu,fri,sat } a,b,c; a=sun; b=mon; c=tue; printf("%d,%d,%d",a,b,c); } main(int argc,char *argv){ while(argc-->1) printf("%s\n",*++argv); } (2)C语言之父的著作《The C Programming Language》 main() { printf("hello world 20200512\n") } (3)《C Primer Plus 第六版》 int main(void) { int dogs; printf("How many dogs do you have?\n"); scanf("%d", &dogs); printf("So you have %d dog(s)!\n", dogs); return 0; } 可以看出,不同的教材里的写法也有差异,在这里我们就看到了main()、main(int argc,char *argv)、int main(void)这三种写法。有的童鞋在其他教材可能还看到过void main()的写法。那么这些写法是不是都对呢?到底有没有标准的写法。我们先来看看linux内核中main函数是怎么写的。 2.linux 内核中的写法 例1 int main( int argc, const char * argv [] ) http://androidxref.com/kernel_3.18/xref/sound/oss/hex2hex.c 68int main( int argc, const char * argv [] ) 69{ 70 const char * varline; 71 int i,l; 72 int id=0; 73 例2 int main(int argc, char **argv) http://androidxref.com/kernel_3.18/xref/tools/power/acpi/tools/acpid/acpidbg.c int main(int argc, char **argv) { int fd = -1; int ch; int len; int ret = EXIT_SUCCESS; 例3 int main(void) http://androidxref.com/kernel_3.18/xref/tools/testing/selftests/powerpc/pmu/ebb/multi_ebb_procs_test.c int main(void) { return test_harness(multi_ebb_procs, "multi_ebb_procs"); } 例4 int main() http://androidxref.com/kernel_3.18/xref/tools/testing/selftests/x86/ptrace_syscall.c 280 int main() 281 { 282 printf("[RUN]\tCheck int80 return regs\n"); 283 test_sys32_regs(do_full_int80); 284 285 #if defined(__i386__) && (!defined(__GLIBC__) || __GLIBC__ > 2 || __GLIBC_MINOR__ >= 16) 286 vsyscall32 = (void *)getauxval(AT_SYSINFO); 287 printf("[RUN]\tCheck AT_SYSINFO return regs\n"); 288 test_sys32_regs(do_full_vsyscall32); 289 #endif 290 291 test_ptrace_syscall_restart(); 292 293 return 0; 294 } 在Linux内核中,搜索main函数,基本只能看到上面四种形式的写法:int main()、int main(void)、int main(int argc, char **argv)、int main( int argc, const char * argv [] )。相比谭浩强版本的《C语言程序设计》课本、C语言之父的《The C Programming Language》,多了返回类型int。 这里你可能还是感到混乱。到底有没有一个标准的写法?让我们从C语言标准中找到答案。 3.C11标准中main函数的写法 C语言标准是国际标准化组织ISO制定的,旨在完整地定义C语言,提供给编译器厂商和编程者参考。最新的C语言标准是C18,也叫 ISO/IEC 9899:2018 。 C语言标准手册是需要花钱买的。但是有一些免费的C语言标准草稿。 C18的最新免费提供的草稿的网络链接打不开。我们来看下C18之前的标准C11(也叫 ISO/IEC 9899:2011)的最新免费提供的草稿。你可以从如下的网址访问它。 http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf 我们看看C11标准里对main函数标准形式的定义,先贴上英文原文(不想看英文原文的,可以跳过,直接看后面我用中文总结的大意): 5.1.2.2.1 Program startup 1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ } or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): int main(int argc, char *argv[]) { /* ... */ } or equivalent; 10) or in some other implementation-defined manner. Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on. 可以清楚地看出,C11标准给出了下面几种main函数返回类型和参数的写法: (1)int main(void) { /* ... */ } (2)int main(int argc, char *argv[]) { /* ... */ } (3)int main(int argc, char **argv) { /* ... */ } (4)可以用定义为int的typedef名称替换int。比如,如果你定义了 typedef int size,那么在写main函数时你可以用size替换int,写成: size main(void) 或者size main(size argc, char *argv[])或者size main(size argc, char **argv) 4.总结 你可以看到,C语言标准里main函数返回类型和参数有以下几种写法: (1)int main(void) { /* ... */ } (2)int main(int argc, char *argv[]) { /* ... */ } (3)int main(int argc, char **argv) { /* ... */ } (4)可以用定义为int的typedef名称替换int。 有些老的编程软件(比如vc++ 6.0)可能也支持编译void main()的写法,但要注意这个写法不在最新的C语言标准中。 一般来说,你平时写程序用int main(void)就可以了。 ……………… 笔者双一流大学计算机专业研究生毕业,软件工程师,工作5年,目前在给世界500强芯片公司工作。 【获取更多c语言知识分享,关注我的微信公众号:C语言初学者之家】 ![]() |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |