1

I'm wondering why gcc doesn't display an error while compiling, even while using the -Wall option. Also tried the -std=c89 option.

Here's the code:

#include <stdio.h>

int main(void)
{
   printf("hello, world\n");
}

Sorry if this post is a duplicate but couldn't find a case where this option is being used.

Thanks!

jjta
  • 11
  • 1
  • despite that return type of `main` must be an `int`, the return statement of `main` and only `main` can be opted out, `return 0;` is implied – Danh Sep 18 '16 at 01:11
  • 1
    `main` is special, and you don't need a return statement in it. – Kerrek SB Sep 18 '16 at 01:15

1 Answers1

1

In C++ and C99 and later, hitting the end of main() without returning a value is equivalent to returning 0. In earlier versions of the C standard it's undefined behavior which the compiler isn't obliged to issue a diagnostic for, although in practice GCC's likely doing an implicit return 0; just as it would for C++ or C99 onwards.

See What should main() return in C and C++? for a lot more detail.

Community
  • 1
  • 1
Todd Knarr
  • 1,210
  • 1
  • 7
  • 14