stderr, Exit Codes & Return Values in C
Unix programs speak two output streams: stdout carries data (pipeable into files and other programs), stderr carries diagnostics (always reaching the terminal). fprintf(stderr, ...) routes errors correctly so ./app > out.txt captures results while complaints still print β the difference between composable tools and chatty ones.
Exit codes continue the contract: return 0 (or EXIT_SUCCESS) means success, nonzero (EXIT_FAILURE, or specific small codes) means failure flavors. Shells, make, and CI branch on these numbers β a tool that always returns 0 lies to its entire ecosystem.
Two streams, two jobs
stdout = data for the next program; stderr = messages for the human. fprintf(stderr, "error: ...") keeps pipelines clean.
stderr is unbuffered
Error output appears immediately even mid-crash β exactly what diagnostics need, unlike buffered stdout.
Return meaningfully
0 success, 1 generic failure, 2 misuse (convention) β callers can only react to codes you actually vary.
EXIT_* macros
return EXIT_SUCCESS / EXIT_FAILURE from <stdlib.h> documents intent portably instead of magic 0/1.
#include <stdio.h>#include <stdlib.h>Β int main(void) { int value = -3; if (value < 0) { fprintf(stderr, "error: negative value\n"); return EXIT_FAILURE; } printf("%d\n", value); return EXIT_SUCCESS;}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself β building beats watching.