问题描述
可能重复:结果
我的工作我的C技能,我注意到
I am working on my C skills and I have noticed that
INT主(INT ARGC,CHAR *的argv [])
和
收益率(EXIT_SUCCESS)
而不是
INT main()和返回0
这是为什么?
推荐答案
如果你要忽略的参数列表,这是合理和明智的使用方法:
If you are going to ignore the argument list, it is reasonable and sensible to use:
int main(void) { ... }
该标准保佑这个用法,以及一个带参数。如果您使用 -Wstrict的原型编译你从海湾合作委员会的警告
,不包括无效
,所以我写无效
。 C ++是这里的不同。
The standards bless this usage, as well as the one with arguments. You get a warning from GCC if you compile with -Wstrict-prototypes
and don't include the void
, so I write the void
. C++ is different here.
至于返回EXIT_SUCCESS;
,似乎有什么好处给它一般;我继续写返回0;
在的main()
函数结束时,即使C99允许您省略任何返回那里(和它然后行为就好像你写返回0;
)。
As for return EXIT_SUCCESS;
, there seems to be little benefit to it in general; I continue to write return 0;
at the end of a main()
function, even though C99 permits you to omit any return there (and it then behaves as if you wrote return 0;
).
ISO / IEC 9899:1999
§5.1.2.2.1程序启动
§5.1.2.2.1 Program startup
¶1称为在程序启动时的功能被命名为主
。实施没有声明
原型此功能。它应 INT
的返回类型,并没有被定义
参数:
¶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) { /* ... */ }
或两个参数(这里称为 ARGC
和的argv
,虽然任何名称可能是
使用,因为它们是局部的,其中声明它们的功能):
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; or in some other implementation-defined manner.
因此, INT
可以定义为 INT
或> 的argv
的char ** argv的
,等等。
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程序终止
§5.1.2.2.3 Program termination
¶1如果主
函数的返回类型是一种与 INT
兼容,从一回
在主
函数初始呼叫相当于调用退出
函数值
由主
函数作为参数返回; 达到}
终止的
主
函数返回值为0如果返回类型是不符合 INT
兼容,该
返回到主机环境的终止状态是不确定的。
¶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; 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.
按照第6.2.4节中声明的自动存储持续时间对象的寿命主
在前者的情况下,即使在他们不会在后者具有将已结束
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.3exit函数
§7.20.4.3 The exit function
¶5最后,控制被返回到主机环境。如果状态的值是零或
EXIT_SUCCESS,状态成功终止实现定义的形式是
回。如果状态值为EXIT_FAILURE,实现定义的形式
的状态返回不成功的终止。否则,返回的状态是
实现定义的。
¶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()
比<$其他C $ C> INT (不像C ++标准,其中前pressly禁止超过 INT
)。然而,由于正确地指出,非 - INT
从主
只允许如果实现明确的记录,这是允许的,并且该文档可能会放在哪些是允许范围的返回类型。的
Aside: Note that §5.1.2.2.3 clearly indicates that the C standard allows an implementation to permit return types for main()
other than int
(unlike the C++ standard, which expressly forbids a return type other than int
). However, as Jens rightly points out, a non-int
return type from main
is only allowed if the implementation explicitly documents that it is allowed, and the documentation will probably place limits on what is allowed.
这篇关于在c主要功能样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!