I just compiled my hello world c program with gcc and ran it in ubuntu. Since I ran it through emacs, I got the exit code of the program: 13. Why 13? I didn't specify anything, so why didn't it default to 0? When I put an exit function at the end, I could change it, but I'm wondering what the significance of this default is.
Code:
#include<stdio.h>
int main()
{
printf("Hello, world!");
}
As of C99, reaching the end of main
without a return is the same as if you'd returned zero (only main
, not all functions in general). Before C99 (and I believe gcc
defaults to C89/90 as a baseline), it was not defined what would happen, so you should be explicitly returning zero if that's what you need.
Or you could adopt C99/C11 by compiling with -std=c99
or the c11
one.
In terms of why 13, it's neither relevant nor portable but it's likely that the return code is whatever happens to be in the eax register (or equivalent if you're using a different calling convention or architecture). For x86, that would probably still be the value that was returned from printf
, which returns the number of characters printed.