0

I am using DevC++.

  • when I write void main(), the compiler says main must return int.
  • when I write main(), the compiler reports nothing.
  • when write int main(), the compiler asks me to return something.

My question is what is main() without int and void?why does compiler behave differently for int main() and just main()?

farheen
  • 111
  • 2
  • 3
  • 13
  • Are you targeting C, or C++ here? (The answer is language-dependent). – Bathsheba Apr 15 '15 at 13:29
  • @Bathsheba I use DevC++ and write both C and C++ programs in it and this happens regardless of the C/C++ program. – farheen Apr 15 '15 at 13:32
  • my question is totally different from the question that has been asked and answered before.I am not asking the difference between void main() and int main() .I am asking what do just main() means to the compiler. – farheen Apr 15 '15 at 13:35
  • The C++ standard says _"If control reaches the end of `main` without encountering a return statement, the effect is that of executing `return 0`;"_ so _"when write `int main()`, the compiler asks me to return something"_ sounds odd if you're really using a C++ compiler. – Michael Apr 15 '15 at 13:35
  • 1
    @Michael It does.This is what I am asking why compiler behaves differently for int main() and main()? – farheen Apr 15 '15 at 13:36

1 Answers1

3

In C, it's a C89 feature made obsolete in C99 and later: if the return type is omitted, int is implied. In C++, it is a compiler extension not supported by the language but supported by some compilers.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • If int is implied then why it does not ask me to write a return statement? – farheen Apr 15 '15 at 13:30
  • The omitted return type and variable type is assumed to be `int` since [K&R](https://en.wikipedia.org/wiki/C_%28programming_language%29#K.26R_C), back in the 70s. – axiac Apr 15 '15 at 13:33
  • @fahreen in C, the return statement in `main` is never mandatory. If omitted, in C99, `return 0` is implied and in C89 an undefined termination status is returned. C++ is like C89 in that matter IIRC. – ouah Apr 15 '15 at 13:37
  • @ouah but when I write int main() then compiler asks me to write return statement.Compiler behaves differently for int main() and just main().Is this a problem with compiler? – farheen Apr 15 '15 at 13:42