return 0 vs return EXIT_SUCCESS

Figuring out the proper return value in the end of a function

Page 1 of 1

7 Replies - 41061 Views - Last Post: 15 July 2008 - 12:40 AM Rate Topic: -----

#1 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

return 0 vs return EXIT_SUCCESS

Post icon  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)?

This post has been edited by Zeddicus: 14 July 2008 - 06:19 AM

Is This A Good Question/Topic? 0
  • +

Replies To: return 0 vs return EXIT_SUCCESS

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

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 :)
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5757
  • View blog
  • Posts: 12,571
  • Joined: 16-October 07

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:
#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. :P

This post has been edited by baavgai: 14 July 2008 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

#4 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

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. :D

But is return EXIT_SUCCESS then only used within the main function whereas return 0 is used in other contexts of the program?
Was This Post Helpful? 0
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

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 :)
Was This Post Helpful? 1

#6 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#7 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: return 0 vs return EXIT_SUCCESS

Posted 14 July 2008 - 06:04 PM

View PostZeddicus, 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.
Was This Post Helpful? 0
  • +
  • -

#8 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1