943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6935
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 18th, 2007
0

why return an int from main?

Expand Post »
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?
Similar Threads
Reputation Points: 160
Solved Threads: 3
Posting Whiz in Training
quintoncoert is offline Offline
266 posts
since May 2007
Jul 18th, 2007
0

Re: why return an int from main?

Last edited by Bench; Jul 18th, 2007 at 7:16 am.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jul 18th, 2007
-1

Re: why return an int from main?

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2007
0

Re: why return an int from main?

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...?
Reputation Points: 86
Solved Threads: 2
Junior Poster
venomlash is offline Offline
143 posts
since Oct 2006
Jul 19th, 2007
0

Re: why return an int from main?

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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jul 20th, 2007
0

Re: why return an int from main?

Click to Expand / Collapse  Quote originally posted by venomlash ...
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?
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jul 20th, 2007
0

Re: why return an int from main?

You're still wrong even if your only way out is via exit()
http://c-faq.com/ansi/voidmain3.html
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 20th, 2007
0

Re: why return an int from main?

Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 20th, 2007
0

Re: why return an int from main?

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.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Jul 23rd, 2007
0

Re: why return an int from main?

Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)
C++ Syntax (Toggle Plain Text)
  1. char* main()
  2. {
  3. char* ptr;
  4. return ptr;
  5. }
I tried it on VC5 and it failed?
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,520 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Plz help me with the Errors Pasted in the Thread.
Next Thread in C++ Forum Timeline: HDC's





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC