参见英文答案 > What should main() return in C and C++? 17个
该标准明确规定主要有两个有效(即保证工作)的签名;即:
int main();
int main(int, char*[]);
我的问题很简单,以下是合法的吗?
int main(const unsigned int, const char* const* argv);
我的测试说’是’,但我不确定答案,因为我没有通过将int更改为unsigned int以及argv的非*const-over来重载main?如果我是,那显然是禁止的.
那么,这些修改是否可以保证在符合标准的编译器上工作?
解决方法:
C 98标准在第3.6.1节第2节中说明
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type
int
, but otherwise its type is implementation-defined. All implementations shall allow both the following definitions ofmain
:int main()
andint main(int argc, char* argv[])
因此,标准中没有规定接受主体的接受是可接受的但是允许的.
因为这是经常提到的,所以前面的段落豁免了独立环境,除了记录它们的行为之外:
A program shall contain a global function called main, which is the designated start of the program. It is
implementation defined
whether a program in a freestanding environment is required to define a main
function. [Note: in a freestanding environment, startup
and termination is implementation defined;
startup
contains the execution of constructors for objects of namespace scope with static storage duration; termination
contains the execution of destructors for objects with static storage duration. ]