Weird character processing. Do you know why?

Source: Internet
Author: User

The Code is as follows:

 define MDPW_READY          "\010"  int test(char *cmd) {     int id = (int)*cmd;     return id; } #define MDPW_READY          "\010"int test(char *cmd){ int id = (int)*cmd; return id;}

What is the returned result when test (MDPW_READY) is called? We can see that there is a \ 0 character before the character, and the end of the string, so what should we do...

The answer is 8. Do you know why? Let me analyze it for you:

Let's take a look at the processing of octal and hexadecimal in C.


How do I represent octal and hexadecimal in C?

The C language itself supports the following three inputs:
1. decimal. For example 20,457;
2. hexadecimal format, starting with 0x. For example, 0x7a;
3. octal, starting with 0. For example

Therefore, the C language does not have binary input and can be implemented by functions at most.

Octal expression

C/C ++ specifies that if a number is specified to use octal notation, a 0 (number 0) must be added before it. For example, 123 is in decimal format, however, 0123 indicates that octal is used. This is the expression of Octal numbers in C and C ++.

Neither C nor C ++ provides the expression of binary numbers.

Now, for the same number, for example, 100, we can use the normal 10 hexadecimal expression in the Code, for example, during variable initialization:

Int a = 100;

We can also write:

Int a = 0144; // 0144 is 100 of the 8 th percentile;

Remember, you cannot lose the first zero when expressing it in octal. Otherwise, the computer is regarded as a 10-digit system. However, when the number of octal characters is used, the value 0 is not used, which is the "Escape Character" expression used to express the characters.
Use of Octal numbers in escape characters

We have learned how to use an escape character '/' and a special letter to represent a character. For example, '\ n' indicates line ), '\ t' indicates the Tab character, and' \ 'indicates the single quotation mark. Today, we have learned another method to use escape characters: Escape Character '\' followed by an octal number, used to indicate characters whose ASCII code is equal to this value.

For example, check the ASCII code table and find the question mark character (?) The ASCII value of is 63, so we can convert it into an octal value: 77, and then use '\ 77' To Represent '? '. Because it is octal, it should be written as '\ 077', but because C/C ++ does not allow the use of a slash plus a 10-digit number to represent characters, so here 0 can be left empty.

For example:

Printf ("\ 077 \ n \ 77 \ n ")

The output result is:

?

?

 

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.