jeudi 22 juillet 2021

Misra warning C code - Comparison of boolean and unsigned values in if loop

In the below C code, while checking the if condition, i am getting the Misra warning as

The operand of the opeartor '=='do not have same essential type category: one is 'Boolean' and other is 'unsigned'

In the header file file1.h

#define timer_4sec       (uint8)160
#define bool_rolltime    (roll_time_count < timer_4sec)

in the source file , file1.c

static uint8 roll_time_count = timer_4sec

void function()
{
  if(bool_rolltime == True)
  {
     printf("rolling ..\n");
  }
}
void function2()
{
    //update the roll_time_count status , increment and decrement static timer variable.
}

The problem is here if(bool_rolltime == True). I have understood that roll_time_count and timer_4sec are uint8 variables. So i have tried to fix this warning like this way

#define bool_rolltime    (bool)(roll_time_count < timer_4sec)

Is this code correct method to avoid the Misra error? If not please suggest how to avoid such warnings ?

Aucun commentaire:

Enregistrer un commentaire