0xXXXX与short的比较.要小心

main.cpp: In function ‘bool foo()’:
main.cpp:6: warning: comparison is always false due to limited range of data type

 1 #include <stdio.h>                                        
 2 bool foo()                                                
 3 {                                                         
 4         short nCommand = 0x813a;                          
 5         //return nCommand == (short)0x813a;               
 6         return nCommand == 0x813a;                        
 7 #if 0                                                     
 8         if ( nCommand == (short)0x813a ) {                
 9                 return true;                              
10         } else {                                          
11                 return false;                             
12         }                                                 
13 #endif                                                    
14 }                                                         
15 int main(int argc, const char *argv[])                    
16 {                                                         
17                                                           
18         if (foo()){                                       
19                 printf("TRUE\n");                         
20         } else {                                          
21                 printf("FALSE\n");                        
22         }                                                 
23         return 0;                                         
24 }                                                         
                                                             

看来要比较short整是要小心点..不然错了也很难找....

原文地址:https://www.cnblogs.com/vimmer/p/2675133.html