-Wtype-limits on attempt to limit an unsigned integer

Viewed 466

Consider the following example:

unsigned short c = // ...
if (c > 0xfffful)
    c = 0xfffful;

Since unsigned short can actually be larger than 16 bits, I want to limit the value before snprintf it in hex format to a fixed-size buffer.

However, GCC (but not clang) gives a warning: comparison is always false due to limited range of data type [-Wtype-limits].

Is it a bug in GCC or I missed something? I understand that on my machine unsigned short is exactly 16 bits, but it's not guaranteed to be so on other platforms.

2 Answers
Related