Peter Clifton: > On Wed, 2010-10-13 at 00:05 +0200, Karl Hammar wrote: > > Why does > > > > #include <stdio.h> > > int main(void) { > > char cc = 0xc4 // 'ä' in latin1 > > short str = cc; > > printf("0x%04hx\n", str); > > } > > > > print 0xffc4 instead of 0x00c4 ? > > It is because 0xc4 is inserted into a "char", not an "unsigned char", > and is in binary: > > 1100 0100 > > Which is a _negative_ number. (-60 in decimal) > > The compiler will (correctly) sign-extend the assigned value in str when > performing the type conversion.
Close, but you missed to point out that a char is signed on my box. It could as well be unsigned, but then the problem would not show up. > I wasn't sure this was a question you already knew the answer two or > not.. but it is certainly a nice example of why you have to be careful > mixing different types, and signed-ness in general. Yes, and there are portability issues also. Regards, /Karl Hammar --------- Aspö Data Lilla Aspö 148 S-742 94 Östhammar Sweden +46 173 140 57 _______________________________________________ geda-user mailing list geda-user@moria.seul.org http://www.seul.org/cgi-bin/mailman/listinfo/geda-user