Martin Drozdik Martin Drozdik - 1 year ago 95
C++ Question

Can you omit the return type in main function?

Are there some special rules with respect to the declaration of the


According to ideone this is legal C++:

main() // As opposed to int main()
return 0;

On the other hand, normal functions do not seem to have the privilege to avoid the return type:


int main()
return 0;

Gives the error:

prog.cpp:1:3: error: ISO C++ forbids declaration of 'f' with no type
[-fpermissive] f(){} ^

Is the
function special in this case?

Answer Source

Never omit main's return type, as it's non-standard!

wandbox example: warning: ISO C++ forbids declaration of 'main' with no type [-Wpedantic] main()

From $3.6.1:

1 A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: In a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. — end note ] § 3.6.1 58

2 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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download