作者khoguan (Khoguan Phuann)
看板C_and_CPP
标题Re: [讨论] 有关main
时间Thu Aug 25 02:35:30 2005
※ 引述《freaky (jon)》之铭言:
: FAQ 并非标准, 请参考 ISO C Standard 5.1.2.2.1.1:
C FAQ (copyright 1995)所遵循的是 C89/90 标准,
而非以下所引的 C99 条文。
: 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 [...] or with two parameters [...] or equivalent;
: or in some other implementation-defined manner.
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: 另外还有 5.1.2.2.3.1:
: [...] If the return type is not compatible with int, the
: termination status returned to the host environment is
: unspecified.
: 显然目前 ISO C Standard 允许这个後门存在.
容许是容许,但既然是 implementation-defined 那就能不用就不用。
因为使用 implementation-defined feature 的程式就不是
strictly conforming program 了。
: : 至於 void main() 的始作俑者,我是在 comp.lang.c* 上看到
: : 人家说是 MS 搞出来的。请让我再找一下有无更权威的证据。
: : BTW, 你所举的实作也都是限於 MS 平台的。
: 然後? 是 MS 实作 IBM VisualAge C/C++ 吗?
然後,就是我所强调的,将 void 改成 int,确保具有最大的
可移植性,也就是写成 int main() { /*...*/ return 0; }
而不是只能在 MS 平台上正常执行。
你所引用的 ISO C 条文,是 C99 的,虽然它已出了好几年,
但大家还是以 C89/90 为主,完全符合 C99 的 compiler 也
不普遍。C89/90 对於 main 不传回 int 的做法是 undefined
behavior[注],也就是说不传回 int 是不对的。
Anyway, 我之前说的「不符标准」,未能充份考究 C 的最新标准
与旧标准还有 C++标准彼此间出入的地方,谢谢你的指正。
[注] 关於这点,我并没有 C89 的原文可做确认(我只有 C99的,
C89 只有昂贵的纸本,不易取得),而是根据 comp.lang.c
上的讨论:
缩址:
http://myurl.com.tw/fq6d
原址:
http://groups.google.com.tw/group/comp.lang.c/browse_thread/thread/
e8004d31ce9cd032/73a45e2194f42369?q=main+return+type+void&hl=zh-TW&
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.130.208.168