【发布时间】:2012-12-05 18:50:38
【问题描述】:
在任何人可能将其标记为相关问题的重复之前。我强调我确实已经阅读了所有这些问题。但我仍然有一些审问(是的,一些小迂腐:))
对于 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,我没有找到关于undefined,但unspecified?
详情:C89中的相关部分为5.1.2.2.1程序启动和5.1.2.2.3程序终止(注意:两者都在5.1.2.2 托管环境部分,我们后面的讨论仅限于此)
引用: -- 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.请注意那部分:如果 } 终止 ... ,它清楚地表明 如果我们省略返回类型 - 因此 } 将在 -
终止状态是未指定根据undefined和unspecified标准的定义, 我应该说它给出了 unspecified value 因为无论它返回什么都是 合法的 int 值,但 后果未定义-我们无法预测什么值 会导致什么灾难性的后果?
-
在 c99 中,int 类型是强制性的,但
gcc --std=c99给出的测试没有 int 类型(实际上没有返回类型)只给出 waring:return type of 'main' is not '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.所以 shall 在本标准中应该被解释为 madatory,为什么 gcc 和 swith --std=c99 违反了这一点?
【问题讨论】:
-
向 gcc 维护者报告错误。
-
使用
-pedantic-errors在 GCC 中产生 ISO 违规错误 -
gcc -std=c99只是一个松散的近似值,它仍然吞噬了大部分已删除的 c89 内容和扩展。但是,它提供了诊断,并且不需要更多。如果你想要一个很好的标准近似值,也可以使用-pedantic-errors。 -
是的,返回类型是强制性的,我相当旧的 GCC (4.4.5) 版本会使用
-std=c99或更高版本警告它。 -
未指定是未定义的Synonym
标签: c