| | |
why return an int from main?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2007
Posts: 266
Reputation:
Solved Threads: 3
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?
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?
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.
- 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.
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.
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.
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.
•
•
•
•
I always learned that main returns an int so that you can putexit(0),exit(1)and other constructions in to tell you why the program quit. Or would that not work...?
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.. ?
Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)
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.
C++ Syntax (Toggle Plain Text)
char* main() { char* ptr; return ptr; }
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.
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.
•
•
•
•
Some compilers will even let you declare main as returning a pointer! (VC++ 6.0 for example)
C++ Syntax (Toggle Plain Text)
char* main() { char* ptr; return ptr; }
If i am helpful, please give me reputation points.
![]() |
Similar Threads
- int main() or void main() ??!! (C++)
- if then statement (C++)
- Old School Compiler vs New Compiler (C++)
Other Threads in the C++ Forum
- Previous Thread: Plz help me with the Errors Pasted in the Thread.
- Next Thread: HDC's
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






