On Sun, 17 Nov 2013 05:06:39 -0800 (PST),
kuoya...@gmail.com wrote:
>> char IWHi, IWLo; //I tried use int IWHi, IWLo; same result
...
> IWLo = fgetc(file4);
> printf("IWLo = %X",IWLo);
...
>Sometimes (20%) OK (Showed "IWLo = FFFFFFD9"), sometimes (80%) failed.
Change IWLo back to int, it *will* make a difference.
The function fgetc() returns int.It reads an unsigned char from the
stream and casts that to int, returning that result.
But you assign the return value of fgetc() to IWLo which is of type
char. And char is typically signed! (it's actually a setting but in
most cases char is signed)
If you use "%X" in the format string of printf() then an argument of
type int is expected (I believe it expects an unsigned int).
So : change the type of IWLo back to int.
The reason why it shows "FFFFFFD9" is because of the implict casts in
your code.
Assume fgetc(file4) returns an int with the value 255 (which is 0xFF
in hexadecimal notation).
You put that value in a (signed) char. As such it will be interpreted
as -1 instead of 255 (given 2's complement, of course). The difference
between an unsigned and a signed char will show if you assign it to a
bigger signed type like int.
For example :
unsigned char a = 0xFF;
int b;
b = a; // b will be 255
b = (char)a; // b will be 0xFFFFFFFF
(The above assumes 2's complement and an int of 32 bits)