why doesn`t void main() work in c++ 14

Oct 21, 2015 at 4:20pm
why does my compiler(c++14) says me that main() must return int?

1
2
3
4
5
6
  #include<iostream>
using namespace std;
void main()
{
int a;
cout<<a;


I tried it on earlier versions and it just worked fine..Please help me on this..
http://ideone.com/bO0yUR
Copy and paste the above link...
Is it some bug in the website or is there any actual upgrade or something?
Oct 21, 2015 at 4:22pm
void main has never been valid C++. main has always been required to return int.

http://www.stroustrup.com/bs_faq2.html#void-main
Last edited on Oct 21, 2015 at 10:10pm
Oct 21, 2015 at 5:07pm
Actually, void main has always been permissible...

C++14 fixes the loophole by declaring that main() must return int.

http://www.cplusplus.com/forum/beginner/174726/

[edited] Whoops! I mixed C and C++. See JLBorges's answer.
Last edited on Oct 21, 2015 at 9:11pm
Oct 21, 2015 at 6:04pm
void main() { /* ... */ ) has never been permissible in standard C++ (hosted).
C++98 specified that main() must have the return type int.
This part of the standard has been retained unchanged in subsequent revisions.

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. ...

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. ...


http://coliru.stacked-crooked.com/a/93e7a7047b438ce4
Last edited on Oct 21, 2015 at 6:08pm
Oct 21, 2015 at 9:12pm
Sorry, C has a defect in the standard making void main technically permissible (though still foolhardy).

C++ has always mandated int main().
Oct 23, 2015 at 12:34pm
so are you guys trying to say that int main() is just convention and does not have logical reason as to why int is returned..
Last edited on Oct 23, 2015 at 12:35pm
Oct 23, 2015 at 12:38pm
to lb..
in the older versions of c++ (such as c++98) it works fine.. we have our own choices and why should we use conventions and why is main() given special treatment..
Oct 23, 2015 at 12:48pm
so are you guys trying to say that int main() is just convention

When they're saying it's mandated by the standard, that means that it is not convention.


in the older versions of c++ (such as c++98) it works fine..

Crossing the road without looking works fine until you get hit.
Oct 23, 2015 at 1:13pm
closed account (48T7M4Gy)
http://www.stroustrup.com/bs_faq2.html#void-main

Can I write "void main()"?
The definition

void main() { /* ... */ }

is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by main() is a way for a program to return a value to "the system" that invokes it. On systems that doesn't provide such a facility the return value is ignored, but that doesn't make "void main()" legal C++ or legal C. Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.

In C++, main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:

#include<iostream>

int main()
{
std::cout << "This program returns the integer value 0\n";
}

Note also that neither ISO C++ nor C99 allows you to leave the type out of a declaration. That is, in contrast to C89 and ARM C++ ,"int" is not assumed where a type is missing in a declaration. Consequently:

#include<iostream>

main() { /* ... */ }

is an error because the return type of main() is missing.
Oct 24, 2015 at 9:30am
thank you people for your support,i got what you guys are trying to say...
vaidhyesh
Topic archived. No new replies allowed.