在 ANSI C 中正确声明 main() 函数
C标准说:
程序启动时调用的函数 被命名为主。实施情况 没有为此声明原型 功能。它应定义为 返回类型为 int 并且没有 参数:
int main(void) { /* ... */ }
或者带有两个参数(参考 这里作为 argc 和 argv,尽管任何 可以使用名称,因为它们是本地名称 到它们所在的功能 声明):
int main(int argc, char *argv[]) { /* ... */ }
或同等或其他一些 实现定义的方式。
然而,Kernighan & Ritchie 在他们的第二版(规范的 ANSI C)圣经中只是使用:
main()
{
/* taram pampam ... */
return 0;
}
谁是对的? 它是否与没有返回值的函数自动假定在 C 中返回 int
有关?
The C standard say:
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:int main(void) { /* ... */ }
or with two parameters (referred to
here as argc and argv, though any
names may be used, as they are local
to the function in which they are
declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent or in some other
implementation-defined manner.
However, Kernighan & Ritchie in their second edition (the canonical ANSI C) bible just use:
main()
{
/* taram pampam ... */
return 0;
}
Who is right?
Does it have to do with function without return value automatic assume to be returning int
in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,如果您想要 ANSI C,那么根据定义,该标准就是正确的。
在 C89/C90 中,隐含了
int
返回类型,因此 K&R 定义是可以接受的。在 C99 中,情况不再如此。
C90 标准具有以下措辞(5.1.2.2.1 程序启动),与 C99 措辞非常相似(可能最重要的是它使用不太强的“can”而不是“shall”):
该部分没有直接说明省略返回类型将导致其默认为
int
的事实。坦率地说,我很难准确地找到标准指定该行为的位置。我能想到的最接近的是 6.7.1(函数定义),其中函数定义的语法表明“声明说明符”是可选的,示例如下:
Well, if you want ANSI C, then by definition the standard is right.
In C89/C90 the
int
return type is implied, so the K&R definition would be acceptable.In C99 this is no longer the case.
The C90 standard has the following wording (5.1.2.2.1 Program startup), which is very similar to the C99 wording (probably most significantly it uses the less strong 'can' instead of 'shall'):
There's nothing in that section directly about the fact that leaving off the return type will result in it defaulting to
int
.Frankly, I have a hard time finding exactly where that behavior is specified by the standard. The closest I can come is in 6.7.1 (Functions definitions) where the grammar for function definitions indicates that the 'declaration-specifiers' are optional, and the examples say:
是的,在 C89(最初的 C 标准)中,如果声明的函数没有返回类型,则假定返回 int。 C99 要求所有函数都有显式返回类型。
Yes, in C89 (the original C standard), if a function is declared without a return type, it is assumed to return
int
. C99 requires an explicit return type on all functions.另外,main() 和 main(void) 之间有一个微妙的区别(至少在声明中)——
函数(隐式)返回 int 并接受未指定数量的参数,
不接受 参数。
Also, there's a subtle difference (at least in declarations) between main() and main(void) --
is a function (implicitly) returning int and taking an unspecified number of arguments
takes no arguments.
K&RI 的版本于 1988 年印刷。当时该标准尚未出台,因此存在一些不一致之处。不过,第二版的大部分内容都符合 C89 标准。
我找到了C89 标准的文本版本(YAY for Google);它说:
The version of K&R I have was printed in 1988. The Standard wasn't out by then, so there are some inconsistencies. However, most of the 2nd edition complies with the C89 Standard.
I found a text version of the C89 Standard (YAY for Google); it says: