signed char c;
unsigned char b;
b = 0xf4;
/* ... */
c = (signed)b;
The value of b
is 0xf4
(244
) but c
type (signed char
) can only hold values between -128
and 127
(in your implementation). So when 244
is assigned to c
it is first converted to signed char
and C says this integer conversion is implementation defined. gcc
as most implementations
just wraps around modulo 256
and that’s why the value of c
is -12
.
Here is gcc
documentation:
http://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html
“For conversion to a type of width N, the value is reduced modulo 2^N to be within range of the type”
2
solved A simple C program output is not as expected