10

Stephen Prata in his book C++ Primer Plus [p 31] says :

Many existing programs use the classic C function header instead:

main() // original C style

Under classic C, omitting the return type is the same as saying that the function is type int. However, C++ has phased out that usage.

However The C++11 draft 3.6.1->2 says

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.

Test Result

$ g++ -Werror=pedantic MainCheck.cpp -o MainCheck
MainCheck.cpp:3:6: error: ISO C++ forbids declaration of ‘main’ with no type [-Werror=pedantic]
 main()
$ # also means g++ don't conform to the standard

confirms that what Mr. Prata said is true when it comes to C++ standard.

Is there a clause in the C++11 draft that discourages the use of :

main() // that is without return type.

Is

It shall have a return type of type int

itself such a clause?

sjsam
  • 19,686
  • 3
  • 43
  • 88
  • @JonathanLeffler : I would assume that many implementation didn't follow requirement to maintain compatibility with Old C Programs. – sjsam Jun 17 '16 at 05:54
  • 2
    `main() {}` is banned by the rules governing declarations. See [\[dcl.type\]/3](http://eel.is/c++draft/dcl.spec#dcl.type-3) and [\[dcl.dcl\]/11](http://eel.is/c++draft/dcl.dcl#11). Similar wording can be found in every published standard. – T.C. Jun 17 '16 at 05:56
  • 3
    ISO/IEC 14882:1998 contains: §7 Declarations ¶7 _Only in function declarations for constructors, destructors, and type conversions can the_ decl-specifier-seq _be omitted.78)_ — and footnote 78 says _The “implicit int” rule of C is no longer supported._ So, `main()` declared without the return type has never been a part of standard C++, but it was allowed roughly up until C++98 standard was created (and probably a bit longer for reasons of backwards compatibility). – Jonathan Leffler Jun 17 '16 at 06:03
  • @JonathanLeffler : Thanks. It is clear now.. – sjsam Jun 17 '16 at 06:04

4 Answers4

22

See also What should main() return in C and C++?

ISO/IEC 14882:1998 contains:

§7 Declarations

¶7 Only in function declarations for constructors, destructors, and type conversions can the decl-specifier-seq be omitted.78)

and footnote 78 says:

The “implicit int” rule of C is no longer supported.

The same statements are in ¶9 and footnote 89 in the C++11 standard.

So, main() declared without the return type has never been a part of standard C++, but it was allowed roughly up until C++98 standard was created (and probably a bit longer for reasons of backwards compatibility).

If you look in Stroustrup's "Design and Evolution of C++" (published 1994), §2.8 The C Declaration Syntax says:

Allowing the type specifier to be omitted (meaning int by default) also led to complications. … The negative reaction to changes in this area from users was very strong. … I backed out the change. I don't think I had a choice. Allowing that implicit int is the source of many of the annoying problems with the C++ grammar today. … Finally, ten years later, the C++ ANSI/ISO standard committee has decided to deprecate implicit int. That means we may get rid of it in another decade or so.

Community
  • 1
  • 1
Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
8

Implicit int types were disallowed in all C++ standards, i.e., it wasn't allowed in C++98. It isn't anything new in C++11. There wasn't any exception for main() with respect to the declaration of the function. Also, the implicit int rule applied to all declarations not just to main().

The relevant clause in the C++ standard is 7 [dcl.dcl] paragraph 11:

Only in function declarations for constructors, destructors, and type conversions can the decl-specifier-seq be omitted.94

94) The “implicit int” rule of C is no longer supported.

I don't have easy access to the C++98 standard right now but C++03 definitely has the same statement. The only difference is that it is in paragraph 7 and the footnote is "79" instead of "94".

C's implicit int rule was part of C++ for a while prior to the first standard but got removed at some point. Compilers may accept programs omitting the type as an extension but I think they are required to emit a diagnostic. Whether that means much is a separate question as it is long established that writing a single \r at the start of a line would meet that requirement.

What is there and what will remain is the odd exception that program execution can flow off the end of main() without a return statement: it is assumed that mandating a return statement in main() would break existing code.

The statement

It shall have a return type of type int

doesn't state anything about how the function acquires its return type. It only states what the return type shall be.

Dietmar Kühl
  • 141,209
  • 12
  • 196
  • 356
3

This is what the working draft says about the main function.
Excerpts follow.

An implementation shall not predefine the main function. This function shall not be overloaded. Its type shall have C++ language linkage and it shall have a declared return type of type int, but otherwise its type is implementation-defined. An implementation shall allow both

  • a function of () returning int and
  • a function of (int, pointer to pointer to char) returning int

as the type of main

A return statement in main has the effect of leaving the main function [...] If control flows off the end of the compound-statement of main, the effect is equivalent to a return with operand 0

So, the standard says that the main function shall have one of the following types:

  • int()

  • int(int, char **)

Being the main function a function, I guess it follows also the rules in 8.3.5.
The question would become thus: can I omit the return type from a function definition? Does the standard allow me to do that?
The answer is no, indeed.

Community
  • 1
  • 1
skypjack
  • 45,296
  • 16
  • 80
  • 161
  • This is the only answer, realy answering the question. None of the other answers addresses the request for a proof in the standard. – exilit Jun 17 '16 at 06:16
  • 2
    @exilit: sadly, it doesn't address the question, though: the question is about the _declaration_ and **not** about the `return` statement, at least as written. – Dietmar Kühl Jun 17 '16 at 06:21
  • 1
    @exilit Except the quotes from the standard here don't actually answer the question. The question is about the implicit `int` return type. – juanchopanza Jun 17 '16 at 06:21
  • 1
    @DietmarKühl And this answer is not about the `return` statement as well. It is about the return type. Which definetly is part of the declaration, no? – exilit Jun 17 '16 at 06:26
  • @DietmarKühl Actually, the answer is about both the return statement and the return type. Anyway, thanks for the comment. I guess the accepted answer is far better and I voted it up. :-) – skypjack Jun 17 '16 at 06:27
  • @exilit There's nothing in the quotes in this answer that states that there is no implicit `int` return type, which was the point of the question. – juanchopanza Jun 17 '16 at 06:30
  • 1
    @juanchopanza as skypjack said the accepted answer is much clearer. At time commenting this the other answer did not exist and I did not expected such a precise statement to exist in the standard. But nevertheless this answer contains two citations plus a third reference which the answerer used to conclude his statement. – exilit Jun 17 '16 at 06:38
  • @exilit There was at least one better answer when you posted your comment (otherwise I wouldn't have commented myself.) Plus I don't see how a number of largely irrelevant quotes from the standard "addresses the request for a proof in the standard". – juanchopanza Jun 17 '16 at 08:20
2

It is not required by the standard for the main to explicitly return. If return statement is missing, compiler simply puts:

return 0;

at the end. This rule doesn't apply to other functions.

In C, default return type of any function was int if not provided by programmer, for example:

get() // int assumed
{
    return 10;
}

C++ standard requires that you must specify the return type. Above code will not compile in C++ compiler. C90 allowed implicit int, C99 disallows implicit int.

Ajay
  • 16,823
  • 9
  • 50
  • 94
  • By `By C++ doesn't allow it ` did you mean the standard doesn't allow it. Some implementation, say `g++` do allow it.. – sjsam Jun 17 '16 at 05:43
  • Yes, C++ standard requires explicit type to be given. – Ajay Jun 17 '16 at 05:44
  • 2
    I think saying C++ doesn't allow it is clear enough, no one refers to any specific implementation of the C++ standard when they say "C++" –  Jun 17 '16 at 05:46
  • @xboi209 : But it is better to say which standard. Saying `C++` is not clear enough. – sjsam Jun 17 '16 at 05:55
  • Actually, 'standard C++' is clear enough; it means C++98, C++03, C++11, C++14 all disallow it (and there's no reason to think any future version of C++ will allow it). – Jonathan Leffler Jun 17 '16 at 06:29