how to clean this lint warning in c?

2019-08-02 03:24发布

问题:

I have the following code:

#define NUMBER_OF_ROOMS

if((unsigned int)(NUMBER_OF_ROOMS - 2) > 20)
{
   // do something here
}

but I got a lint warning:Warning 506: Constant value Boolean, what does this mean and how to fix it?

回答1:

It means that the value of the expression is constant, and thus the if is pointless since it's known at compile-time whether or not it will be true or not.

You could of course make it more dynamic, or use the preprocessor instead:

#if (NUMBER_OF_ROOMS - 2) > 20
// do something here
#endif

I assumed the cast to (unsigned int) was pointless, if these really were values close to the boundaries of the integer precision, then Jens Gustedt's comment applies.



回答2:

It means that the value of your if statement is known at compile-time.

The compiler just sees if (30 - 2 > 20) (plus an unneeded cast), which it doesn't need to evaluate at runtime.



标签: c macros lint