Or is that one of the things that C++ does not support for C?
Asked
Active
Viewed 445 times
-2
-
1C++ is not fully backwards compatible with C. Also, you should probably specify which C standard allows `void main()`. – juanchopanza Jul 08 '13 at 17:10
-
2`void main` is illegal in C, too. – chris Jul 08 '13 at 17:11
-
1Also read this http://stackoverflow.com/questions/3027177/what-are-the-differences-between-c-and-c – banarun Jul 08 '13 at 17:11
-
3To prove that C++ is **not** backwards compatible with C, here is valid C but **not** C++: `int new =5;` `char class, public; public = class;` – abelenky Jul 08 '13 at 17:15
3 Answers
11
C++ is not backwards compatible with C.
C++ looks like a superset of C, but it actually isn't.

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
3To say the same thing differently: A C++ compiler will compile MANY valid C programs without complaint. It's NOT intended or guaranteed that it will compile ALL valid C programs. – Michael Kohne Jul 08 '13 at 17:14
5
void main
is illegal in C. I don't have access here to my
copy of C90 (on paper), but C99 clearly says (concerning main
)
"It shall be defined with a return type of int[...]".
This has been, in fact, the case since the first edition of Kernighan and Richie.

James Kanze
- 150,581
- 18
- 184
- 329
-
5.1.2.2.1 Program Startup: "The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: [...] or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): [...] or equivalent;9) or in some other implementation-defined manner." – John Dibling Jul 08 '13 at 18:11
-
0
void main()
is not illegal. It's non-standard. However, a number of beginners' books on C have used void main(void) in all of their examples.
int main()
is the proper definition of main as per the C++ spec.
Bjarne Stroustrups made this quite clear:
"The definition void main() is not and never has been C++, nor has it even been C."

Vivek Sadh
- 4,230
- 3
- 32
- 49
-
An implementation *may* support
void main()
*in addition* to the standardint main(void)
andint main(int, char**)
signatures ("or in some other implementation-defined manner"), but it must document that additional signature. If it's not documented, then the behavior is undefined. – John Bode Jul 08 '13 at 19:36 -
@JohnDibling It requires a diagnostic from the compiler (at least in C++). – James Kanze Jul 09 '13 at 08:19
-
@JohnBode Where do you find that? The return type must be `int` _and_ (one of a number of other possibilities). In the case of C++, violations of a constraint must be diagnosed unless there is a specific statement otherwise, so a diagnostic is required. This seems to be the case in C99 as well, although I seem to recall that no diagnostic was required in earlier versions of the C standard. – James Kanze Jul 09 '13 at 08:26
-
@JamesKanze: 5.1.2.2.1/1, "; or in some other implementation-defined manner". – John Bode Jul 09 '13 at 11:08
-
1@JohnBode So we get "The return type must be `int` and [...] in some other implementation-defined manner." You're parsing the sentence wrong: it's "The return type must be `int` and **(** [...] or [...] or in some other implementation-defined manner **)**." The C++ standard was originally worded the same way, but was corrected to eliminate any ambiguity (after discussion with some of the members of the C committee, to be sure that the new wording corresponded to the original intent). – James Kanze Jul 09 '13 at 11:37