here is my code in linux:
#include <stdio.h>
static int tt(void);
static int tt(void)
{
return 1;
}
int main(int charc,char **charv)
{
tt();
}
in the shell:
$./a.out
$echo %?
$1
Why i got the "1" as the result
here is my code in linux:
#include <stdio.h>
static int tt(void);
static int tt(void)
{
return 1;
}
int main(int charc,char **charv)
{
tt();
}
in the shell:
$./a.out
$echo %?
$1
Why i got the "1" as the result
Returning from a non-void function without returning a value is undefined behavior. You can't depend on the result.
There is a special case for the main
function starting with the C99 standard. If no value is returned from main
, a return value of 0 is assumed. However, you appear to be compiling in C89 mode (which is the default for gcc) where this is not allowed.
If I compile this code as C89, I get a warning about not returning a value. To demonstrate undefined behavior, if I compile without optimizations the exit status is 1, but if I compile with -O3
the exit status is 96.
If I compile in C99 mode I get no warning and the exit status of the program is 0.
To compile in C99 mode, pass the flag -std=c99
to gcc.