c - Why is an unsigned int 1 lower than a char y -1? -


 main() {     unsigned x=1;     char y=-1;      if(x>y)           printf("x>y");     else         printf("x<=y"); } 

i expected x>y. when changed unsigned int signed int, got expected results.

if char equivalent signed char:

  • char promoted int (integer promotions, iso c99 §6.3.1.1 ¶2)
  • since int , unsigned have same rank, int converted unsigned (arithmetic conversions, iso c99 §6.3.1.8)

if char equivalent unsigned char:

  • char may promoted either int or unsigned int:
    • if int can represent unsigned char values (typically because sizeof(int) > sizeof(char)), char converted int.
    • otherwise (typically because sizeof(char)==sizeof(int)), char converted unsigned.
  • now have 1 operand either int or unsigned, , unsigned. first operand converted unsigned.

integer promotions: expression of type of lower rank int converted int if int can hold of values of original type, unsigned otherwise.

arithmetic conversions: try convert larger type. when there conflict between signed , unsigned, if larger (including case 2 types have same rank) type unsigned, go unsigned. otherwise, go signed in case can represent values of both types.

conversions integer types(iso c99 §6.3.1.3):

conversion of out-of-range value unsigned integer type done via wrap-around (modular arithmetic).

conversion of out-of-range value signed integer type implementation defined, , can raise signal (such sigfpe).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -