Learn C
#include "ioformatting/stderr_exit.h"

stderr, Exit Codes & Return Values in C

#concept

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.

#how it works
1

Two streams, two jobs

stdout = data for the next program; stderr = messages for the human. fprintf(stderr, "error: ...") keeps pipelines clean.

2

stderr is unbuffered

Error output appears immediately even mid-crash β€” exactly what diagnostics need, unlike buffered stdout.

3

Return meaningfully

0 success, 1 generic failure, 2 misuse (convention) β€” callers can only react to codes you actually vary.

4

EXIT_* macros

return EXIT_SUCCESS / EXIT_FAILURE from <stdlib.h> documents intent portably instead of magic 0/1.

#example
#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;
}
Output
error: negative value
1Validation fails, so the error branch runs instead of normal output.
2The message goes to stderr β€” identical on screen, separable under redirection.
3EXIT_FAILURE (1) tells the shell this run failed; success would return 0.
#use cases
CLI tools whose stdout feeds other programs
Crash-safe diagnostics on the unbuffered stream
CI scripts branching on tool exit codes
perror()/strerror() messages paired with meaningful codes

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself β€” building beats watching.