Consider the following code:
#include <stdio.h>
typedef int (*addif_fn_t) (int, int, int);
int add (int a, int b) {
return a + b;
}
int addif (int a, int b, int cond) {
return cond ? (a + b) : 0;
}
int main() {
addif_fn_t fn;
fn = addif;
printf("addif:\t%d %d\n", fn(1, 2, 1), fn(1, 2, 0));
fn = (addif_fn_t)add;
printf("add:\t%d %d\n", fn(1, 2, 1), fn(1, 2, 0));
return 0;
}
On any Intel machine using the standard C calling convention, this results in:
addif: 3 0
add: 3 3
The question is: how portable is this idiom? Does C allow calling a function with more parameters than it accepts?
My guess is that it depends entirely upon the ABI and how it determines out where both the function arguments and local variables are stored. More to the point, this is likely not portable code. But I have seen this idiom used several times in real codebases.