Strange condition assessment

The following code:

#include <stdint.h>

int main() {
    uint8_t Byte;

    if (Byte < 0) { }
}

issues the following compilation warning:

main.cpp: In function `int main()':
main.cpp:6: warning: comparison is always false due to limited range of data type

This is normal. But when I change the condition to:

(1) if (true || (Byte < 0)) { }

I am still getting a warning, while I expect to get a warning, such as "the comparison is always true ..." :)

If I changed the byte declaration to:

(2) uint32_t Byte;

warning disappears.

How can I explain the behavior?

My RHEL 5.3 64 bit system comes with gcc 4.1.2.

EDIT:

(1) not a problem, I just misunderstood the compiler warning. He does not say that the if integer is false, but rather "Byte <0".

So the problem is only in (2) - why the byte type triggers a compiler warning. The constant "0" is of type int, so its 4 bytes. Therefore, it must be associated with comparison if uint8_t with int

+3
1

0 int () int. uint8_t 0 255, 32- int.

, uint32_t 0 2 ^ 32-1, 32- int ( , 2 ^ 31, int, ). , , .

+1

Source: https://habr.com/ru/post/1773618/


All Articles