A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
main returns an int. That int is the program's exit status, delivered to the parent process. By convention, 0 means success and nonzero means failure. The C standard defines EXIT_SUCCESS and EXIT_FAILURE in stdlib.h as portable values. In C99 and later, reaching the end of main without a return statement is equivalent to return 0. In C89, it was undefined behavior. The shell captures the exit status in $? on Unix and %ERRORLEVEL% on Windows. Scripts branch on it.
#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]) {if (argc < 2) {fprintf(stderr, "Usage: %s <filename>\n", argv[0]);return EXIT_FAILURE;}FILE *fp = fopen(argv[1], "r");if (fp == NULL) {perror(argv[1]);return EXIT_FAILURE;}int ch;int count = 0;while ((ch = fgetc(fp)) != EOF) {count++;}fclose(fp);printf("%d bytes\n", count);return EXIT_SUCCESS;}
Three invocations, three outcomes. The shell sees 0 for success and 1 for failure. A script can chain: if ./main_return data.txt; then echo "processed"; else echo "failed"; fi. The exit status is the program's final word to the outside world. Use it deliberately. Return distinct nonzero values for distinct error categories if your callers need to distinguish them.
In C99 and later, if control reaches the end of main without a return statement, the compiler inserts return 0 implicitly. This is specific to main. Any other function that reaches its closing brace without returning a value has undefined behavior if the caller uses the result. The implicit return 0 is a convenience for simple programs. In production code, write the return explicitly. It documents intent and satisfies static analysis tools.
The exit status is a contract. Scripts depend on it. CI systems alert on it. Monitoring dashboards aggregate it. Return 0 when the work is done. Return nonzero when it is not. Be specific about the nonzero value if your callers need to distinguish error types. Next: comments, and why the best comment is the one you did not need to write.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.