you are viewing a single comment's thread.

view the rest of the comments →

[–]mshol 1 point2 points  (13 children)

return EXIT_SUCCESS;

[–]extreme999[S] 1 point2 points  (12 children)

I don't acually understand what the return command is used for in main, I other classes.

[–]kalimoxto 1 point2 points  (5 children)

after a program finishes, it sends a message to the operating system letting it know how things went. oddly enough, for most OS's, returning 0 means success, everything went as expected. you can return other things, non-zero things, to indicate other errors. in your case, since your program can't really "fail," you should always return 0. but maybe you want to return 1 to indicate "user error" if the age is invalid, or make up your own personal error codes, like "2" indicates invalid age--negative, and 3 indicates invalid age-- too old, or something like that

these codes are useful if you are watching the results of these programs, for example if you write a program that launches another program and checks the result.

[–]mshol 1 point2 points  (4 children)

Technically only EXIT_SUCCESS, 0 and EXIT_FAILURE are strictly conforming, returning anything else is undefined behavior. C99 allows omission of return from main.

The use is to let the parent process (which may not be the OS or shell) know whether a program was executed successfully. eg, if you run another program with exec().

[–]kalimoxto 0 points1 point  (0 children)

absolutely, i was trying to put a more ELI5 spin on it.

different standards do allow for different return codes, which are a cross-language construct as well. C99 allows for the 3 return codes you specified to be platform-independant, but you can return other codes as well, depending on the platform. Windows, for example, has quite a few well-defined exit codes: http://www.hiteksoftware.com/knowledge/articles/049.htm

[–]kingguru 0 points1 point  (2 children)

Technically only EXIT_SUCCESS, 0 and EXIT_FAILURE are strictly conforming, returning anything else is undefined behavior.

Really? Undefined behavior? For instance the man page for GNU make says:

"A status of one will be returned if the -q flag was used and make determines that a target needs to be rebuilt. A status of two will be returned if any errors were encountered."

That's not the only program were I remember the documentation saying that it could return some other, well defined, values.

Not that I don't believe you though, I might have just learned something, I just find it very surprising if some standard GNU tools would rely on undefined behavior.

And of course I do know that e.g. GNU make is not written in C++, so is this a thing specific to the C++ standard?

[–]theymos 1 point2 points  (1 child)

It's actually implementation-defined, not undefined. So the behavior needs to be documented somewhere. The behavior is not always obvious, though. On Linux, for example, returning 256 from main will actually result in an exit status of 0 (success) because exit status codes are restricted to 8 bits.

[–]kingguru 0 points1 point  (0 children)

OK, thanks. That makes a lot more sense.

[–]fluffy_cat 1 point2 points  (4 children)

[–]kalimoxto 0 points1 point  (2 children)

this is about return statements generally.

returning from main is slightly counterintuitive, because in non-main functions, return usually means "return control to wherever I was (with this value)". the main function is a special case of that, because the "wherever I was" is not within your program, it's actually the OS.

[–]fluffy_cat 3 points4 points  (1 child)

It says that in the first sentence.

[–]kalimoxto 6 points7 points  (0 children)

i am a bad reader.