Is returning zero from main necessary, and how can the return value from main be useful?
I know it's been the convention in C89 to always return a 0 integer value from main in a C program, like this:
int main() {
/* do something useful here */
return 0;
}
This is to return a "successful" result to the operating system. I still consider myself a novice (or an intermediate programmer at best) in C, but to date I've never fully understood why this is important.
My guess is, thi开发者_运维百科s is a useful return result if you're tying the output of this program into the input of another, but I'm not sure. I've never found it useful, or maybe I just don't understand what the intention is.
My questions:
- Is returning zero always necessary from a C program?
- How is the return value from
main()
useful?
When writing scripts (like in Bash, or CMD.exe on Windows) you can chain some commands with the && and || operators.
Canonically, a && b
will run b
if the result of a
is zero, and a || b
will run b
if a
returned nonzero.
This is useful if you wish to conditionally run a command if the previous one succeeded. For example, you would like to delete a file if it contains word foo. Then you will use :
grep foo myfile && rm myfile
grep
returns 0 when there was a match, else nonzero.
Returning 0 is a convention. When a program returns 0, it can be assumed that it worked OK, without actually looking at what the program did (ahem :D).
As a widely used convention, that assumption is in a lot of places. As Benoit points out that's the case of the shell (UNIX and Windows) and other parts of the Operating system.
So answering your questions:
- For a C program you must return either EXIT_SUCCESS or EXIT_FAILURE. But you can return EXIT_FAILURE even if your program worked OK.
- If you don't return a 0 (EXIT_SUCCESS), it's quite possible that other programs will assume your program failed.
There's a related question with C++ and C great responses
What should main() return in C and C++?
In modern C aka C99 (not sure for C89) all the three terminations of main
are equivalent:
- just ending
main
at the last}
return 0
exit(0)
the idea behind all this is (as others mentioned) to give a return status to the invoking program.
The return value is the "result" of the program execution, and 0 is used to indicate a successful termination, while a non-zero return value indicates a failure or unexpected termination.
The return value doesn't really matter to the system when you call you program normally, but it can have two purposes. One of them is debugging. In MSVC, the most commonly used compiler for C++, you see the programs return value after it finishes executing. This can be helpful to see "how and why" your program exited.
Another use is when your application is called from other programs, where the return value may indicate success, or pass on a result.
Hiya, like you said its mainly if the program is used as part of wider program network. Returning zero to the program environment lets the network know everything went fine (like you said) however you can also have it return 1, 2, 3... (depending on what error has occured) to let your network of code know that something has gone wrong. Equivalently, you can have exit(0) or exit(1) at the end of your 'main' program to do exactly the same thing. You may find this useful: http://en.wikipedia.org/wiki/Exit_status Hope that helps. Jack
You should use EXIT_SUCCESS when the program finished correctly, and EXIT_FAILURE when it didn't. EXIT_SUCCESS is zero, and zero is portable to any operating system, while EXIT_FAILURE changes from UNIX to Windows, for example. These constants are defined in the stdlib.h
header.
#include <stdlib.h>
int main()
{
int toret = EXIT_SUCCESS;
if ( !( /* do something useful here */ ) ) {
toret = EXIT_FAILURE;
}
return toret;
}
The return code of the program was more useful when programs were written for the console. Nowadays, it is quite uncommon, unless you work in a very professional environment (and even this is now changing, with the workflow tools available).
As @Benoit said, the exit code tells the operating system when the operation was successful or not. If the exit code means failure, then you can break the flow of the batch program, since it is not likely to work out.
For example, a compiler can have an exit code of zero if compilation was successful, and any another value if compilation was unsuccessful. In Windows, this can be accessed through the operating system variable "errorlevel":
gcc helloworld.cpp -ohelloworld.exe
goto answer%errorlevel%
:answer0
copy helloworld.exe c:\users\username\Desktop
echo Program installed
goto end
:answer1
echo There were errors. Check your source code.
:end
echo Now exiting...
This windows batch file "installs" helloworld.exe in the Desktop when the compilation was successful. Since you can trigger execution of batch files with double-click, this can make it possible for you to avoid touching the command line for compilation.
Of course, take into account that is better managed by integrated environments (if the exit code did not exist, they wouldn't be able to work correctly). Also note that make is best in this field:
https://en.wikipedia.org/wiki/Make_(software)
Make also needs of exit codes to run correctly.
We can return any integer from main()
.
Now, by default main()
function returns 0 if we do not explicitly mention main()
function to return any specific value.
return 0 actually resembles that the program terminated successfully without any errors though its not a rule but this is the way it works.
And if we explicitly return a non-zero number then even though the program runs correctly but internally it actually means that the program terminated with some errors or program terminated with an unexpected result.This can be useful if another program is called based on our current programs return value using '&&' or '||' in our command line.
You can also check the value of return of your previously executed program on linux using this command:
echo $?
精彩评论