Here's a small test program that demonstrates what I'm experiencing. I'm building it with gcc 4.2.1.
Code:
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
void test(int foo, ...) {
va_list args;
va_start(args, foo);
printf("%lld\n", va_arg(args, long long));
va_end(args);
}
int main() {
// I would expect this to output -1
test(0, -1);
// Works with a cast
test(0, (long long)-1);
return 0;
}
The output of this is:
4294967295
-1
The first number corresponds to (unsigned)-1. I cannot figure out why this conversion is being made. My understanding (and probably incorrect) is that the compiler should promote the untyped value of -1 to the type specified in va_arg.
Can anyone shed light on why this is happening?
Thanks,
Fred