It is little bit weird. I just play with the unsigned char type and negative values. I have the following code.
#include <stdio.h>
int main(int argc, char* agrv[]){
unsigned char c = -3;
printf("%d, %u, %d, %u\n", c, c, ~c, ~c);
}
The output is,
253, 253, -254, 4294967042
I can not figure out the last three values. What does %d and %u really do?
The
%dformat prints out anint, and%uprints out anunsigned int. All arithmetic onunsigned charvalues is done by first casting them tointand doing the operations onintvalues, and so~c(which is equal to-1 - (int)c) will return a negativeintvalue. An explicit cast would be needed to get theunsigned charresult before printing it out (and the call toprintfwould cast it back tointanyway).