Reputation: 1361
I made a simple C program with empty main as given below and compiled it with gcc
void main(){
}
upon execution, it return exit code as 25
,
and if I add a printf statement,after including stdio.h, it returns exit code 5
what's the meaning of 25
and 5
here and why it's being returned by whatever code that is executed before or after main()?
exit code is zero if I use int main() and don't put a return statement.
Upvotes: 1
Views: 1303
Reputation: 18902
(Adding on John Bollinger's answer)
The old ISO C Standard (ISO/IEC 9899:1999) stated:
[main] shall be defined
- with a return type of
int
and
- with no parameters […] or
- with two parameters […] or equivalent; or
- in some other implementation-defined manner.
§ 5.1.2.2.1 ¶ 1 of the C Standard
If the return type is not compatible with
int
, the termination status returned to the host environment is unspecified.
§ 5.1.2.2.3 ¶ 1
which indicates that allowing forms that didn't return int
was intentional.
Many compiler's manuals (e.g. Watcom C/C++, IBM VisualAge C/C++, Microsoft Visual C/C++) stated that main
may have the return type void
so a program with void main()
was a conforming program.
For a long time much code was written with a return type of void
. gcc
(probably) consider important to be compatible with legacy code and allows void main()
but in that case:
warning: return type of ‘main’ is not ‘int’
);References:
int main() {}
This is undefined in C89/90 and well defined in following versions (where it returns 0
).
On x86 the EAX
register is normally used for return values. So
int main() {}
is compiled to something like:
main:
push rbp
mov rbp, rsp
mov eax, 0
pop rbp
ret
For
void main() {}
the simplest action is removing mov eax, 0
:
main:
push rbp
mov rbp, rsp
nop
pop rbp
ret
If you add a printf
statement:
#include <stdio.h>
void main()
{
printf("1234");
}
you get:
.LC0:
.string "1234"
main:
push rbp
mov rbp, rsp
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
pop rbp
ret
The printf
call alters the EAX
register (returns the number of characters written to the stream, and EAX
is used for the return value).
Upvotes: 5
Reputation: 180201
what's the meaning of 25 and 5 here
The C language specifications do not define behavior for a program whose main()
is declared to return void
. Implementations that nevertheless accept such code may or may not define behavior for it. In a general sense, then, no exit statuses have any meaning or significance for a program with void main()
.
Apparently, GCC in particular does accept the code, but I find no definition for its behavior among GCC's documented C extensions, so neither in that particular case do the exit statuses you observe have any meaning.
and why it's being returned by whatever code that is executed before or after main()?
No particular reason. The behavior is undefined.
exit code is zero if I use int main() and don't put a return statement.
That case is well defined. C specifies that if execution reaches the end of the initial call to main()
(presumed declared with one of the two C-standard signatures, both of which return int
) then the behavior is as if exit(0)
were called. This is a special case. The behavior is otherwise undefined if execution reaches the closing brace of a non-void
function, and its caller does anything with the return value.
Upvotes: 5