The form of character data storage in memory and its usage (C implementation)

Source: Internet
Author: User
Tags arithmetic
The form of character data storage in memory and its usage (C implementation)
1, put a word constants amount in a character variable, not actually put the character itself into the memory unit, but the corresponding ASCII code of the character into the storage unit. For example, the ASCII code for the character ' A ' is C1, ' B ' is 98, and in memory the value of the variable, c2, is shown in the following figure (a). is actually stored in binary form, as shown in (b).

Since in memory, character data is stored in ASCII code, it is stored in the same form as an integer. The C language enables the use of both character and integer data. A character data can be output either as a character or as an integer. When outputting as a character, the ASCII code in the storage cell needs to be converted to the corresponding character and then output. When outputting as an integer, the ASCII code is output directly as an integer. You can also perform arithmetic operations on character data, which is equivalent to arithmetic operations on their ASCII code. Value, the character data occupies only one byte, and it only holds integers between the 0~255.

2. Assign an integer to a character variable.
1 Main () {
2 Char Ch1,ch2;
3 CH1 = 97;
4 CH2 = 98;
5 printf ("%c%c\n", CH1,CH2);
6 printf ("%d%d\n", CH1,CH2);
7} The results of the operation are as follows:
A b
97 98
Related Article

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.