Home > Software engineering >  Why is the exit status of a program important?
Why is the exit status of a program important?

Time:10-31

Are there any other reasons why we need exit status in a program other than that it'll help the operating system to check if the program is successful?

For example, in C we write exit(EXIT_SUCCESS);. Is it just to show the C program that the code is done?

CodePudding user response:

As the comments said, it is for a parent program, such as a shell or batch file, to see if the program was successful.

For example, in a shell script, using the set -e command, the shell script will exit if any simple command returns a non-zero value.

Similarly, in a Makefile, if any compilation fails, it will exit the Makefile (Unless otherwise specified, I believe). This is used to stop compilation if a file is unable to be compiled.

Also, different exit codes can signify different errors. A code of 0 signifies success, while a non-zero value (1-255) represents failure. As said by the GNU bash manual, an exit code of 127 represents the command attempted to be executed could not be found, and 126 represents the file is not an executable.

  • Related