Returning 1 on main()

Started by
14 comments, last by Promag 17 years, 9 months ago
Hi, i am using Bruce Eckel' s "thinking in C" Flash seminar to prepare me for C++, and in chapter one, he explains that a correct ending of main should be returning 0. He also mentions that returning 1 should be used when the program incorrectly ends. Now, i ask myself, why would i return 1, instead of fixing my code to let my program end gracefully. What does it actually do, returning 1 ? Does it allow the OS (windows, in my case) to perform additional cleanup? Confusing thoughts of the unwary beginner ;) Thanks in advance.
Advertisement
The return value of the application is traditionally 0 when the program executes correctly, but non-zero if the program fails for some reason. Reasons for failure include the user asking to open a file that doesn't exist, an invalid hostname or some other network failure and so on. Generally you don't return 1 because there was a problem with your code, you return an error code when something prevents your program from executing correctly.
Returning 1, like SiCrane said, is just a convention. The return value can be useful when you call another program from your program and want to know if it failed, but that's about it.
Good question, the return value of main can be used by the operating system to record how the program ended. For example in linux you can do

[~]$ echo $?

Which will show you the return value of the last excuted program. Zero is usually used if a program ended succesfully and there are a bunch of more error codes in linux you can look up.

As for why you would make your program exit with a error code, well that depends on what could go wrong during runtime and not when you compile your code. For example when your program checks if there is enough VRAM it can exit with a error.
Quote:Original post by betabyte
Now, i ask myself, why would i return 1, instead of fixing my code
to let my program end gracefully. What does it actually do, returning 1 ?

It's more often used when the program for some reason couldn't do the task it was supposed to.
Typical examples are:
- You try to delete a file that's write-protected. the del/rm/whatever command can't do much about that, so it needs to return a code that tells the caller that *something* went wrong. Or you try to copy a file but there's not enough disk space.
- You try to compile some C++ code which contains errors. The compiler *needs* to be able to inform surrounding processes whether it succeeded (For example, if using a makefile, it needs to know that it should abort, instead of moving on to the next commands. In Visual studio, you have the same. The IDE needs to know whether compilation succeeded, because if it didn't, it'd probably be a bad idea to automatically start the program.

Basically, any time other programs depend on yours, you need to return a code saying whether you succeeded at your task. In a game, that's often not so important, but still a good convention to stick to. Return 0 if you returned normally, or 1 if something went wrong an you couldn't fix things from within the program.

But you're right, if possible, you should try to fix the problems that occur, and then return 0. But that's not always possible. Other return codes are just to signal that for whatever reason, you couldn't do what you were supposed to.
There's also standard macros for that, defined in stdlib.h: EXIT_SUCCESS and EXIT_FAILURE. (not sure if there are more)
The reason why zero is treated as success and non-zero as failure is simply because there can be multiple reasons for failure. On the other hand, you don't usually care to know why your program succeeded. [rolleyes]
So you can use different values to report different problems while still being able to treat the value as a boolean if you just want to test for success/failure.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks for the replys.
So, if i understand correct, this could be used in a way,
as simple cross-application exeption handling ?

Like "AppThatSendsText.exe" returns 1, so that
"AppThatRecievesText.exe" could process the error
correctly.

Hmm, thanks alot. It's very clear to me now
as to why and when i should use it.

Just remember that it's completely arbitrary. If the return value of an application is not documented you can't rely on it being any particular thing.

As a counter example to the 0 means success / non-0 means error convention consider a word-counting program. For this program it's perfectly reasonable to have a convention that negative return codes are errors and zero or positive is the word count.

Quote:betabyte
i am using Bruce Eckel' s "thinking in C" Flash seminar to prepareme for C++


While many people do use C++ as an extended version of C, actually programming in C++ is quite a different mindset than C, especially if you use exceptions.
-Mike
Don't forget your code will happily execute with no return type on the main function.

void main()

I have never gave this that much thought, then again, I hardly ever give my program exiting much thought.

This topic is closed to new replies.

Advertisement