C variable and constant value comparison not matching -
if have:
signed char * p; and comparison:
if ( *p == 0xff ) break; it never catch 0xff, if replace -1 will:
if ( *p == (signed char)0xff ) break; how can happen? sign flag? though 0xff == -1 == 255.
the value 0xff signed int value. c promote *p int when doing comparison, first if statement equivalent to:
if( -1 == 255 ) break; which of course false. using (signed char)0xff statement equivalent to:
if( -1 == -1 ) break; which works expect. key point here comparison done int types instead of signed char types.
Comments
Post a Comment