const unsigned short min = 0, max = 10; int main(int argc,char* argv[]) { unsigned short test = 0; if ( (test >= min) && (test <= max) ) return 0; else return 1; } $ g++ -o test main.cpp has the following error: main.cpp: In function âint main(int, char**)â: main.cpp:6: warning: comparison is always true due to limited range of data type $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=i386-redhat-linux Thread model: posix gcc version 4.1.2 20070925 (Red Hat 4.1.2-27) Notes: If I remove the const declaration, then no warning--why?
Because if the min variable is not const, 0 is just an initializer and the comparison must be done at runtime, as it could have been changed by other compilation units (e.g. in constructors). When it is const, there is test >= 0 comparison were test is unsigned short. Which is always true, there are no negative unsigned values.