8. main()长啥样?
先贴个帝球的帖子:
找不着了。。先搁置。
接下来是C99的内容:
5.1.2.2.1 Program startup
1 The function called at program startup is named main. The implementation declares noprototype 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 beused, 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.
标准允许的(具有可移植性)的是这两种,但也允许其它的形式。
不过这些形式,包括void main()、void main(void)、int main(int argc, char** argv, char**envp)、int main()(它实际上和int main(void)声明了相同的函数,但是它没有使用原型)甚至char main(void)都是不推荐的。这么使用没有益处。
C++则有所不同:(C++11)
3.6 Start and termination [basic.start]
3.6.1 Main function [basic.start.main]
1 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, start-up 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. — end note ]
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 of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* ar gv[]) { /* ... */ }
同样要求实现必须允许这两种形式,但是额外增加了一个限制:main必须返回int。这就是说:C++中void main()是不合法的(当然它是合法的VC++代码,这另当别论。)
另外存在一个例外:在freestandind env中程序的开始和结束是实现定义的,也就是说main的形式,甚至是否存在main都是无所谓的。按照我的理解,这大致相当于在没有操作系统情况下的程序。。
挖坟大法好。
先贴个帝球的帖子:
找不着了。。先搁置。
接下来是C99的内容:
5.1.2.2.1 Program startup
1 The function called at program startup is named main. The implementation declares noprototype 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 beused, 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.
标准允许的(具有可移植性)的是这两种,但也允许其它的形式。
不过这些形式,包括void main()、void main(void)、int main(int argc, char** argv, char**envp)、int main()(它实际上和int main(void)声明了相同的函数,但是它没有使用原型)甚至char main(void)都是不推荐的。这么使用没有益处。
C++则有所不同:(C++11)
3.6 Start and termination [basic.start]
3.6.1 Main function [basic.start.main]
1 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, start-up 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. — end note ]
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 of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* ar gv[]) { /* ... */ }
同样要求实现必须允许这两种形式,但是额外增加了一个限制:main必须返回int。这就是说:C++中void main()是不合法的(当然它是合法的VC++代码,这另当别论。)
另外存在一个例外:在freestandind env中程序的开始和结束是实现定义的,也就是说main的形式,甚至是否存在main都是无所谓的。按照我的理解,这大致相当于在没有操作系统情况下的程序。。
挖坟大法好。