What are the general rules for comparing different data types in C?

后端 未结 4 1739
情歌与酒
情歌与酒 2020-12-10 14:32

Lets say I have the following scenarios:

int i = 10;
short s = 5;

if (s == i){
   do stuff...
} else if (s < i) {
  do stuff...
}

When

4条回答
  •  没有蜡笔的小新
    2020-12-10 15:26

    From Type Conversions:

    The set of implicit conversions on page 44, though informally stated, is exactly the set to remember for now. They're easy to remember if you notice that, as the authors say, the `lower' type is promoted to the `higher' type,'' where theorder'' of the types is

    char < short int < int < long int < float < double < long double
    

    That rule is easy to remember - "lower to higher" - but regarding signed and unsigned integer types it doesn't help much, those are nicely explained in Oli's post. But it's easy to remember and helps you in most cases.

提交回复
热议问题