-1

What will happen if we don't use it? will the compiler automatically add it like it does in C++?

U. Watt
  • 279
  • 2
  • 14
  • @FredLarson, I don't think that this is the correct duplicate. Thie one there asks about the return *type*. This one here asks if the `return` statement is really necesary. – Jens Gustedt Oct 27 '17 at 20:53
  • 1
    @JensGustedt: It's thoroughly covered in the answers. For example, "It's also worth noting that in C++, int main() can be left without a return value at which point it defaults to returning 0. This is also true with a C99 program." – Fred Larson Oct 27 '17 at 20:55
  • @FredLarson, a better duplicate would have been https://stackoverflow.com/questions/4138649/why-is-return-0-optional, but I was not fast enough you guys closed it already. – Jens Gustedt Oct 27 '17 at 21:00
  • Perfectly valid question. – Patrick Sturm Oct 27 '17 at 21:21
  • 1
    @CinderBiscuits: That's usually a *bad* idea where C is concerned; there are too many ways to write code that *looks* like it works correctly, but contains all kinds of nasty time bombs waiting for the right moment to blow up. – John Bode Oct 27 '17 at 21:21

1 Answers1

3

It will return 0 automatically in C99-compliant compilers (and later). In pre-C99 compilers the return value is undefined in such cases.

In other words, in modern C you don't need an explicit return 0 at the end of main.

AnT
  • 291,388
  • 39
  • 487
  • 734
  • 1
    While this is technically true, enough compilers still default to C89 mode, or just plain don't implement C99 at all, that I think omitting the `return 0` in C is still a bad idea. – zwol Oct 27 '17 at 21:27