之前有人可能會標記它相關的問題重複。我強調我DO已閱讀所有這些問題。但我仍然有一些審訊(是的,一些小的迂腐:))如果我省略C中主函數的返回類型,該怎麼辦?
對於C
一些結論:
1. In C89(C90), this is _undefined_ .
2. In C99(or C11), a type of int is madatory; control flow reached the closing }
will return a value of 0.
這纔是我的審訊。
在c89,我都一無所知不確定,但不明發現了什麼?
詳細信息:在C89相關部分是5.1.2.2.1計劃啓動和5.1.2.2.3計劃終止(注意:兩者都是5.1.2.2託管環境下部分,在其中我們後面的討論是limitted)
引用: - 5.1.2.2.3計劃終止 -
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 If the } that terminates the main function is reached, the termination status returned to the host environment is unspecified.
不過請注意部分:如果}終止...,上面清清楚楚地寫着 ,如果我們忽略了返回類型 - 因此}將達到 -
終止狀態不明根據的不確定和不明標準的定義, 我應該說,它給未指定的值,因爲不管它返回的是合法的int值,但因此不確定 - 我們無法預測哪個值會導致什麼樣的災難性後果?
在C99中,一個int類型是madatory,但
gcc --std=c99
給出,但int型測試(沒有返回類型實際上)僅給出華林:的「主」的返回類型不是「詮釋」,但不錯誤?詳細說明:相關部件與c89相同。
引用: - 5.1.2.2.1計劃啓動 -
It shall be defined with a return type of int and ...
和 - 4。 - 符合
1. In this International Standard, ‘‘shall’’ is to be interpreted as a requirement on an implementation or on a program; conversely, ‘‘shall not’’ is to be interpreted as a prohibition.
所以應應該在這個標準解釋爲madatory,爲什麼GCC與開關等--std = C99違反本?
向gcc維護者報告bug。 –
使用'-pedantic-errors'使GCC中的ISO違規錯誤 – Cubbi
'gcc -std = c99'只是一個寬鬆的近似值,它仍然吞噬了大部分被刪除的c89內容和擴展。但是,它提供了一個診斷,而不是必需的。如果你想要一個很好的近似標準,也可以使用'-pedantic-errors'。 –