Differences between C/C ++ char and int

Source: Internet
Author: User

The literal value is generally represented by a pair of single quotes. The char type is generally used to initialize and assign values with the literal value. Because char is a single-byte length, when a char variable is assigned a value of the character nominal value, when the content in a single quotation mark exceeds one byte, the system automatically intercepts one byte of content to the char variable and ignores other bytes of content.

For example, char a = '000000'; C ++ considers every value in a single quotation mark as a character nominal value, that is, 1 2 3 4 occupies one byte, '123' occupies a total of 4 bytes. However, char variable A occupies only one byte, but its initial value has 4 bytes, the system needs to extract a byte from '123' to A, but does it trash '1' to? No, it is to give '4' to. Because on the x86 Platform (http://baike.baidu.com/view/339142.htm), data is arranged in the form of little-Endian (http://baike.baidu.com/view/2368412.htm), low bytes are placed in the memory's low address, high bytes are placed in the memory's high address. Let's look at '1234568', from left to right from 1 to 4. However, the format stored in the computer is from 0x04 to 0x01, that is to say, the low 4 of '123' is stored in the low address space of the computer's memory, and the high 1 of '123' is stored in the high address space of the memory, therefore, when '123' is given to variable A, the system gives 4 of the addresses in the memory to A, and the rest are ignored.

Another example is Char B = 'Ah'. Because a Chinese character is dubyte, the system intercepts one of the bytes to Char, but the ghost knows what the captured byte is, so cout <B may contain garbled characters.

When the char variable is assigned a correct value, use the characters in the asii table, such as char c = '! ', What is the content in the single quotes, and what will be output by cout.

When a char variable is initialized with an integer literal, such as char d = 55, the system will enter what is the control character represented by this integer value in the asii table, for example, in asii Table 55 is the asii value of '7', and the result of cout <D; is 7. The ASCII value corresponding to char can be output if it is a printable character. If it is a non-printable character, it cannot be output. it's like 0x0d and 0x0a are both listed in carriage returns and line breaks, but they cannot be displayed if they do not have a shape.

However, there are only 127 ASCII tables ranging from 0 to 128. If an integer greater than 127 or even greater than 255 is given to Char, the system running result is unknown.

String literal value, which is defined by double quotation marks. It is generally called a C-style string. Its type should be char *, so char E = "T "; compilation errors may occur.

For int variables, int F = 10; is correct, which does not need to be explained. However, if you give the literal value of a character to int, for example, int G = 'A', the ASCII value (that is, 97) of character a in the ASCII table is given to, the result of cout <G; is 97.

If you give a multi-character literal value to int, for example, int H = 'abc'; cout 01100011
01100010
01100001
00000000
Reading is the opposite of storing data. First, read the high address bit and then the low address bit, that is, 00000000 01100001 01100010 01100011. The 32 digits indicate that the decimal integer value is 6382179.

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.