C语言 C 中的 main() 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18446686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
main() function in C
提问by Heikki
I've been learning C programming in a self-taught fashion for some weeks, and there are some questions that I have concerning the main()function.
几个星期以来,我一直以自学的方式学习 C 编程,我对这个main()函数有一些疑问。
All functions must be declared in their function prototype, and later on, in their defintion. Why don't we have to declare the
main()function in a prototype first?Why do we have to use
int main()instead ofvoid main()?What does return 0 exactly do in the
main()function? What would happen if I wrote a program ending themain()function withreturn 1;, for example?
所有函数都必须在它们的函数原型中声明,然后在它们的定义中声明。为什么我们不必先
main()在原型中声明函数?为什么我们必须使用
int main()而不是void main()?return 0 在
main()函数中到底做了什么?例如,如果我编写了一个以 结束main()函数的程序,会发生什么return 1;?
采纳答案by Fred Foo
- A declaration of a function is needed only before a function is used. The definition is itself a declaration, so no prior prototype is required. (Some compilers and other tools may warn if a function is defined without a prior prototype. This is intended as a helpful guideline, not a rule of the C language.)
- Because the C standard says so. Operating systems pass the return value to the calling program (usually the shell). Some compilers will accept
void main, but this is a non-standard extension (it usually means "always return zero to the OS"). - By convention, a non-zero return value signals that an error occurred. Shell scripts and other programs can use this to find out if your program terminated successfully.
- 只有在使用函数之前才需要声明函数。定义本身就是一个声明,因此不需要事先的原型。(如果函数是在没有事先原型的情况下定义的,一些编译器和其他工具可能会发出警告。这是一个有用的指南,而不是 C 语言的规则。)
- 因为 C 标准是这么说的。操作系统将返回值传递给调用程序(通常是 shell)。一些编译器会接受
void main,但这是一个非标准的扩展(它通常意味着“总是向操作系统返回零”)。 - 按照惯例,非零返回值表示发生了错误。Shell 脚本和其他程序可以使用它来确定您的程序是否成功终止。
回答by Yu Hao
1) All functions must be declared in their function prototype, and later on, in their definition. Why don't we have to declare the main() function in a prototype first?
1) 所有函数都必须在其函数原型中声明,然后在其定义中声明。为什么我们不必先在原型中声明 main() 函数?
Not true. Simple example:
不对。简单的例子:
void foo(){} //definition
int main()
{
foo();
return 0;
}
Only when one function is called but the definition isn't seen yet, a declaration is required. That will never happen to mainsince it is the starup of the program.
只有当调用了一个函数但还没有看到定义时,才需要声明。这永远不会发生,main因为它是程序的启动。
2) Why do we have to use int main() instead of void main()?
2) 为什么我们必须使用 int main() 而不是 void main()?
Because the standard says so. (To be more precise, it's true on a hosted environment, which is usually the case)
因为标准是这么说的。(更准确地说,在托管环境中确实如此,通常是这种情况)
C99 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 ofintand with no parameters:int main(void) { /* ... */ }or with two parameters (referred to here as
argcandargv, 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.
C99 5.1.2.2.1程序启动
程序启动时调用的函数名为
main. 实现声明没有此函数的原型。它应定义为返回类型int并且不带参数:int main(void) { /* ... */ }或带有两个参数(这里称为
argcandargv,尽管可以使用任何名称,因为它们对于声明它们的函数是本地的):int main(int argc, char *argv[]) { /* ... */ }或同等学历; 或以其他一些实现定义的方式。
3) What does return 0 exactly do in the main() function? What would happen if I wrote a program ending the main() function with return 1, for example?
3) return 0 在 main() 函数中到底做了什么?例如,如果我编写了一个以 return 1 结束 main() 函数的程序,会发生什么?
The return value indicates the result of the program. Usually 0indicates success while other values indicates different kinds of failure.
返回值表示程序的结果。通常0表示成功,而其他值表示不同类型的失败。
回答by Jens
You are not free to chose the return type of main because you did not write the code that calls main. The code calling main already existed before you even thought about learning C. It was written by the folks providing the C runtime startup code, which usually is linked automatically to your executable without you knowing. This code often resides in a file called crt0.o (created from crt0.c or even assembler in crt0.s). It expects to use a return value indicating success (0) or failure (nonzero), plus possibly other information like whether the code was terminated due to a signal and if so, which one. These are bits of Unix history, that I won't repeat here :-)
您不能随意选择 main 的返回类型,因为您没有编写调用 main 的代码。在您考虑学习 C 之前,调用 main 的代码已经存在。它是由提供C 运行时启动代码的人编写的,这些代码通常会在您不知情的情况下自动链接到您的可执行文件。此代码通常驻留在名为 crt0.o 的文件中(从 crt0.c 或什至是 crt0.s 中的汇编程序创建)。它期望使用指示成功 (0) 或失败(非零)的返回值,以及可能的其他信息,例如代码是否因信号而终止,如果是,则是哪一个。这些是 Unix 历史的一部分,我不会在这里重复:-)
回答by John Bode
1) Not necessarily; a definition also serves as a declaration. Secondly, there are only a few valid signatures for mainanyway, and you normally won't call mainwithin your code unless you're writing an entry for the IOCCC.
1) 不一定;定义也可作为声明。其次,main无论如何只有几个有效的签名,main除非您为 IOCCC 编写条目,否则您通常不会在代码中调用。
2) Short answer: because the language definition says so. Longer answer: this is how your program indicates success or failure to the host environment. An individual implementation is free to support additional signatures for main, but it must document those additional signatures. If your compiler documentation does not list void main()as a legal signature, then you shouldn't use it.
2)简短回答:因为语言定义是这样说的。更长的答案:这就是您的程序向主机环境指示成功或失败的方式。单个实现可以自由地支持 的附加签名main,但它必须记录这些附加签名。如果您的编译器文档未void main()列为合法签名,则不应使用它。
3) By convention (at least on *nix systems where C was first used), a status of 0 indicates success, and a non-zero status indicates ... something other than success. Exactly what value corresponds to what status is up to the implementation.
3) 按照惯例(至少在首次使用 C 的 *nix 系统上),状态为 0 表示成功,非零状态表示...不是成功。究竟什么值对应什么状态取决于实现。
回答by zentrunix
Functions need not necessarily be declared first as a prototype. Such a declaration is needed only if we need to use a function before it is defined.
函数不必首先声明为原型。仅当我们需要在定义之前使用函数时才需要这样的声明。
main has type int by definition.
main根据定义具有 int 类型。
The meaning of the value returned from main is conventional. The convention generally accepted is that 0 is considered success, and not 0 some kind of failure.
从 main 返回的值的含义是约定俗成的。普遍接受的约定是 0 被认为是成功,而不是 0 某种失败。
回答by Mahalakshmi
1.The main() function is implicitly called by the C library by recognizing the in-built keyword 'main'. So we don't need to declare a prototype for main function .
1.C 库通过识别内置关键字“main”隐式调用 main() 函数。所以我们不需要为 main 函数声明一个原型。
2.This I am not sure, but I think it depends on the type of editor used . In Turbo C , void main() will be accepted, whereas in dev-cpp main() should return a value.
2.这个我不确定,但我认为这取决于使用的编辑器的类型。在 Turbo C 中, void main() 将被接受,而在 dev-cpp main() 中应该返回一个值。
3.return 0 simply exits the program with exit status 0 , in other words the return value determines the exit status of the main thread.
3.return 0 只是以退出状态 0 退出程序,换句话说,返回值决定了主线程的退出状态。
回答by arapEST
Simply put most essence for all of your questions is traditions and conformance. Toolchains, operating systems, etc know that way that this procedure called main(), must be called out first from the user code space(program)...
简单地说,您所有问题的最本质是传统和一致性。工具链、操作系统等都知道,这个称为 main() 的过程必须首先从用户代码空间(程序)中调用......
Now specifically: 1) Because of the conformance as I said. You do not need to declare because toolchains and operating systems know already about main. Also there are other conformance functions like exit().
现在特别是:1)因为我说的一致性。您不需要声明,因为工具链和操作系统已经知道 main。还有其他一致性函数,如 exit()。
2) When the main some time returns then the operating system can have the result back from it. Usually non zero means error. So when U are using scripts or other programs calling out your program e.g. main() function, you can check if it was successful.
2)当主要一段时间返回时,操作系统可以从中获得结果。通常非零意味着错误。因此,当您使用脚本或其他程序调用您的程序时,例如 main() 函数,您可以检查它是否成功。
3) Return something else than zero means error. But actually you can interpret that value how you want it. But as I said OS can have the result.
3) 返回除零以外的其他内容意味着错误。但实际上,您可以按照自己的意愿解释该值。但正如我所说,操作系统可以有结果。
Additional info: main() is actually not THE FIRST function (you have written) that will be called out when you start the program. BUT actually operating systems and tool chains facilitate other calls before your main, to setup environment, do the initialization or whatever. But you do not know about that directly when you are writing your code and you do not have to deal with that and think about that at all. In embedded systems there will be usually some very low level functions called to setup the CPU main clock, interrupts, stack, etc. Some of the tool chains like IAR actually can enable you to execute your own code before main is called.
附加信息: main() 实际上不是第一个在您启动程序时会被调用的函数(您已编写)。但实际上操作系统和工具链在您的主要之前促进了其他调用,以设置环境,进行初始化或其他任何事情。但是,当您编写代码时,您并不直接知道这一点,而且您根本不必处理和考虑它。在嵌入式系统中,通常会调用一些非常低级的函数来设置 CPU 主时钟、中断、堆栈等。一些工具链(如 IAR)实际上可以让您在调用 main 之前执行自己的代码。
Hope this helped :)
希望这有帮助:)
回答by UA_
When we run a C program, Computers control passes over to the C programs main()function, From there itself the C program starts to execute
当我们运行一个 C 程序时,计算机控制权传递给 C 程序的main()函数,从那里 C 程序开始执行
回答by jambono
1) it is false, you can only create the defintion of a function.
1)它是错误的,你只能创建一个函数的定义。
2) we can know if the main() function correctly terminate
2) 我们可以知道 main() 函数是否正确终止
3)the same except that in your shell it will be writed 1 instead of 0
3)除了在你的shell中它会被写成1而不是0

