Say you have a C code like this:
#include <stdio.h>
int main(){
printf("Hello, world!\n");
printf("%d\n", f());
}
int f(){
}
It compiles fine with gcc, and the output (on my system) is:
Hello, world!
14
But.. but.. how is that possible? I thought that C won’t let you compile something like that because f() doesn’t have a return statement returning an integer. Why is that allowed? Is it a C feature or compiler omission, and where did 14 come from?
The return value in this case, depending on the exact platform, will likely be whatever random value happened to be left in the return register (e.g.
EAXon x86) at the assembly level. Not explicitly returning a value is allowed, but gives an undefined value.In this case, the 14 is the return value from
printf.