用C main()函数 [英] main() function in C

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

问题描述

我一直在学习C语言编程的一个自学成才的时尚了几个星期,有我所关于的main()函数的一些问题。


  1. 所有职能都必须在自己的函数原型声明,而且后来,在他们确定指标。为什么我们不必须首先声明的main()函数原型?


  2. 我们
  3. 为什么要使用 INT的main()而不是无效的主要() <? / p>


  4. 什么是返回0,在的main()函数究竟做什么?如果我写的结局与的main()功能的程序,会发生什么返回1​​; ,例如:



解决方案

  1. 您需要的定义或为了正确调用一个函数的原型,但绝不能由任何其他函数调用,因此它的切不可的声明。

  2. 因为C标准是这么说的。操作系统传递返回值给调用程序(通常为壳)。一些编译器会接受无效的主要,但这是一个非标准扩展(这通​​常意味着总是返回零到OS)。

  3. 按照惯例,发生了错误非零返回值的信号。 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.

  1. 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?

  2. Why do we have to use int main() instead of void main()?

  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?

解决方案

  1. 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.
  2. 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").
  3. 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屋!

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