NO

Author Topic: Wrong? warning #2251 when comparing two unsigned integers  (Read 2487 times)

iZzz32

  • Guest
Wrong? warning #2251 when comparing two unsigned integers
« on: April 07, 2015, 12:37:43 AM »
Pelles C claims that:
Quote from: pocc
Operands of '>' have types with different signedness: 'unsigned int' and 'int'

in the following code:

Code: [Select]
#include <stdio.h>

int main(void) {
    unsigned int foo = 0;
    unsigned short bar = 1;
    return foo > bar;
}

The warning is obviously wrong because both variables are unsigned, thus bar should be silently promoted to unsigned int.
« Last Edit: April 07, 2015, 12:42:08 AM by iZzz32 »

neo313

  • Guest
Re: Wrong? warning #2251 when comparing two unsigned integers
« Reply #1 on: April 20, 2015, 10:41:35 PM »
My explanation on the issue:

Compiler applies integer promotions first and then checks and applies the remaining rules correctly( assuming the final conversion is applied ). However the warning is not correct.

Standard defines that first, integer promotions are performed on both operands, and then the remaining rules are applied:

We start with: foo: unsigned short int and bar: unsigned int

ISO/IEC 9899:201x
6.3.1.8 Usual arithmetic conversions


skip text...

Otherwise, the integer promotions are performed on both operands. Then the
following rules are applied to the promoted operands:


Otherwise in this case indicates that we are dealing with integers.
So we get: foo: int and bar: unsigned int

skip text...

Otherwise, if the operand that has unsigned integer type has rank greater or
equal to the rank of the type of the other operand, then the operand with
signed integer type is converted to the type of the operand with unsigned
integer type.


And finally: foo: unsigned int and bar: unsigned int

Compiler warns that the conversion of foo from int to unsigned int might change the sign and wrap. This is correct for the last conversion, but the compiler forgets that foo started with the type unsigned short int, therefore it cannot wrap, and looking at the overall conversion the warning isn't correct.


gcc with appropriate warnings( -conversion,... ) doesn't complain.
« Last Edit: April 20, 2015, 11:34:41 PM by neo313 »