调用返回int的函数后,在非void函数中调用return的结果是什么? [英] What is the result of calling return in a non-void function after calling a function returning int?

查看:50
本文介绍了调用返回int的函数后,在非void函数中调用return的结果是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2018 年更新:这是我写的一个老问题,我对 C 的理解减少了.请不要投反对票.

当我使用以下代码时:

int mytest(void);
int main(void)
{
   mytest();
   return;
}
int mytest(void)
{
   return 3;
}

main 的返回值是多少?我明白这是

What is the return value of main? I understand this is

  • 未定义的行为
  • 可能会产生错误

大评论:我知道这是未定义的行为.逻辑上,会产生什么返回值?

large comment: I know this is undefined behavior. Logically, what return value will be produced?

编辑 2:示例:http://ideone.com/fAxnNn

Edit 2: Sample: http://ideone.com/fAxnNn

推荐答案

这里的问题是你似乎不明白 未定义的行为意味着.

The issue here is you don't seem to understand what undefined behavior means.

当您调用未定义的行为时,任何事情都可能发生.您的程序可能会崩溃,可能会产生意想不到的结果,或者它可能看起来工作正常.进行看似无关的更改,例如添加未使用的变量或额外调用 printf,可以改变未定义行为的表现方式.

When you invoke undefined behavior, anything can happen. Your program can crash, it can generate unexpected results, or it can appear to work correctly. Making a seeming unrelated change, such as adding an unused variable or an extra call to printf, can change how undefined behavior manifests itself.

这也意味着两个不同的编译器可以为相同的代码生成不同的结果,或者具有两种不同优化设置的一个编译器可以生成不同的结果.

This also means that two different compilers can generate different results for the same code, or that one compiler with two different optimization settings can generate different results.

我给你举个例子.我在带有 gcc 4.1.2 的 CentOS 5.10 上编译了您的原始代码.首先我编译没有任何优化:

I'll give you an example. I compiled your original code on a CentOS 5.10 with gcc 4.1.2. First I compiled without any optimization:

gcc -Wall -Wextra -g -o /tmp/x1 /tmp/x1.c

我运行了生成的代码,然后运行了 echo $?.后者打印前一个进程的退出代码(即 main 的返回值).结果:

I ran the resulting code and then ran echo $?. The latter prints the exit code of the previous process (i.e. the return value of main). The result:

[dbush@db-centos tmp]$ /tmp/x1
[dbush@db-centos tmp]$ echo $?
3
[dbush@db-centos tmp]$ 

现在我将在 Visual Studio 2010 下的 Windows 7 机器上编译相同的代码:

Now I'll compile the same code on a Windows 7 machine under Visual Studio 2010:

cl x1.c

如果我运行这个然后 echo %ERRORLEVEL% 打印返回码,我得到这个:

If I run this and then echo %ERRORLEVEL% to print the return code, I get this:

C:\Users\dbush\Documents>x1

C:\Users\dbush\Documents>echo %ERRORLEVEL%
0

C:\Users\dbush\Documents>

如您所见,gcc 和 MSVC 对相同的代码生成不同的结果.在一种情况下它返回 3,而在另一种情况下它返回 0.

As you can see, gcc and MSVC generate different results for the same code. In one case it returns 3 while in the other case it return 0.

现在让我们进行优化.我在同一台 CentOS 机器上用相同版本的 gcc 编译了相同的代码,但开启了优化:

Now let's play around with optimization. I compiled the same code on the same CentOS machine with the same version of gcc, but with optimization turned on:

gcc -Wall -Wextra -g -o /tmp/x1 /tmp/x1.c -O3

然后我运行了两次.然后我得到了以下结果:

I then ran this twice. I then got the following result:

[dbush@db-centos tmp]$ /tmp/x1
[dbush@db-centos tmp]$ echo $?
68
[dbush@db-centos tmp]$ /tmp/x1
[dbush@db-centos tmp]$ echo $?
36
[dbush@db-centos tmp]$ 

因此,在使用不同的优化设置运行时,我不仅会得到不同的结果,而且多次运行相同的程序也会得到不同的结果.这就是未定义行为可能发生的情况.

So not only do I get different results when running with different optimization settings, I get different results running the same program multiple times. That's what can happen with undefined behavior.

所以要回答您的问题将产生什么返回值",答案是这取决于您调用了未定义的行为.您无法可靠地预测会发生什么.

So to answer your question "what return value will be produced", the answer is it depends because you invoked undefined behavior. You can't reliably predict what will happen.

更多带有更新代码的示例.

More examples with your updated code.

在没有优化的 gcc 上:

On gcc with no optimizations:

[dbush@db-centos tmp]$ /tmp/x1
mytest2 = 3
[dbush@db-centos tmp]$ echo $?
12

在gcc上优化-O3:

[dbush@db-centos tmp]$ /tmp/x1
mytest2 = -1078711820
[dbush@db-centos tmp]$ echo $?
22
[dbush@db-centos tmp]$ /tmp/x1
mytest2 = -1077511916
[dbush@db-centos tmp]$ echo $?
22

在没有优化的 MSVC 上:

On MSVC with no optimizations:

C:\Users\dbush\Documents>x1
mytest2 = 3

C:\Users\dbush\Documents>echo %ERRORLEVEL%
0

C:\Users\dbush\Documents>

在带有优化的 MSVC 上/Ox:

On MSVC with optimization /Ox:

C:\Users\dbush\Documents>x1
mytest2 = 1

C:\Users\dbush\Documents>echo %ERRORLEVEL%
0

C:\Users\dbush\Documents>

同样,不能保证.

这篇关于调用返回int的函数后,在非void函数中调用return的结果是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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