why return an int from main?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2007
Posts: 266
Reputation: quintoncoert is an unknown quantity at this point 
Solved Threads: 3
quintoncoert quintoncoert is offline Offline
Posting Whiz in Training

why return an int from main?

 
0
  #1
Jul 18th, 2007
i hope that this does not sound like a silly question but can someone please tell me why we return an integer from the main method in c++?

if program execution starts and ends in main then what is the point? main calls other mothods and receive return types so why does main have to return something itself?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 492
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: why return an int from main?

 
0
  #2
Jul 18th, 2007
Last edited by Bench; Jul 18th, 2007 at 7:16 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: why return an int from main?

 
0
  #3
Jul 18th, 2007
Historically,
- C++ was derived from C.
- In C, everything was an int unless specified otherwise (there was no void), so it was natural for main to return int.
- C programs were originally developed for command line use, and using a command line interpreter which was far more capable 30 years ago than M$ cmd.exe is even today. That command interpreter could easily determine whether the program just ran was a success (or not) from the value returned by main.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 143
Reputation: venomlash is an unknown quantity at this point 
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

Re: why return an int from main?

 
0
  #4
Jul 18th, 2007
I always learned that main returns an int so that you can put exit(0), exit(1) and other constructions in to tell you why the program quit. Or would that not work...?
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: why return an int from main?

 
0
  #5
Jul 19th, 2007
yup. The code is returned to the operating system and can be polled from other programs (like shellscripts) to determine the reason why the program terminated (with 0 traditionally indicating that everything worked smoothly).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: why return an int from main?

 
0
  #6
Jul 20th, 2007
Originally Posted by venomlash View Post
I always learned that main returns an int so that you can put exit(0), exit(1) and other constructions in to tell you why the program quit. Or would that not work...?
Interesting question, to rephrase:
If i have a void main() and in the code I write exit(11). When I call this program from command line what will be return code of it?
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: why return an int from main?

 
0
  #7
Jul 20th, 2007
You're still wrong even if your only way out is via exit()
http://c-faq.com/ansi/voidmain3.html
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,582
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: why return an int from main?

 
0
  #8
Jul 20th, 2007
Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)
  1. char* main()
  2. {
  3. char* ptr;
  4. return ptr;
  5. }

But since pointers are really integers the program will actually return the integer value of that pointer. Meaningless -- absolutely. But I've seen someone try to do it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: why return an int from main?

 
0
  #9
Jul 20th, 2007
Originally Posted by quintoncoert View Post
if program execution starts and ends in main then what is the point? main calls other mothods and receive return types so why does main have to return something itself?
Like others have said, main's return value is used to determine if the program ran successfully or what the errors were. As to why the mechanism is a return value, when your program runs, something in your OS is calling main. When your program finishes, main returns an int to the OS function which called it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,253
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 540
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator

Re: why return an int from main?

 
0
  #10
Jul 23rd, 2007
Originally Posted by Ancient Dragon View Post
Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)
  1. char* main()
  2. {
  3. char* ptr;
  4. return ptr;
  5. }
I tried it on VC5 and it failed?
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC