This post has been edited by Zeddicus: 14 July 2008 - 06:19 AM
return 0 vs return EXIT_SUCCESSFiguring out the proper return value in the end of a function
Page 1 of 1
7 Replies - 36001 Views - Last Post: 15 July 2008 - 12:40 AM
#1
return 0 vs return EXIT_SUCCESS
Posted 14 July 2008 - 06:17 AM
I am beginner in C++ and I have tried to find answers on the difference between return 0 and return EXIT_SUCCESS. When should you use the one or the other? Can someone please help me and explain this to me in basic way (code examples are much appreciated)?
Replies To: return 0 vs return EXIT_SUCCESS
#2
Re: return 0 vs return EXIT_SUCCESS
Posted 14 July 2008 - 06:27 AM
First off, I'm assuming that you know that main() should always return int.
Secondly, when we return a value from main, we are basically saying "the program was executed successfully!"
However: most operating systems will regard return 0; as a successful run, but some will be looking for return 1; to say "this program was executed successfully!"
Now, EXIT_SUCCESS; is a C++ macro, which will just make sure that it returns the correct value, always saying "this program was executed successfully!"
Hope this helps
Secondly, when we return a value from main, we are basically saying "the program was executed successfully!"
However: most operating systems will regard return 0; as a successful run, but some will be looking for return 1; to say "this program was executed successfully!"
Now, EXIT_SUCCESS; is a C++ macro, which will just make sure that it returns the correct value, always saying "this program was executed successfully!"
Hope this helps
#3
Re: return 0 vs return EXIT_SUCCESS
Posted 14 July 2008 - 06:34 AM
The EXIT_SUCCESS is for clarity. Because, you know, return 0 is so meaningful. 
Somewhere in a header file, stdlib I think, you'll find a line like this:
Meaning, when the thing compiles, EXIT_SUCCESS will be replaced with 0. To the compiler it makes no difference. The point is to help the humans reading the code.
Essentially, meaningful names make code easier to read.
EDIT: Gabe's fast.
Somewhere in a header file, stdlib I think, you'll find a line like this:
#define EXIT_SUCCESS 0
Meaning, when the thing compiles, EXIT_SUCCESS will be replaced with 0. To the compiler it makes no difference. The point is to help the humans reading the code.
Essentially, meaningful names make code easier to read.
EDIT: Gabe's fast.
This post has been edited by baavgai: 14 July 2008 - 06:35 AM
#4
Re: return 0 vs return EXIT_SUCCESS
Posted 14 July 2008 - 06:42 AM
Thank you so much for your fast reply, gabehabe. It became a bit clearer to me.
But is return EXIT_SUCCESS then only used within the main function whereas return 0 is used in other contexts of the program?
But is return EXIT_SUCCESS then only used within the main function whereas return 0 is used in other contexts of the program?
#5
Re: return 0 vs return EXIT_SUCCESS
Posted 14 July 2008 - 06:48 AM
EXIT_SUCCESS is used wherever the program is supposed to exit.
Don't get confused with return values though, you return stuff from other functions, I'm not sure of your level, so I don't know if you know about functions yet, but basically, main is a function, and it also acts as the entry point to the program (or at least for console applications, you'll learn more as you progress into the programming world)
But basically, return tells a the program to return back to the function from which this function was called. Damn, that sounds retarded.
But, simply, it's this:
When you return from a function, you are exiting a function. Because there is nothing for main to return to, so it exits the program.
Hopefully that's clarified things a little
Don't get confused with return values though, you return stuff from other functions, I'm not sure of your level, so I don't know if you know about functions yet, but basically, main is a function, and it also acts as the entry point to the program (or at least for console applications, you'll learn more as you progress into the programming world)
But basically, return tells a the program to return back to the function from which this function was called. Damn, that sounds retarded.
But, simply, it's this:
When you return from a function, you are exiting a function. Because there is nothing for main to return to, so it exits the program.
Hopefully that's clarified things a little
#6
Re: return 0 vs return EXIT_SUCCESS
Posted 14 July 2008 - 06:56 AM
baavgai: you confirmed one of my suspicions on why to use EXIT_SUCCESS.
If you add this to what gabehabe wrote about some operating systems not expecting 0 in this part of the program, then you have both computer- and human-oriented reasons for using EXIT_SUCCESS instead of 0.
If you add this to what gabehabe wrote about some operating systems not expecting 0 in this part of the program, then you have both computer- and human-oriented reasons for using EXIT_SUCCESS instead of 0.
#7
Re: return 0 vs return EXIT_SUCCESS
Posted 14 July 2008 - 06:04 PM
Zeddicus, on 14 Jul, 2008 - 06:56 AM, said:
If you add this to what gabehabe wrote about some operating systems not expecting 0 in this part of the program, then you have both computer- and human-oriented reasons for using EXIT_SUCCESS instead of 0.
I don't think most operating systems care too much about the return value... It's really something that is interpreted by user programs. For example, the return code is often evaluated in shell scripts. The operating system passes it back to the executing program, but I don't think there usually is any interpretation done at that level. In Bourne-like shells, the return code is generally placed in the $? variable and can be evaluated to choose a path of execution. Similarly, the %ERRORLEVEL% variable for cmd.exe/command.com is used for this purpose.
#8
Re: return 0 vs return EXIT_SUCCESS
Posted 15 July 2008 - 12:40 AM
perfectly.insane
Thanks for your information. I just wonder what a Bourne-like shell is.
Thanks for your information. I just wonder what a Bourne-like shell is.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote










|