C 的 main() 函数的有效签名是什么? [英] What are the valid signatures for C's main() function?

查看:27
本文介绍了C 的 main() 函数的有效签名是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 中 main 函数的有效签名到底是什么?我知道:

What really are the valid signatures for main function in C? I know:

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

还有其他有效的吗?

推荐答案

C11 标准明确提到了这两个:

The C11 standard explicitly mentions these two:

int main(void);
int main(int argc, char* argv[]);

虽然它确实提到了短语或等效物"带有以下脚注:

although it does mention the phrase "or equivalent" with the following footnote:

因此,int 可以替换为定义为 inttypedef 名称,或 argv 的类型可以写成char ** argv,依此类推.

Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.

此外,它还提供了更多(实现定义的)可能性.

In addition, it also provides for more (implementation-defined) possibilities.

相关文本(5.1.2.2.1 部分,但此特定方面与 C99 相同)指出:

The relevant text (section 5.1.2.2.1, but this particular aspect is unchanged from C99) states:

程序启动时调用的函数名为main.实现声明没有此函数的原型.它应定义为返回类型为 int 且不带参数:

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) { /* ... */ }

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

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.

如果声明了它们,main 函数的参数应遵守以下约束:

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

  • argc 的值应为非负.

argv[argc] 应为空指针.

如果argc的值大于零,数组成员argv[0]argv[argc-1] inclusive 应包含指向字符串的指针,这些字符串在程序启动之前由宿主环境赋予实现定义的值.目的是从托管环境中的其他地方向程序启动之前确定的程序提供信息.如果宿主环境无法提供大写和小写字母的字符串,则实现应确保以小写形式接收字符串.

If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

如果argc的值大于零,则argv[0]指向的字符串代表程序名;argv[0][0] 如果程序名称在宿主环境中不可用,则应为空字符.如果argc的值大于1,则argv[1]argv[argc-1]指向的字符串代表程序参数.

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

参数argcargv 以及argv 数组指向的字符串应可由程序修改,并保留它们在程序启动和程序终止之间最后存储的值.

The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

请注意,这是针对托管环境,即您通常在 C 程序中看到的环境.如同一标准的 5.1.2.1 所述,独立环境(例如嵌入式系统)的约束要少得多:

Note that this is for a hosted environment, the ones you normally see in C programs. A free-standing environment (such as an embedded system) is far less constrained, as stated in 5.1.2.1 of that same standard:

在独立环境中(在这种环境中,C 程序可以在没有任何操作系统优势的情况下执行),程序启动时调用的函数的名称和类型是实现定义的.除了第 4 条要求的最小集合之外,任何独立程序可用的库设施都是实现定义的.

In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

这篇关于C 的 main() 函数的有效签名是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆