I was studying how C stores data in memory by bit patterns.
However I have confronted some issues when it comes to printf formatting.
I have saved a variable as -10 (I do understand two's complement)
and another variable as 246. Those two variables have bit patterns of 11110110 (which is 0xF6).
I was trying to print out a value using the unsigned int hexadecimal format in printf.
char a = -10;
unsigned char b = 246;
printf("a : %x , b : %x\n" , (unsigned int) a, (unsigned int) b);
//a : fffffff6 , b : f6
Both integers have the same bit pattern of 0xF6. However, if I do perform type casting into unsigned int, the result varies. The result is 0xFFFFFFF6 for a, while b remains the same.
For a signed character, it seems to me that type casting process made the unsigned char into an integer and filled all the empty bits with 1.
Is this due to their signedness? Or is this just Undefined Behavior?