🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →C Function Argument Mismatch
Learn how C function arguments are matched against prototypes, and how to predict what happens when a call has too few, too many or wrongly typed arguments.
Almost every output-prediction question about functions starts with the same hidden step: do the C function arguments in the call actually match what the function expects? Before you trace a single value, the compiler has already decided whether your program is valid. In this lecture of our free C programming course you will learn exactly how that decision is made, why a mismatch is a compile error in modern C but silent corruption in older code, and why this matters a great deal for embedded C programming on microcontrollers.
This is a hands-on free C tutorial. Every program below was compiled with GCC and executed, so the outputs and error messages you see are real, not guessed.
What You Will Learn
- How the compiler checks C function arguments against a prototype, step by step
- What happens when a call passes too few or too many arguments
- Why a declaration and a definition that disagree produce a conflicting types error
- Why you cannot use the result of a
voidfunction in an expression - How old-style empty parentheses behaved in C17 and how C23 changed them
- A repeatable checklist for answering “what is the output?” questions on functions
Prerequisites
You should know how to write a simple function, what a return value is, and how to compile a program with gcc. If these are new to you, revisit the earlier lectures of this chapter on function calls, declarations and the return statement first. This lecture belongs to the free embedded C course track, so every idea is explained from the ground up.
How the Compiler Matches C Function Arguments
A function prototype is a promise. It tells the compiler the name, the number of parameters, the type of each parameter and the return type. When the compiler later meets a call, it compares the call against that promise. If the promise is broken, a correct compiler stops. If there is no promise at all, the compiler has nothing to compare against and must trust you blindly.
A correct baseline program
Start with a program that is fully valid, so that you have something to compare the broken versions against.
#include <stdio.h>
int area(int w, int h);
int main(void)
{
printf("%d\n", area(4, 5));
printf("%d\n", area(10 / 3, 7 % 4));
return 0;
}
int area(int w, int h)
{
return w * h;
}
Compile and run it:
$ gcc -Wall -Wextra -std=c17 ok_area.c -o ok_area
$ ./ok_area
20
9
The second call deserves attention. Each argument is an expression, and each expression is reduced to a single value before the function starts. Here 10 / 3 is integer division and gives 3, and 7 % 4 gives 3, so the function multiplies 3 by 3. The function never sees the expressions, only the two resulting values. That single fact explains many tricky exam questions.
Too Few or Too Many C Function Arguments
Now break the call on purpose. The prototype still says two integers, but the call supplies only one.
int area(int w, int h);
int main(void)
{
printf("%d\n", area(4));
return 0;
}
$ gcc -Wall -Wextra -std=c17 few.c -o few
few.c:5:20: error: too few arguments to function 'area'
Supplying three values to a two-parameter function fails in the same way:
$ gcc -Wall -Wextra -std=c17 many.c -o many
many.c:5:20: error: too many arguments to function 'area'
So when an exam program calls a function with the wrong number of values and a proper prototype is visible, the correct answer is not a number. The correct answer is “compilation error”.
What happens without a prototype
Older C allowed a declaration with empty parentheses, such as int area();. That does not mean “no parameters”. In C17 and earlier it means “parameters unspecified”, so the compiler stops checking. A call like area(4) then compiles cleanly even though the definition wants two values. The result is undefined behavior, which means the language gives no guarantee at all.
$ gcc -Wall -std=c17 oldstyle.c -o old17
(no diagnostic - the program compiles)
$ gcc -Wall -std=c2x oldstyle.c -o old23
oldstyle.c:5:20: error: too many arguments to function 'area'
oldstyle.c:8:5: error: conflicting types for 'area'; have 'int(int, int)'
C23 changed the rule: empty parentheses now mean the same as (void). The same source that silently compiled under C17 is rejected under C23. Never rely on the old behavior; always write real prototypes.
Declaration and Definition Must Agree
The compiler also checks the prototype against the later definition. If one says one parameter and the other says two, you get a conflicting types error.
int scale(int a);
...
int scale(int a, int b) { return a * b; }
conflict.c:8:5: error: conflicting types for 'scale'; have 'int(int, int)'
In real projects this happens when someone updates a function in the .c file but forgets the header. Keeping prototypes in a header that the defining file also includes lets the compiler catch the disagreement immediately.
Using the Result of a void Function
A function declared void returns nothing, so there is no value to store or print.
void report(int code) { printf("code %d\n", code); }
int main(void)
{
int r = report(3);
printf("%d\n", r);
return 0;
}
voidval.c:5:13: error: void value not ignored as it ought to be
Summary Table: C Function Arguments Outcomes
| Situation | C17 with prototype | C17 with empty parentheses | C23 |
|---|---|---|---|
| Too few arguments | Compile error | Compiles, undefined behavior | Compile error |
| Too many arguments | Compile error | Compiles, undefined behavior | Compile error |
| Declaration and definition differ | Conflicting types error | Often compiles | Conflicting types error |
| Result of void function used | Compile error | Compile error | Compile error |
| Argument is an expression | Reduced to one value first | Same | Same |
A Checklist for Checking C Function Arguments
Whenever you are asked to predict the output of a program that uses functions, work through these steps in order. Most students skip straight to step five and lose marks on programs that never compile.
- Is a prototype visible before each call, or does the definition appear earlier?
- Does the number of values in each call equal the number of parameters?
- Does the declaration match the definition in parameter count and types?
- Is any
voidfunction used inside an expression or assignment? - Only now trace the values, call by call, writing each variable in a small table.
Real-World Use: Driver Headers in Embedded Projects
Hardware abstraction layers expose small functions such as one that writes bytes to a serial port. A header file holds the prototype, and every source file includes it.
/* serial.h */
#ifndef SERIAL_H
#define SERIAL_H
#include <stddef.h>
#include <stdint.h>
int serial_write(const uint8_t *buf, size_t len);
#endif
If a developer later adds a timeout parameter to the definition, every old call site that still passes two values will fail to compile, which is exactly what you want. The compiler becomes a checklist that lists every place needing an update.
Common Mistakes and Troubleshooting
Most bugs with C function arguments fall into the four patterns below.
| Symptom | Likely cause | Fix |
|---|---|---|
| too few / too many arguments | Call and prototype disagree | Count the parameters, fix the call or the prototype |
| conflicting types for function | Header and definition not updated together | Include the header in the defining file |
| void value not ignored | Using a void function as a value | Change the return type or stop using the result |
| Random value in a parameter | Call to an unprototyped function with a missing argument | Add a prototype and compile with warnings |
Best Practices, Performance and Security
- Always write full prototypes, and write
(void)for functions that take nothing. - Compile with
-Wall -Wextra -Wstrict-prototypesand treat warnings as errors in your build. - Put prototypes in headers and include the header in the file that defines the function.
- Review every call site whenever you change the C function arguments of a shared helper.
- Performance: checking arguments costs nothing at run time, because it happens entirely during compilation.
- Security: a call with the wrong arguments can corrupt the stack or leak register contents, so mismatches are a real source of vulnerabilities in firmware.
Key Takeaways
- The C function arguments in a call are evaluated to plain values before the function body runs.
- With a prototype, a wrong argument count is a compile error, not a runtime surprise.
- Empty parentheses meant “unspecified” in C17 and mean “no parameters” in C23.
- Declaration, definition and call must all agree.
- Check validity first, trace values second.
Conclusion
Understanding C function arguments is less about memorizing rules and more about knowing who checks what. The prototype is the contract, the compiler enforces it, and the old unprototyped style is the loophole that causes hours of debugging. If you keep that picture in mind, a whole category of tricky function questions becomes easy: spot the mismatch, declare a compile error and move on. In the next lecture of this free embedded C course we look at what a function can and cannot change once the arguments arrive.
Frequently Asked Questions
What is the difference between a parameter and an argument in C?
A parameter is the variable named in the function definition. An argument is the value supplied in the call. The argument initializes the parameter.
Is calling a function with fewer arguments always an error?
With a visible prototype, yes. Without a prototype in C17 and earlier it compiles but is undefined behavior, and in C23 it is an error.
Does int f(); mean the function takes no arguments?
Not before C23. It meant unspecified parameters. Use int f(void); to say no parameters.
Can the compiler check argument types too?
Yes. With a prototype each argument is converted to the parameter type, and incompatible types are diagnosed.
Why do embedded projects care so much about prototypes?
Hardware faults from wrong arguments are silent and hard to reproduce, so catching them at compile time saves large amounts of debugging effort.
Where should prototypes live?
In a header file included both by callers and by the file that defines the function.
Keep Learning with the Free C Tutorial
Continue through the free C programming course and build solid embedded C programming skills at no cost.

2 Comments