1
#include<iostream>

using namespace std;

int main()
{
    cout << "hello world";
}

Shouldn't I get a compiler warning? return 0; does not exist.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Does this answer your question? [g++ How to get warning on ignoring function return value](https://stackoverflow.com/questions/2870529/g-how-to-get-warning-on-ignoring-function-return-value) – max Nov 08 '20 at 16:03
  • 4
    No. `main` is the exception, it returns 0 implicitly. – 273K Nov 08 '20 at 16:04
  • ^^. Some compilers nevertheless *do* warn if control can reach the closing brace of `main()`, but such programs do not for that reason fail to conform to the standard, and their behavior is not impacted (unless `main()` is called recursively and its return value used by the caller). – John Bollinger Nov 08 '20 at 16:09
  • But also, beware of assuming that your compiler will or even *can* warn you about all potential issues with your code. They can't and don't. They are not even required to diagnose all of the issues they are able to recognize. What warnings you get and how you get them is implementation specific. – John Bollinger Nov 08 '20 at 16:11
  • @JohnBollinger -- calling `main` recursively is not allowed in C++, regardless of what you do with the return value. – Pete Becker Nov 08 '20 at 19:27

3 Answers3

2

main is special, this is specially allowed by the standard and the function is equivalent to having "return 0;" at the end. main is the only such function that has an assumed return value like this.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
0

http://eel.is/c++draft/basic.start.main#5

If control flows off the end of the compound-statement of main, the effect is equivalent to a return with operand 0 (see also [except.handle]).

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
0

I think this also depends on the IDE you’re using. I found that using codeblocks gives a warning while things work normally with Virtual Studio