0x hexadecimal number of common traps in Network programming (c + + literal constants)

Source: Internet
Author: User

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

    1. Shaped literal constant (0xfe. 0x124,123,0) is of type int
    2. The type of the character literal constant (' 0 ', ' a ') is Char
    3. The type of Boolean literal constant (TRUE,FALSE) is bool
    4. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.