看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《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