Why does the following code print “?” ?
Also how can -1 be assigned to an unsigned char?
char test;
unsigned char testu; //isn't it supposed to hold values in range 0 - 255?
test = -1;
testu = -1;
cout<<"TEST CHAR = "<<test<<endl;
cout<<"TESTU CHAR = "<<testu<<endl;
unsignedsimply affects how the internal representation of the number (chars are numbers, remember) is interpreted. So-1is1111 1111in two’s complement notation, which when put into anunsigned charchanges the meaning (for the same bit representation) to 255.The question mark is probably the result of your font/codepage not mapping the (extended) ASCII value 255 to a character it can display.
I don’t think
<<discerns between an unsigned char and a signed char, since it interprets their values as ASCII codes, not plain numbers.Also, it depends on your compiler whether
chars are signed or unsigned by default; actually, the spec states there’s three different char types (plain, signed, and unsigned).