之前有人可能会标记它相关的问题重复。我强调我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'。 –