473,379 Members | 1,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

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

Jul 23 '05 #1
4 4029
"Doomster" <el*********@netgate.net> wrote in message
news:dc******************************@localhost.ta lkaboutprogramming.com...
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

Jul 23 '05 #2
Ali Çehreli wrote:
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.
Jul 23 '05 #3
Ali Çehreli wrote:
"Doomster" <el*********@netgate.net> wrote in message

news:dc******************************@localhost.ta lkaboutprogramming.com...
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...


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

Jul 23 '05 #4
>
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..

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sibylle Koczian | last post by:
Still trying to learn PyQt from a book about several Python GUI toolkits, I seem to learn first what doesn't work. The following small script seems to work, but after closing the window I get the...
192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
27
by: poiuz24 | last post by:
aeh, this may be stupid, but could s.b. please explain to me why the following won't link (gcc 3.3)? foo.cpp = """ namespace foo { int main () { return 0; }
5
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be...
28
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
27
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
17
by: mike-yue | last post by:
and, Is it possible to call one main function from another main function? Thanks guys.
6
by: chang | last post by:
Hi ALL, I am working in C from past few months. Still now i can't figure out who is called main() in 'C' programme? Main() is a function from that we can call our sunroutines but someone has to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.