Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove the following lint warning in C?

Tags:

c

loops

lint

I have the following code:

#define NUM_DAYS 65
#define NUM_PERSON 33

int num = 0;

if(NUM_DAYS % NUM_PERSON)
{
    num = NUM_DAYS / NUM_PERSON;
}
else 
{
    uum = NUM_DAY / NUM_PERSON + 1;
}

num = num - 1;

while(num > 0)
{
    //do something here
}

but I got the following lint warning:

Warning 681: Loop is not entered

what is the problem and how to fix it?

like image 392
user2131316 Avatar asked Jul 23 '13 13:07

user2131316


2 Answers

As your code is written, the loop will not be entered. NUM_DAYS % NUM_PERSON will evaluate to true, so num will equal NUM_DAYS / NUM_PERSON. Since we are dealing with ints, 65 / 33 is equal to 1. 1 -1 is 0, so the while condition will fail.

If your code is written as intended (as in, those constants are the values you are expecting to always use), just remove the while loop. It will never be used. If, however, NUM_DAYS or NUM_PERSON may later contain other values, you probably have nothing to worry about. If those specific values aren't important, try setting them to values such that the division will evaluate to something greater than 1.

like image 199
pattivacek Avatar answered Sep 20 '22 17:09

pattivacek


#define NUM_DAYS 65
#define NUM_PERSON 33

int num = 0;

if(NUM_DAYS % NUM_PERSON) // we go here, since (NUM_DAYS % NUM_PERSON) > 0
{
    num = NUM_DAYS / NUM_PERSON; // so num = 1 now
}
else 
{
    uum = NUM_DAY / NUM_PERSON + 1;
}

num = num - 1; // num = 0 now

while(num > 0) // num = 0 ! So we don't go in this loop
{
    //do something here
}

That's why you get this warning. The compiler has determined that your loop is useless (with your current #define value).

like image 29
nouney Avatar answered Sep 22 '22 17:09

nouney