是否有必要返回一个值在main()? [英] Is It Necessary to Return a Value in Main()?

查看:134
本文介绍了是否有必要返回一个值在main()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是写一个快速程序计算有些事情,当我遇到return语句/退出声明是针对C程序。

I was just writing a quick program for calculating some things when I came across the return statement/exit statement for the C program.

我宣布的main()为类型 INT ,所以我将不得不投入的回报一个整数,或者我的程序不会正确编译。但是,它是可以接受的,以 A 布尔甚至无效

I declared main() to be of type int, so I would have to put in a return of an integer, or my program would not compile correctly. However, is it acceptable to make main a Boolean or even void?

我知道要创建一个C程序的标准方法是返回一个值,所以任何的问题都可以整理出来,除其他事项外,但不会在布尔工作一样的方法?另外,可我宣布摆脱它无效,并已终止后,没有与操作系统问题仍在运行我的程序?

I know the standard way to create a C program is to return a value so any problems can be sorted out, among other things, but wouldn't a Boolean work the same way? Also, could I get away with declaring it void and not having problems with the operating system still running my program after it has been terminated?

感谢您的任何和所有帮助。

Thanks for any and all help.

推荐答案

C99标准表示:(§5.1.2.2.1的程序启动的)

The C99 standard says: (§5.1.2.2.1 Program startup)

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


  
  

或具有两个参数(此处称为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 main(int argc, char *argv[]) { /* ... */ }

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

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

所以在托管环境中, INT 是唯一有效的,标准的返回类型。实现可以定义虽然其他入口点。

So in a hosted environment, int is the only valid, standard return type. Implementations can define other entry points though.

请注意该节§5.1.2.2.3的程序终止的有这样的:

Note that section §5.1.2.2.3 Program Termination has this:

如果main函数的返回值类型是int类型的类型兼容从一回
  主功能初始呼叫等同于调用exit函数的值
  由主函数作为参数返回; 到达}终止的
  主函数返回值为0
的值。如果返回类型是不兼容INT的
  返回到主机环境的终止状态是不确定的。

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

所以,你忽略从符是C99合法的,只要你的返回 INT 。结果
(但是,$ P $ C标准的pvious版本没有这个例外 - 没有返回值(或到达最终} 没有return语句)原因的的终止状态返回到主机环境[待]未定义的。)

So you omitting a return from main is legal in C99, as long as your main returns an int.
(But previous versions of the C standard didn't have that exception for main - returning no value (or reaching the final } without a return statement) causes "the termination status returned to the host environment [to be] undefined.".)

这篇关于是否有必要返回一个值在main()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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