上一次我们说明了C99 5.1.2.2.1 Program startup。今天我们继续对5.1.2.2.3 Program termination说明。
1. If the return type of the main function is a type compatible with int,
1.1a 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;
用代码来说明:
int status = main(argc, argv, envp); /* Linux 下 */
exit(status);
1.2 reaching the } that terminates the main function returns a value of 0.
理解:碰到结束括号},认为就是返回0。
推理:
1)int main()内可以不要写return语句。
代码验证
$ cat main.c
int main() { }
连续运行两次,退出状态都是0,不会是巧合吧。
$ ./a.out;echo $?
0
$ ./a.out;echo $?
0
所以,main函数就是不一般。
TODO: 查看gcc-4.2.4中对main特殊处理的代码
2. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.