在 ANSI C 中正确声明 main() 函数

发布于 2024-08-11 13:58:47 字数 560 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

囍孤女 2024-08-18 13:58:47

好吧,如果您想要 ANSI C,那么根据定义,该标准就是正确的。

在 C89/C90 中,隐含了 int 返回类型,因此 K&R 定义是可以接受的。

在 C99 中,情况不再如此。

C90 标准具有以下措辞(5.1.2.2.1 程序启动),与 C99 措辞非常相似(可能最重要的是它使用不太强的“can”而不是“shall”):

程序启动时调用的函数名为main。该实现没有声明该函数的原型。它可以不带参数定义:

int main(void) { /* ... */ }

或带有两个参数(此处称为argcargv,尽管可以使用任何名称,因为它们对于声明它们的函数来说是本地的) :

int main(int argc, char *argv[]) { /* ... */ }

如果定义了它们,则 main 函数的参数应遵守以下约束:

[等等。 ...]

该部分没有直接说明省略返回类型将导致其默认为 int 的事实。

坦率地说,我很难准确地找到标准指定该行为的位置。我能想到的最接近的是 6.7.1(函数定义),其中函数定义的语法表明“声明说明符”是可选的,示例如下:

示例:

  1. 如下:

     extern int max(int a, int b)
      {
          返回一个>乙?甲:乙;
      }
    

    extern 是存储类说明符,int 是类型说明符(每个都可以省略,因为它们是默认值)...

    < /里>

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'):

The function called at program startup is named main. The implementation declares no prototype for this function. It can be defined 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[]) { /* ... */ }

If they are defined, the parameters to the main function shall obey the following constraints:

[etc. ...]

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:

Examples:

  1. In the following:

      extern int max(int a, int b)
      {
          return a > b ? a : b;
      }
    

    extern is the storage class specifier and int is the type specifier (each of which may be omitted as those are the defaults)...

断桥再见 2024-08-18 13:58:47

是的,在 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.

瀟灑尐姊 2024-08-18 13:58:47

另外,main() 和 main(void) 之间有一个微妙的区别(至少在声明中)——

main()

函数(隐式)返回 int 并接受未指定数量的参数,

main(void)

不接受 参数。

Also, there's a subtle difference (at least in declarations) between main() and main(void) --

main()

is a function (implicitly) returning int and taking an unspecified number of arguments

main(void)

takes no arguments.

梦罢 2024-08-18 13:58:47

K&RI 的版本于 1988 年印刷。当时该标准尚未出台,因此存在一些不一致之处。不过,第二版的大部分内容都符合 C89 标准。

我找到了C89 标准的文本版本(YAY for Google);它说:

“程序启动”

程序调用的函数
启动名为 main 。这
实现声明没有原型
对于这个功能。可以定义
不带参数:

 int main(void) { /*...*/ }

或者带有两个参数(参考
这里作为 argc 和 argv ,尽管任何
可以使用名称,因为它们是本地名称
到它们所在的功能
声明):

 int main(int argc, char *argv[]) { /*...*/ }

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:

"Program startup"

The function called at program
startup is named main . The
implementation declares no prototype
for this function. It can be defined
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[]) { /*...*/ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文