Data type promotions during arithmetic operations: -1 < (unsinged int) 1 == false

前端 未结 2 1484
青春惊慌失措
青春惊慌失措 2020-12-31 11:09
main()  {   
  if ( -1 < (unsigned char) 1 )
    printf(\"less than\");
  else        
    printf(\"NOT less than\");
} 

Prints less than<

2条回答
  •  醉梦人生
    2020-12-31 11:19

    This is due to integer promotions. Both arguments can be represented as an int, so they are converted to an int.

    ISO C 6.3.1.1, paragraph 2:

    If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.48) All other types are unchanged by the integer promotions.

提交回复
热议问题