How is main() different in C vs C++?

G

Guest

Doomster said:
That was asked by someone and I don't know.

One difference is that in C, main can be called recursively from within the
program. Not so in C++...

Another difference is that, in C++, main could be exited without executing a
return statement. In C, there had to be a return statement. (I don't know
whether the "new C" has changed this.) i.e.:

int main() {}

would be legal program in C++; but it had to be written as

int main() { return 0; }

in C. Again, this difference might have disappeared...

Ali
 
R

Ron Natalie

Ali said:
Another difference is that, in C++, main could be exited without executing a
return statement. In C, there had to be a return statement. (I don't know
whether the "new C" has changed this.) i.e.:

int main() {}

would be legal program in C++; but it had to be written as

int main() { return 0; }

in C. Again, this difference might have disappeared...
Actually
int main() { }
was legal in both versions of the C standard was well. For the same
stupid reason that C++ allows falling off main. The thing that is
different in the two languages is that:

int main() { return; }

is legal in C but not in C++. What did change between the versions
was that returning nothing from C used to return an unspecified value
(which was pretty much born out in practice). Falling off the end
in the later version has the C++ implicit return 0 misfeature.
 
R

Rolf Magnus

Ali said:
news:dc9104c572e279fa82e8eb6d3e7ba73b@localhost.talkaboutprogramming.com...

One difference is that in C, main can be called recursively from within
the program. Not so in C++...

Another difference is that, in C++, main could be exited without executing
a return statement. In C, there had to be a return statement. (I don't
know whether the "new C" has changed this.) i.e.:

int main() {}

would be legal program in C++; but it had to be written as

int main() { return 0; }

in C. Again, this difference might have disappeared...

Another difference is that in C, main() is always the first thing to be
executed, while in C++, constructors of global objects are executed before
main().
 
G

Grant Schoep

Another difference is that in C, main() is always the first thing to
be executed, while in C++, constructors of global objects are executed
before main().

Ahh, the wonderful day of repeated cursing that I learned this. I
remeber.. "How the f%$&ing g%%$amn is it coring before hitting main!" The
answer was obvious if I payed attention to what my trusty dubugger was
telling me. But an irate programer intent on damaging his mouse and
keyboard sometimes don't listen to their helpful tools(alas, yes the
debugger is also my best friend...)


(yes I probably need counsling)

(and yes, I have mice that have had they buttons "torn off")


Hmm..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top