This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Comparison of constant (checking if flash location is not 0xff)

Hello,

I have one application, where I store several copies of settings into internal flash. I made command "CAT" for list of all stored settings. To identify single store location as "empty" I simply check one byte if it has value 0xff (when flash location is erased). Microcontroller is STM32F0. Here is part of my code:

#define FLASH_STORE_ADR                         0x8007c00
const settings_t settings_Store[SETTINGS_CATLEN] __attribute__((at(FLASH_STORE_ADR)));


....

        check = settings_Store[i].Ident[0];


        if ((strlen(settings_Store[i].Ident) > 0) & (check!=0xff))
        {
                DisplaySettings((settings_t *)&settings_Store[i]);
        }
        else
        {
                snprintf(izp, 32, " <Empty> ");
                USB_write((uint8_t *)izp, strlen(izp));
        }

....

The IDE shows "warning: comparison of constant 255 with expression of type 'char' is always true". The code works as it should, but the warning is annoying. The warning is displayed only in IDE as exclamation mark. Build does not report any warnings.

Is there any option to avoid this warning?

complete project is here: e.pavlin.si/.../

Parents
  • I would think you have the project build with char as being signed. As such, the positive maximum would be 127. Now, the 0xFF will be treated as 255. So, it is clear that whatever the value read from the location, it will never be 255. Hence the warning.

    There would be a number of additions you could make to remove the warning.

    Add typecast to the 0xFF: (char)0xFF

    Add typecast to the read of the char: (int)char

    Personally, I prefer to compiler projects with char being unsigned. To my mind it is less confusing and more predictable. You might want to consider this one too.

Reply
  • I would think you have the project build with char as being signed. As such, the positive maximum would be 127. Now, the 0xFF will be treated as 255. So, it is clear that whatever the value read from the location, it will never be 255. Hence the warning.

    There would be a number of additions you could make to remove the warning.

    Add typecast to the 0xFF: (char)0xFF

    Add typecast to the read of the char: (int)char

    Personally, I prefer to compiler projects with char being unsigned. To my mind it is less confusing and more predictable. You might want to consider this one too.

Children