C语言中主函数的类型

5

可能是重复问题:
main函数的正确声明方式是什么?

我正在学习C语言技能,发现在编写主函数时通常使用:

int main( int argc, char *argv[] )

以及

return (EXIT_SUCCESS)

而不是

int main() 和 return 0

这是为什么呢?


这里提出了两个问题。 - Christian Neverdal
@ChristianJonassen:FAQ问题还讨论了如何从main()函数返回。 - Ben Voigt
你说得对。这个问题是重复的。 - Christian Neverdal
1
我不太同意(因此我认为这个问题与关于C ++ main()函数的重复问题不同)。另一个问题特别针对C ++,并且与C的等效答案有明显的区别(尤其是,C标准允许main()的返回类型不是int,请参见我的答案,而C ++ 绝对不允许)。 - Jonathan Leffler
5个回答

11
如果您要忽略参数列表,使用以下内容是合理且明智的:

int main(void) { ... }

这些标准支持此用法,以及带参数的用法。如果您使用-Wstrict-prototypes编译时没有包含void,则会收到GCC的警告,因此我写了void。C ++在这里有所不同。
至于return EXIT_SUCCESS;,一般来说似乎没有什么好处;即使C99允许您在那里省略任何返回(然后它的行为就像您写了return 0;),我仍然在main()函数末尾写return 0;

ISO/IEC 9899:1999

§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;9) or in some other implementation-defined manner.

9) 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.

§5.1.2.2.3 Program termination

¶1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

10) In accordance with §6.2.4, the lifetimes of objects with automatic storage duration declared in main will have ended in the former case, even where they would not have in the latter.

§7.20.4.3 The exit function

¶5 Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.

顺便提一下:注意到 §5.1.2.2.3 明确指出,C标准允许实现允许 main() 返回类型为除 int 以外的其他类型(与C++标准明确禁止返回类型为除 int 以外的其他类型不同)。然而,正如Jens所指出的那样,只有在实现明确文档说明允许使用非int类型的 main 返回类型时才允许这样做,并且文档可能会对允许使用的内容进行限制。


+1,但也许应该补充一下,main 函数不返回 int 的属性是“实现特定的”。首先,不是用户选择编译器生产者的返回类型,其次,这必须在编译器文档中明确指定。 - Jens Gustedt

1

int main(int argc, char* argv[]) 是用于处理命令行参数的。

EXIT_SUCCESS 只是一个比 0 更具描述性的 #define


1

int main( int argc, char *argv[] ) 允许用户在程序执行时输入参数,即在程序名称后在控制台中编写的文本。

return (EXIT_SUCCESS) 是为了防止操作系统将0作为成功退出的值:它可以是任何其他值,但在大多数情况下,EXIT_SUCCESS 等于0。


从主函数返回0或调用exit(0)必须被解释为成功退出,即使返回给调用环境的值通常不将0视为成功(在这种情况下,0会映射到将被视为成功的某个值)。请参阅我回答中来自C99标准的引用。 - Jonathan Leffler

1

1

关于你的问题: 这两个问题没有任何关联。

回答你的第一个问题: 拥有int main()只是意味着程序不接受命令行参数。 当它接受两个参数时,argc是参数计数(它将始终大于或等于1,因为第一个参数将是程序本身的路径或名称),而argv是参数列表。

回答你的第二个问题: EXIT_SUCCESS被底层操作系统保证被解释为成功。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接