Dumb char problem

Dumb char problem

Post by Martin Joh » Fri, 16 Apr 2004 05:12:53


I'm having difficult understanding what's going on here let me explain my
quandry.

char x;
x = 133 % 256;

Now can someone please tell me the value of x?

This is not a trick question, but he's what happens when I print it out this
way.

printf("%x %x %d", x, 133 % 256, 133 % 256);

This prints out "FFFFFF85 85 133"

Why does it have to be 0xFFFFFF85, because it's really screwing things up!
Can someone please explain as I want it as 0x85.

Thanks,

Martin
 
 
 

Dumb char problem

Post by Baxte » Fri, 16 Apr 2004 09:27:53

Because %x is looking for an int and not a char?

--
-------------------------------------------------------------------------
Free software - Baxter Codeworks www.baxcode.com
-------------------------------------------------------------------------




this

 
 
 

Dumb char problem

Post by David Phil » Fri, 16 Apr 2004 16:08:24

In article <aWgfc.304$74.2@newsfe1-win>,


^ x gets sign-extended to an int here. Should be:

printf("%x %x %d", (unsigned) x, 133 % 256, 133 % 256);

Or:

If you had declared x as:

unsigned char x;

then the compiler would have widened it to an int appropriately.