1

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?

  • 3
    It's important if the exist status is to be used by the program that lauched your program, for example shell scripts. For your C program itself it's irrelevant. – Jabberwocky Oct 31 '22 at 06:51
  • 2
    Successful exit in just one aspect, consider the many scenarios where it can fail, a specific exit status will let you know why exactly it failed without an immediate need to dig deeper. – Sourav Ghosh Oct 31 '22 at 06:52
  • 2
    To control the logic in a batch file. For example, if the compiler was unsucessfull, don't call the linker. – Weather Vane Oct 31 '22 at 07:41

1 Answers1

2

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.

cs1349459
  • 911
  • 9
  • 27