A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
The exit status is the integer your program returns to the parent process. The C standard defines EXIT_SUCCESS and EXIT_FAILURE in stdlib.h as portable success and failure values. On Unix, EXIT_SUCCESS is 0 and EXIT_FAILURE is 1. The shell convention extends this: 0 is success, 1 is general error, 2 is misuse of a command, 126 is permission denied, 127 is command not found, and 128+N is termination by signal N. Your program can use specific nonzero values to encode error categories.
#include <stdio.h>#include <stdlib.h>#include <string.h>#define EXIT_OK 0#define EXIT_BAD_ARGS 1#define EXIT_FILE_ERROR 2#define EXIT_PARSE_ERROR 3int main(int argc, char *argv[]) {if (argc < 2) {fprintf(stderr, "Usage: %s <filename>\n", argv[0]);return EXIT_BAD_ARGS;}FILE *fp = fopen(argv[1], "r");if (fp == NULL) {perror(argv[1]);return EXIT_FILE_ERROR;}char line[256];if (fgets(line, sizeof(line), fp) == NULL) {fprintf(stderr, "Empty or unreadable file\n");fclose(fp);return EXIT_PARSE_ERROR;}printf("First line: %s", line);fclose(fp);return EXIT_OK;}
Three distinct error codes. A calling script can branch on each: case $? in 0) echo ok;; 1) echo bad args;; 2) echo file error;; 3) echo parse error;; esac. The macros make the code self-documenting. The reader does not need to remember that 2 means file error. The name says it.
Exit statuses are your program's final communication with the outside world. Use them deliberately. Define named constants for your error categories. Return 0 only when the work succeeded. The scripts, monitors, and humans that depend on your program will thank you. Next: function parameters vs arguments, a terminology distinction that matters in documentation and code review.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.