Fix “control reaches end of non-void function” in C
In C, falling off the end of a non-void function whose return value is used by the caller causes undefined behavior. Whatever arbitrary bits happen to occupy the CPU return register (`EAX`/`RAX`) leak to the caller.
Quick Fix Summary
Add an unconditional final return (or else branch return) in every non-void function, and compile with -Werror=return-type.
Why This Error Happens
Why callers receive unpredictable numbers
Only main() has a special C99+ rule that inserts an implicit return 0; at its closing brace. All other non-void functions leave their return register uninitialized if a path misses return.
Switch statements and exhaustive enums
Even if a switch handles every enum constant, adding a default: branch protects against runtime integer values out of range.
Top Root Causes & Specific Code Fixes
An if without a matching else return
When the if condition is false, control reaches the closing brace without returning.
int sign_of(int x) {
if (x > 0) return 1;
if (x < 0) return -1;
}int sign_of(int x) {
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}Forgotten default return after a switch statement
If no case matches and no default returns, execution falls out the bottom.
int code(char c) {
switch (c) { case 'A': return 1; }
}int code(char c) {
switch (c) { case 'A': return 1; default: return -1; }
}Complete Broken and Corrected Programs
Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.
Read the broken program
#include <stdio.h>
int max_pos(int a, int b) {
if (a > b) return a;
}
int main(void) {
printf("%d\n", max_pos(2, 5));
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
int max_pos(int a, int b) {
return (a > b) ? a : b;
}
int main(void) {
printf("%d\n", max_pos(2, 5));
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
5Compiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| warning: control reaches end of non-void function | Branch finishes without `return` value | -Werror=return-type | Add explicit fallback `return` at function bottom |
| switch missing default return | Unmatched enum/int skips all `return`s | -Wswitch -Werror=return-type | Add `default: return ...;` branch |
Debugging Techniques and Commands
gcc -std=c17 -Wall -Wextra -Werror=return-type main.c -o app
Prevents binaries with missing return paths from compiling silently.
clang -std=c17 -fsanitize=return main.cpp -o app
Detects missing returns in C++ builds immediately at runtime.
Frequently Asked Questions
Does void main() avoid this warning?
Standard hosted C requires `int main(void)` returning `int`; `main()` implicitly returns `0` in C99+, though writing `return 0;` explicitly is clearer.
Why did my program return 0 once without a return statement?
By coincidence, a previous helper call left 0 in the RAX register; optimization or different compiler flags will change that value unpredictably.
Can a function return void instead?
Yes. If no value needs to flow back to callers, change the return type to `void`.