The problem here is that you don't seem to understand what undefined behavior means.
When you invoke undefined behavior, anything can happen. Your program may crash, it may generate unexpected results, or it may appear that it is working correctly. Making a seemingly unrelated change, such as adding an unused variable or an optional printf call, can change the way undefined behavior appears.
It also means that two different compilers can generate different results for the same code, or one compiler with two different optimization settings can generate different results.
I will give you an example. I compiled your original code on CentOS 5.10 with gcc 4.1.2. At 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 ). Result:
[ dbush@db-centos tmp]$ /tmp/x1 [ dbush@db-centos tmp]$ echo $? 3 [ dbush@db-centos tmp]$
Now I will compile the same code on a computer running Windows 7 in Visual Studio 2010:
cl x1.c
If I run this and then echo %ERRORLEVEL% to print the return code, I get the following:
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, and in the other case, it returns 0.
Now let's play with optimization. I compiled the same code on the same CentOS machine with the same gcc version, but with optimization enabled:
gcc -Wall -Wextra -g -o /tmp/x1 /tmp/x1.c -O3
Then I ran this twice. Then I 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]$
Thus, I not only get different results when working with different optimization settings, but I get different results that run the same program several times . This can happen with undefined behavior.
So, to answer your question “what value will be returned”, the answer depends on whether you called undefined behavior. You cannot reliably predict what will happen.
EDIT:
Additional examples with updated code.
In gcc without optimization:
[ dbush@db-centos tmp]$ /tmp/x1 mytest2 = 3 [ dbush@db-centos tmp]$ echo $? 12
In gcc with -O3 optimization:
[ 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
In MSVC without optimization:
C:\Users\dbush\Documents>x1 mytest2 = 3 C:\Users\dbush\Documents>echo %ERRORLEVEL% 0 C:\Users\dbush\Documents>
In 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.