Compilation (gcc error)Type & Declaration6 min read

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.

Broken Pattern
void scale(int n);
double scale(int n) { return n * 1.5; }
Corrected Pattern
double scale(int n);
double scale(int n) { return n * 1.5; }
Illustrative diagnostic (not executed):error: conflicting types for 'scale'; note: previous declaration was here

Parameter count or const/pointer mismatch

Passing char * vs const char * or differing parameter counts breaks compatibility.

Broken Pattern
int sum(int a, int b);
int sum(int a, long b) { return a + (int)b; }
Corrected Pattern
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 compiler
Read 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 compiler

Expected output from the corrected program

Reference output, not a live execution result.

1.5

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
conflicting types for `foo`Prototype in `.h` or top differs from `.c` bodyInclude `.h` in defining `.c`Align return type and parameter types everywhere
previous implicit declaration mismatchCompiler assumed `int foo()` before definition-Werror=implicit-function-declarationMove prototype above all calls

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

Single-Header Declaration
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.

Missing Prototype Warnings
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.