C89 vs c99 GCC compiler

user69514 picture user69514 · Feb 16, 2010 · Viewed 24.7k times · Source

Is there a difference if I compile the following program using c89 vs c99? I get the same output. Is there really a difference between the two?

#include <stdio.h>

    int main ()
    {
      // Print string to screen.
      printf ("Hello World\n");
    }

gcc -o helloworld -std=c99 helloworld.c 
vs
gcc -o helloworld -std=c89 helloworld.c 

Answer

Alok Singhal picture Alok Singhal · Feb 16, 2010
  • // comments are not a part of C89 but are OK in C99,
  • falling off of main() without returning any value is equivalent to return 0; in C99, but not so in C89. From N1256 (pdf), 5.1.2.2.3p1:

    If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

So your code has undefined behavior in C89, and well-defined behavior in C99.