Search code examples
cconstantspc-lint

how the following PC-Lint warning be cleaned in C?


I have a question about the following code:

#define NUM_DAYS 60
#define NUM_PEOPLE 30

int days[NUM_DAYS];
int people[NUM_PEOPLE];

int size;

size = sizeof(day) > sizeof(people) ? sizeof(day) : sizeof(people);

while the macros NUM_PEOPLE can be changed to bigger than NUM_DAYS or less than NUM_DAYS, then I got the warning: Warning 506: Constant value Boolean, how to fix it? or are there other ways to walk around it except change constant to variable?


Solution

  • Your checker is informing you that sizeof(day) and sizeof(people) are known at compile time, and so the conditional will always take one branch, and never the other.

    As an alternative to suppressing the warning on your tool, you can modify your code to use a conditional preprocessor directive to make the taken branch explicit.

    #if (NUM_DAYS > NUM_PEOPLE)
    #define SIZE sizeof(day)
    #else
    #define SIZE sizeof(people)
    #endif
    
    size = SIZE;