[Solved] What is the result of calling return in a non-void function after calling a function returning int?


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

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.

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

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]$ 

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

cl x1.c

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>

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.

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.

EDIT:

More examples with your updated code.

On gcc with no optimizations:

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

On gcc with optimization -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

On MSVC with no optimizations:

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

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

C:\Users\dbush\Documents>

On MSVC with optimization /Ox:

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

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

C:\Users\dbush\Documents>

So again, no guarantees.

1

solved What is the result of calling return in a non-void function after calling a function returning int?