NO

Author Topic: Comparing unsigned int with unsigned char produces different signedness warning  (Read 499 times)

Offline FRex

  • Member
  • *
  • Posts: 4
Example program:
Code: (C) [Select]
int main(void)
{
    unsigned int x = 0u;
    unsigned char y = 0u;
    return x > y;
}

Code: [Select]
main.c(5): warning #2251: Operands of '>' have types with different signedness: 'unsigned int' and 'int'.
I report it because I consider this incorrect since both operands are clearly unsigned. MSVC, clang and gcc do not generate such erroneous warning in this situation either (but would if y was just 'int').

I'm guessing this appears because unsigned char is first promoted to int (which is signed) and then the compiler just sees (signed) int being compared with unsigned int, forgetting that this int came from an integer promotion of a smaller unsigned integer type.
« Last Edit: March 05, 2019, 11:08:36 pm by FRex »

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 1995
    • http://www.smorgasbordet.com
Uncommon case, but I will look at it some day...
/Pelle

Offline FRex

  • Member
  • *
  • Posts: 4
I ran into it in one file (lparser.c) of Lua 5.1