- 论坛徽章:
- 0
|
原帖由 飞灰橙 于 2006-12-5 14:49 发表
C还是拿代码说话吧:
以下代码,在我的linux上测试,如果用exit (0), 会显示tom
用return 0, 会显示jerry
[code]
#include <stdio.h>
#include <stdlib.h>
int a, b;
void test ()
{ ...
这个,N1124中说:
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.
也就是说严格来说,语义上有一点点的区别,但也仅仅是生存期了,啥也不影响,所以用了equivalent一词。至于你的结果不一样,应该是a=(int)&n造成的。这样的cast结果undefined,所以才会不一样。
另外多说一句,关于C这样的问题,我认为引用C标准的同时再解释一下常见的实现情况要好一些。脱离标准谈论,总觉得不够。莫非我太学院派了? |
|