cexit-code

What does the OS do when the EXIT code of a program is not given. C code example


so I have this C code that compiles (without warning flags enabled):


#include <stdio.h>

int main()
{
  printf("output\n");

}

I was wondering what the OS would interpret by this? Would the status code be an arbitrary value, just like variables that have not been initialized, or is it equivalent to 1 ?

I was expecting the compiler to throw an error and avoid to compile this program


Solution

  • As an exception to the normal rules, the C standard, ever since its 1999 revision, says that if you "fall off the end" of main — leave the function without executing a return statement — that's equivalent to executing return 0. So the exit status is well-defined in this case, and your code is not considered incorrect.

    Many people (including myself) consider this a bad feature of the language, both because it confuses people like you who didn't expect it, and because it encourages bad habits like writing void main and not returning a meaningful exit status. But we're stuck with it.