Fix “conflicting types for” Function Mismatch in C
C compilers require every forward prototype, header declaration, and function definition in a translation unit to agree completely on return type and parameter types. Any mismatch aborts compilation.
Quick Fix Summary
Declare a shared prototype in a .h header included by both the .c file defining the function and every .c caller, and place helper prototypes above main().
Why This Error Happens
Header vs implementation drift
Updating a parameter from int to double or size_t in math_utils.c while forgetting math_utils.h breaks type compatibility across translation units.
Empty parentheses vs (void)
Prior to C23, int init(); declared unspecified arguments, conflicting subtly with stricter prototypes; always write int init(void); when taking no parameters.
Top Root Causes & Specific Code Fixes
Forward prototype disagrees with function definition
Returning void in a prototype while returning double in the definition triggers a type conflict.
void scale(int n);
double scale(int n) { return n * 1.5; }double scale(int n);
double scale(int n) { return n * 1.5; }Parameter count or const/pointer mismatch
Passing char * vs const char * or differing parameter counts breaks compatibility.
int sum(int a, int b);
int sum(int a, long b) { return a + (int)b; }int sum(int a, long b);
int sum(int a, long b) { return a + (int)b; }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 compute(int x);
double compute(int x) {
return x * 0.5;
}
int main(void) {
printf("%f\n", compute(3));
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
double compute(int x);
double compute(int x) {
return x * 0.5;
}
int main(void) {
printf("%.1f\n", compute(3));
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
1.5Compiler & Sanitizer Diagnostic Matrix
| Compiler / Runtime Symptom | Root Cause | Diagnostic Flag / Check | Corrected Pattern |
|---|---|---|---|
| conflicting types for `foo` | Prototype in `.h` or top differs from `.c` body | Include `.h` in defining `.c` | Align return type and parameter types everywhere |
| previous implicit declaration mismatch | Compiler assumed `int foo()` before definition | -Werror=implicit-function-declaration | Move prototype above all calls |
Debugging Techniques and Commands
gcc -std=c17 -Wall -Wextra -Wstrict-prototypes *.c -o app
Include the .h file in the defining .c file so the compiler verifies prototype agreement automatically.
gcc -Wmissing-prototypes -Wall main.c -o app
Catches public non-static functions lacking matching header declarations.
Frequently Asked Questions
Why put #include "myheader.h" inside myheader.c too?
Including the header in its own implementation file guarantees the compiler catches signature drift immediately at compile time.
Do parameter variable names have to match?
No. Only types and qualifiers (`int`, `const char *`) must match between prototype and definition.
How does C differ from C++ here?
C++ overloads functions by parameter types; C does not overload functions, so duplicate names with differing parameter types conflict.