This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[if statement with unsigned and signed integers]



Hi!

I have a

signed int iA

and an unsigned int uiB!

Now,

is

if (iA > uiB) the same as if (uiB < iA)?

The question behind it,

is there a general rule, which says, iA is always converted to unsigned int,

and then the comparison is processed? Or is the conversion different, depending which

data type comes first in the if statement?

Thanks for your remarks,

AB

  • Yes, the C standard mandates that both operands of any binary operator (including comparisons) are converted to a common type before performing the operation.  The rules for finding the common type are called the "usual arithmetic conversions."  In this case, the common type is "unsigned int".  The order of the operands is not significant.