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

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

问题描述

真的是什么在C主要功能有效签名?我知道:

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

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

还有没有其他有效的吗?

Are there other valid ones?

推荐答案

本标准作为这个答案的时间(C11)明确提到这两个:

The current standard as at the time of this answer (C11) 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 可以定义为 INT 的typedef 名称替换code>或的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 C11 ,但这个特殊的方面是不变 C99 )规定:

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

称为在程序启动时的功能被命名为主力。实施声明没有原型此功能。它应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主要(无效){/ * ... * /}

或具有两个参数(此处称为argc和argv,虽然任何名称可能是
  使用,因为它们是局部的,其中声明它们的功能):

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主(INT ARGC,CHAR *的argv []){/ * ... * /}

或同等学历;或在一些其它实现定义方式。

or equivalent; or in some other implementation-defined manner.

如果它们被声明,参数的主要功能应遵循以下限制:

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


      
  • ARGC的值应为正数。

  • The value of argc shall be nonnegative.

ARGV [ARGC]应是一个空指针。

argv[argc] shall be a null pointer.

如果argc个的值是大于零,阵列成员的argv [0]到
  的argv [ARGC-1]包容应包含指向字符串的指针,这是给
  之前由程序启动主机环境实现定义的值。该
  目的是提供给确定之前编程的启动程序信息
  从托管环境别处。如果主机环境不能够
  供应与大写和小写字母串,实现
  应确保字符串用小写收到。

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]
  再presents程序名称;的argv [0] [0]应为空字符,如果
  程序名称是不能从主机环境。如果的argc的值是
  大于一,琴弦通过的argv所指向的argv [1] [的argc-1]
  再present程序参数。

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.

参数argc和argv与字符串指向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天全站免登陆