Why does this not work as expected?
int main()
{
unsigned char louise, peter;
printf("Age of Louise: ");
scanf("%u", &louise);
printf("Age of Peter: ");
scanf("%u", &peter);
printf("Louise: %u\n", louise);
printf("Peter: %u\n", peter);
return 0;
}
Outputs:
Age of Louise: 12
Age of Peter: 13
Louise: 0
Peter: 13
But if I swap variable declarations it works:
unsigned char peter, louise;
Outputs:
Age of Louise: 12
Age of Peter: 13
Louise: 12
Peter: 13
I've also noticed that using int
or unsigned int
works without needing to swap variables, but char
doesn't.
I've tried putting printf("%u", louise);
just after the scanf()
for louise and the value is saved correctly. And if I comment out the second scanf()
it also works fine...
The "problem" shows on Windows (DevCpp) and Linux (kwrite + make). Is that a bug of the compiler, or mine?