Search code examples
cintegerunsignedsigned

Preferred way to fix "comparison between signed and unsigned integer" warnings?


What is assumed to be the preferred way to fix "comparison between signed and unsigned integer" warnings safely? In my case I have to compare some uint_32 variables with #define constants.

Really simplified:

#define MAX(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
#define c (10)
unsigned int d = 11;
if(MAX(c,d)>10){
  //...
}

But I have really a lot of such cases - what would you suggest to solve this?


Solution

  • In this case, you can just change your constant to be unsigned as well:

    #define c 10U
    

    or alternatively, use a cast so that the typeof in your macro creates an unsigned variable:

    if (MAX((unsigned)c, d) > 10) {