Comparing int with long and others

后端 未结 2 803
猫巷女王i
猫巷女王i 2021-01-20 21:00

I am wondering if things like this :

int a = ...;
long b = ...;

if (a < b)
     doSomethings();

always works (excepted for unsigned)

2条回答
  •  后悔当初
    2021-01-20 21:17

    In this condition

    if (a < b)
    

    an object of type int is always converted to the type long provided that one of the operands has type long because type long has higher rank than type int.

    As for other types then according to the C Standard (6.5.8 Relational operators)

    3 If both of the operands have arithmetic type, the usual arithmetic conversions are performed.

    It means that after the integer promotion an operand with a lower rank is converted to the type of the other operand.

提交回复
热议问题