/
Signed/Unsigned Assignment Error

Signed/Unsigned Assignment Error

1) assign signed or char type variable, which has smaller bit size, into unsigned variable  (Sign extension problem)

ex)  32bit unsigned =  8bit / 16bit signed     ,, (occur sing extension)

ex)  32bit unsigned = char  (worse! It depends on compiler option)

   

unsigned  aaa; 
char zzs;
signed char ss;
  
ss= zzs = 0xF0;
  
aaa = ss ;   //expected aaa, but 0xFFFF FFF0
aaa = zzs ;   //expect aaa <= 0xF0, but it has different value depending on compiler option

For gcc, the default is signed, but you can modify that with -funsigned-char. note: for gcc in Android NDK, the default is unsigned. You can also explicitly ask for signed characters with -fsigned-char.

   

2) when compare char variables, it has different values depending on signed or unsigned

it depends on compiler option.

 

for (char depth = 3; depth >= 0 ; --depth)

 

char has 3 types

char  :  not allowed to use as number

signed char :  -128 ~ 127 

unsigned char : 0 ~ 255

 

Ie, this is error when you compare with char variables

=> It is fine when you use signed char or unsigned char

 

Related content