GCC默认的主返回值不为零 [英] GCC default main return value is not zero

查看:182
本文介绍了GCC默认的主返回值不为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有没有从主任何明确的回报一​​些C程序是这样的:

I have some C programs without any explicit return from main, like this:

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

如果我编译他们GCC 4.6.3和以下选项:

If I compile them with GCC 4.6.3 and the following options:

gcc file.c -Wall -Wextra

程序不返回0,给我的问题,但标准(以及让很多其他的答案)说:

Programs does not return 0 and give me problems, but the standard (and a lot of other answers on SO) says:

如果main函数的返回类型为int类型兼容,从最初的调用主函数的返回等同于调用由主函数作为参数返回的值exit函数; [10] 到达}终止主函数返回值为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;[10] 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.

[10]根据6.2.4,具有自动存储持续时间的对象在主声明的寿命将已在前者的情况下结束,即使他们不会在后者有

[10] In accordance with 6.2.4, the lifetimes of objects with automatic storage duration declared in main will have ended in the former case, even where they would not have in the latter.

我的程序返回不同的值,160,15,14 ...垃圾?

My programs return different values, 160, 15, 14... garbage?

我缺少的东西吗?这是一个GCC的bug?找不到在海湾合作委员会的在线文档任何东西。
是gcc仍参考到这可能是没有指定一些老的C标准(pre-C99)?

Am I missing something? Is this a GCC bug? Can't find anything in the GCC online doc. Is GCC still referring to some old C standard (pre-C99) where this could be not specified?

推荐答案

默认情况下 GCC -std = gnu89 这是C90 + GNU扩展。

By default gcc is -std=gnu89 which is C90 + GNU extensions.

和C90说:

(C90,5.1.2.2.3)如果主函数执行的返回,指定值,则终止状态返回到主机环境是不确定的

(C90, 5.1.2.2.3) "If the main function executes a return that specifies no value, the termination status returned to the host environment is undefined"

-std = C99 -std = gnu99 来有一个返回值<$ C $编译C> 0 时,如果忽略主函数的返回。

Compiles with -std=c99 or -std=gnu99 to have a return value of 0 when return is omitted in main function.

这篇关于GCC默认的主返回值不为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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