用C main()函数 [英] main() function in C
问题描述
我一直在学习C语言编程的一个自学成才的时尚了几个星期,有我所关于的main()
函数的一些问题。
-
所有职能都必须在自己的函数原型声明,而且后来,在他们确定指标。为什么我们不必须首先声明
的main()
函数原型? -
为什么要使用
INT的main()
而不是无效的主要()
<? / p> -
什么是返回0,在
的main()
函数究竟做什么?如果我写的结局与的main()
功能的程序,会发生什么返回1;
,例如:
我们
- 您需要的定义或为了正确调用一个函数的原型,但
主
绝不能由任何其他函数调用,因此它的切不可的声明。 - 因为C标准是这么说的。操作系统传递返回值给调用程序(通常为壳)。一些编译器会接受
无效的主要
,但这是一个非标准扩展(这通常意味着总是返回零到OS)。 - 按照惯例,发生了错误非零返回值的信号。 shell脚本和其他程序可以使用它来看看你的程序成功终止。
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.
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?
- You need either a definition or a prototype in order to properly call a function, but
main
must never be called from any other function, so it must not be declared. - 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 main()函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!