Inference for equal hexadecimal numbers
Excuse me for example the following program output is God horse?
#include <iostream> #include <string>using namespace std;int main (int, char *[]) { char s[1]={0}; S[0] = 0xFE; if (s[0] = = 0xFE) { cout<< "= =" <<endl; } else { cout<< "! =" <<endl; } return 0;}
Why is it not equal?
See:
Detailed reason: Literal constants also have default types, details such as the following
- Shaped literal constant (0xfe. 0x124,123,0) is of type int
- The type of the character literal constant (' 0 ', ' a ') is Char
- The type of Boolean literal constant (TRUE,FALSE) is bool
- The type of string literal ("ABC") is const char*
Http://zhidao.baidu.com/question/198400742.html?
Qbl=relate_question_1&word=c%2b%2b%20%ca%fd%d6%b5%c4%ac%c8%cf%c0%e0%d0%cd
Assigning a value to a variable with a literal literal will cause truncation.
Above is the type of literal constants, the following is the truncated rule. The following diagram is the key
The right approach:
(1) When assigning values with literal constants, ensure that the type of the left operand can contain the value of the operand (a byte hexadecimal literal of 0x** can be included by Char.) So you can rest assured that the value is assigned)
(2) When using variables and literals for logical and relational operations, consider whether the default type and variable type of the literal value are the same (no implicit type conversions will occur). Also see if the literal value will happen (1)
#include <iostream> #include <string>using namespace std;int main (int, char *[]) { char s[1]={0}; S[0] = (char) 0xfe;//s[0] = Fe, s[0] < 0 if (s[0] = = (char) 0xfe)//forbidden type conversion to int { cout<< "= =" <<en DL; } else { cout<< "! =" <<endl; } return 0;}
Therefore, when the char variable is assigned a constant, it is better to convert it, and to infer equality, avoid converting to int
0x hexadecimal number of common traps in Network programming (c + + literal constants)