C 的 main() 函数的有效签名是什么?

发布于 2024-08-18 19:38:53 字数 110 浏览 5 评论 0原文

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

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

还有其他有效的吗?

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

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

Are there other valid ones?

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

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

发布评论

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

评论(5

与之呼应 2024-08-25 19:38:53

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

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

尽管它确实提到了短语“或等效项”并带有以下脚注:

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

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

相关文本(5.1.2.2.1 节,但这一特定方面与 C99 相比没有变化)指出:

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

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

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

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

或同等内容;或者以其他一些实现定义的方式。

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

  • argc 的值应为非负数。

  • argv[argc] 应为空指针。

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

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

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

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

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

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:

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.

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

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.

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

  • The value of argc shall be nonnegative.

  • argv[argc] shall be a null pointer.

  • 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.

  • 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.

  • 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.

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:

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.

抚你发端 2024-08-25 19:38:53

标准 C

对于托管环境(这是正常的环境),C99 标准规定:

5.1.2.2.1 程序启动

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

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

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

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

或等效的;9)或以其他一些实现定义的方式。

9) 因此,int 可以替换为定义为 int 的 typedef 名称,或 argv 的类型code> 可以写成 char **argv 等。

C11和C18标准与C99标准基本相同。

标准 C++

C++98 标准规定:

3.6.1 主要函数[basic.start.main]

1 程序应包含一个名为 main 的全局函数,它是程序的指定开始。 [...]

2 实现不应预定义主函数。该函数不得超载。它将
有一个 int 类型的返回类型,但除此之外,其类型是实现定义的。
所有实施
应允许 main 的以下两个定义:

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

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

C++ 标准明确表示“它 [主函数] 应具有 int 类型的返回类型,但否则其类型是实现定义的”,并且要求与 C 标准相同的两个签名。因此,C++ 标准直接不允许“void main()”,尽管它无法阻止非标准一致性实现允许替代方案(也无法阻止标准一致性实现允许替代方案作为标准的扩展)。

C++03、C++11、C++14 和 C++17 标准与 C++98 基本相同。

公共扩展

经典上,Unix 系统支持第三种变体:

int main(int argc, char **argv, char **envp) { ... }

第三个参数是一个以 null 结尾的字符串指针列表,每个字符串都是一个环境变量,有一个名称、一个等号和一个值(可能为空)。如果您不使用它,您仍然可以通过“extern char **environ;”获取环境。该变量(仍然)未在任何 POSIX 标头中声明(尽管有此答案的先前版本)。

这被 C 标准认可为通用扩展,记录在附录 J 中:

###J.5.1 环境参数

¶1 在托管环境中,main 函数接收第三个参数 char *envp[]
指向一个以 null 结尾的 char 指针数组,每个指针都指向一个字符串
提供有关程序执行环境的信息 (5.1.2.2.1)。

Microsoft C

Microsoft VS 2010 编译器很有趣。该网站说:

main 的声明语法是

 int main();

或者,可选,

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

或者,mainwmain 函数可以声明为返回 void(无返回值)。如果将 mainwmain 声明为返回 void,则无法使用 return 语句将退出代码返回到父进程或操作系统。要在 mainwmain 声明为 void 时返回退出代码,您必须使用 exit 函数。< /p>

我不清楚当带有 void main() 的程序退出时会发生什么(什么退出代码返回给父级或操作系统)——并且 MS 网站也保持沉默。

有趣的是,MS 并没有规定 C 和 C++ 标准所要求的 main() 的双参数版本。它只规定了三参数形式,其中第三个参数是 char **envp,指向环境变量列表的指针。

Microsoft 页面还列出了一些其他替代方案 - wmain()(它采用宽字符字符串)等等。

此页面的 Microsoft VS 2005 版本 没有列出 void main() 作为替代方案。 Microsoft VS 2008 及以后的版本做。

int main()int main(void) 相同吗?

有关详细分析,请参阅我对 在 C 和 C++ 中 main() 应该返回什么。 (似乎我曾经认为这个问题提到了C++,尽管它没有也从来没有这样做过。在C++中,int main()int main( void)int main() 是惯用的 C++。)

在 C 中,这两种表示法之间存在差异,但您只有在深奥的情况下才会注意到它。具体来说,如果您从自己的代码中调用 main() 函数,则会有所不同,您可以在 C 中执行此操作,但不允许在 C++ 中执行此操作。

int main() 表示法不提供 main() 的原型,但这仅在您递归调用它时才有意义。使用 int main(),您稍后可以(在同一个函数或另一个函数中)编写 int rc = main("absolute", "twaddle", 2): 正式地,编译器不应该抱怨拒绝编译代码,尽管它可能会合理地抱怨(警告你)(并且在 GCC 中使用 -Werror 会将警告转换为错误)。如果您使用 int main(void) ,则对 main() 的后续调用应该会生成错误 - 您说该函数不接受任何参数,但试图提供三个参数。当然,在声明或定义 main() 之前,您不能合法地调用它(除非您仍在使用 C90 语义)——并且该实现不会声明 main 的原型()。注意:C11 标准在不同的示例中说明了 int main()int main(void) — 两者在 C 中都是有效的,尽管它们之间存在细微的差异。

Standard C

For a hosted environment (that's the normal one), the C99 standard says:

5.1.2.2.1 Program startup

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;9) or in some other implementation-defined manner.

9) 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.

The C11 and C18 standards say essentially the same as the C99 standard.

Standard C++

The C++98 standard says:

3.6.1 Main function [basic.start.main]

1 A program shall contain a global function called main, which is the designated start of the program. [...]

2 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.
All implementations
shall allow both of the following definitions of main:

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

and

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

The C++ standard explicitly says "It [the main function] shall have a return type of type int, but otherwise its type is implementation defined", and requires the same two signatures as the C standard. So a 'void main()' is directly not allowed by the C++ standard, though there's nothing it can do to stop a non-standard conforming implementation from allowing alternatives (nor a standard conforming implementation from allowing alternatives as extensions to the standard).

The C++03, C++11, C++14, and C++17 standards say essentially the same as C++98.

Common Extension

Classically, Unix systems support a third variant:

int main(int argc, char **argv, char **envp) { ... }

The third argument is a null-terminated list of pointers to strings, each of which is an environment variable which has a name, an equals sign, and a value (possibly empty). If you do not use this, you can still get at the environment via 'extern char **environ;'. This variable is (still) not declared in any POSIX header (previous versions of this answer notwithstanding).

This is recognized by the C standard as a common extension, documented in Annex J:

###J.5.1 Environment arguments

¶1 In a hosted environment, the main function receives a third argument, char *envp[],
that points to a null-terminated array of pointers to char, each of which points to a string
that provides information about the environment for this execution of the program (5.1.2.2.1).

Microsoft C

The Microsoft VS 2010 compiler is interesting. The web site says:

The declaration syntax for main is

 int main();

or, optionally,

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

Alternatively, the main and wmain functions can be declared as returning void (no return value). If you declare main or wmain as returning void, you cannot return an exit code to the parent process or operating system by using a return statement. To return an exit code when main or wmain is declared as void, you must use the exit function.

It is not clear to me what happens (what exit code is returned to the parent or o/s) when a program with void main() does exit — and the MS web site is silent too.

Interestingly, MS does not prescribe the two-argument version of main() that the C and C++ standards require. It only prescribes a three-argument form where the third argument is char **envp, a pointer to a list of environment variables.

The Microsoft page also lists some other alternatives — wmain() which takes wide-character strings, and some more.

The Microsoft VS 2005 version of this page does not list void main() as an alternative. The versions from Microsoft VS 2008 onwards do.

Is int main() the same as int main(void)?

For a detailed analysis, see the end of my answer to What should main() return in C and C++. (It seems that I once considered that this question referred to C++, even though it doesn't and never did. In C++, there is no difference between int main() and int main(void) and int main() is idiomatic C++.)

In C, there is a difference between the two notations, but you only notice it in esoteric cases. Specifically, there's a difference if you call the main() function from your own code, which you're allowed to do in C and are not allowed to do in C++.

The int main() notation does not provide a prototype for main(), but that only matters if you call it recursively. With int main(), you might later (in the same function, or in another function) write int rc = main("absolute", "twaddle", 2): and formally the compiler shouldn't complain to the extent of refusing to compile the code, though it might legitimately complain (warn you) about it (and using -Werror with GCC would convert the warning into an error). If you use int main(void), the subsequent call to main() should generate an error — you said the function takes no arguments but tried to provide three. Of course, you can't legitimately call main() before you've declared or defined it (unless you are still using C90 semantics) — and the implementation does not declare a prototype for main(). NB: The C11 standard illustrates both int main() and int main(void) in different examples — both are valid in C, even though there's the subtle difference between them.

瑕疵 2024-08-25 19:38:53

POSIX 支持 execve(),它反过来支持

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

添加的参数是环境,即形式为 NAME=VALUE 的字符串数组。

POSIX supports execve(), which in turn supports

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

The added argument is the environment, i.e. an array of strings of the form NAME=VALUE.

天涯离梦残月幽梦 2024-08-25 19:38:53

http://en.wikipedia.org/wiki/Main_function_(programming) #C_and_C.2B.2B

除了通常的 int main(int argc, char *argv[]) 和 POSIX int main(int argc, char **argv, char **envp),在 Mac OS X 上也支持,

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

当然它仅限于 Mac。

在 Windows 上有

int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);

Unicode(实际上是宽字符)变体。当然还有WinMain

http://en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

Besides the usual int main(int argc, char *argv[]) and the POSIX int main(int argc, char **argv, char **envp), on Mac OS X also supports

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

Of course it's Mac-only.

On Windows there's

int wmain(int argc, wchar_t* argv[], wchar_t* envp[]);

as the Unicode (actually, wide-character) variant. Of course there is WinMain too.

初心 2024-08-25 19:38:53
int main(void)

在某些操作系统(例如 Windows)下,这也是有效的:

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

其中 envp 提供一个环境,否则可以通过 getenv() 访问

int main(void)

Under some OS (for example, Windows) also such is valid:

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

where envp gives an environment, otherwise accessible through getenv()

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