According to this answer numeric constants passed to variadic functions are always treated as int
if they fit in one. This makes me wonder why the following code works with both, int
and long long
. Consider the following function call:
testfunc(4, 1000, 1001, 1002, 1003);
testfunc
looks like this:
void testfunc(int n, ...)
{
int k;
va_list marker;
va_start(marker, n);
for(k = 0; k < n; k++) {
int x = va_arg(marker, int);
printf("%d\n", x);
}
va_end(marker);
}
This works fine. It prints 1000, 1001, 1002, 1003. But to my surprise, the following code works as well:
void testfunc(int n, ...)
{
int k;
va_list marker;
va_start(marker, n);
for(k = 0; k < n; k++) {
long long x = va_arg(marker, long long);
printf("%lld\n", x);
}
va_end(marker);
}
Why is that? Why does it work with long long
too? I thought that numeric integer constants were passed as int
if they fit in one? (cf. link above) So how can it be that it works with long long
too?
Heck, it's even working when alternating between int
and long long
. This is confusing the heck out of me:
void testfunc(int n, ...)
{
int k;
va_list marker;
va_start(marker, n);
for(k = 0; k < n; k++) {
if(k & 1) {
long long x = va_arg(marker, long long);
printf("B: %lld\n", x);
} else {
int x = va_arg(marker, int);
printf("A: %d\n", x);
}
}
va_end(marker);
}
How can this be? I thought all my parameters were passed as int
... why can I arbitrarily switch back and forth between int
and long long
with no trouble at all? I'm really confused now...
Thanks for any light shed onto this!