c++ 吗?标准禁止 void main() 原型?

发布于 2024-08-14 12:11:31 字数 218 浏览 2 评论 0原文

在 C++ 标准 1998 和 2003 版的 3.6.1.2 节中,

实现不应预定义主函数。该函数不得超载。它将 具有 int 类型的返回类型,但除此之外,其类型是实现定义的。

我不是以英语为母语的人。我不知道“但是否则”是什么意思。是否是禁止其他返回类型,或者给予C++编译器编写者权利?

那么答案是什么呢?

In section 3.6.1.2 of both C++ Standard 1998 and 2003 editions,

An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation-defined.

I am not a native English speaker.I do not sure what does"but otherwise" means.Whether it is to prohibit the other return type,or to give the right to C++ compiler writer?

So what's the answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

‖放下 2024-08-21 12:11:31

您引用的英语确实禁止声明 main 返回 void。它允许传入参数的变化,但不允许返回类型的变化。

The english you quote does prohibit declaring main to return void. It is allowing variation in the arguments that come in, but not in the return type.

复古式 2024-08-21 12:11:31

啊啊!是的,确实如此。标准允许的唯一返回类型是 int。引用第 3.6.1 节:

它的返回类型应为
int,但除此之外它的类型是
实现定义的。

这意味着它可能看起来像这样:

int main( float f );
int main( int x, int y );

等等等等。

Aaargh! Yes it does. The only return type allowed by the standard is int. To quote from section 3.6.1:

It shall have a return type of type
int, but otherwise its type is
implementation-defined.

meaning it could look like this:

int main( float f );
int main( int x, int y );

etc. etc.

溺ぐ爱和你が 2024-08-21 12:11:31

该类型不仅仅包含返回类型。因此,返回类型必须是 int,但是您可以自由考虑剩余的参数,即您可以在 和 之间进行

int main()

选择

int main(int argc, char **argv)

The type contains more than just the return type. Hence, the return type must be int, but you are free considering the remaining argument, i.e. you may e.g., select between

int main()

and

int main(int argc, char **argv)
烟─花易冷 2024-08-21 12:11:31

标准规定返回类型必须为 int,但类型的其余部分取决于实现。例如,您可以制作一个符合标准(但不是很有用)的 C++ 编译器。

int main(int secondsSinceSystemStart, int myFavoriteNumber, char* aFunnyJoke)

来自维基百科

在 C 和 C++ 中,主函数的函数原型如下所示:

int main(void)
int main(int argc, char **argv)

参数argc(参数计数)和argv(参数向量)分别给出程序命令行参数的数量和值。 argc 和 argv 的名称可以是任何有效的标识符,但使用这些名称是常见的约定。 C 和 C++ 标准也允许其他依赖于平台的格式;例如,Unix(尽管不是 POSIX.1)和 Microsoft Visual C++ 有第三个参数,给出程序的环境,否则可以通过 stdlib.h 中的 getenv 进行访问:

int main(int argc, char **argv, char **envp)

Mac OS X 和 Darwin 有第四个参数,其中包含操作系统提供的任意信息,例如执行二进制文件的路径:

int main(int argc, char **argv, char **envp, char **apple)

The standard is saying that the return type must be int, but that the rest of the type is up to the implementation. For example, you could make a standard-compliant (but not terribly useful) C++ compiler that used.

int main(int secondsSinceSystemStart, int myFavoriteNumber, char* aFunnyJoke)

From Wikipedia:

In C and C++, the function prototype of the main function looks like one of the following:

int main(void)
int main(int argc, char **argv)

The parameters argc, argument count, and argv, argument vector, respectively give the number and value of the program's command-line arguments. The names of argc and argv may be any valid identifier, but it is common convention to use these names. Other platform-dependent formats are also allowed by the C and C++ standards; for example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving the program's environment, otherwise accessible through getenv in stdlib.h:

int main(int argc, char **argv, char **envp)

Mac OS X and Darwin have a fourth parameter containing arbitrary OS-supplied information, such as the path to the executing binary:

int main(int argc, char **argv, char **envp, char **apple)
慢慢从新开始 2024-08-21 12:11:31

就参数而言,它允许

  1. int main()

  2. int main(int argc , char * argv[])

  3. int main(int argc , char * argv[] , char * envr[])

但作为为了保持一致性,每个标准返回类型应为int

As far as parameters are concern ,it allows

  1. int main()

  2. int main(int argc , char * argv[])

  3. int main(int argc , char * argv[] , char * envr[])

But as per standard return type should be int for consistency purpose.

温柔戏命师 2024-08-21 12:11:31

其目的是说,除返回类型之外的 main 函数类型的各个方面都是实现定义的。这意味着该标准的这一子句允许此声明:

int main(int fred, char *bouncy);

但不允许此声明:

void main(int fred, char *bouncy);

其返回类型必须int,但实现允许具有不同的参数类型。

The intent is to say that aspects of the type of the main function other than the return type are implementation defined. That means this declaration is allowed by this clause of the standard:

int main(int fred, char *bouncy);

but not this one:

void main(int fred, char *bouncy);

Its return type must be int, but the implementation is allowed to have different argument types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文