The difference between C + + char and int

Source: Internet
Author: User

Character literals are typically represented by a pair of single quotes. Char types are typically initialized and assigned with character literals. Because the char type is a single-byte length, when a variable of type char is assigned a character literal, when the contents of the single quotation mark exceed one byte, the system automatically intercepts the contents of a byte to the char variable, ignoring the other byte contents.
such as Char a= ' 1234 '; C + + would consider each value in single quotation marks as a character literal, that is, 1 2 3 4 each for a byte, ' 1234 ' is a total of 4 bytes, but the char variable A is only one byte, the initialized value has 4 bytes, the system will be from ' 1234 ' intercept a byte of content to a, but is it intercept ' 1 ' to a? No, give ' 4 ' to a. Because on the x86 platform (http://baike.baidu.com/view/339142.htm), the data is based on Little-endian (http://baike.baidu.com/view/2368412.htmIn the form of an array, the low-order byte is placed in the lower address of the memory, and the high-bit byte is placed in the memory. We look at ' 1234 ', from left to right from 1 to 4, however the format stored in the computer is from 0x04 to 0x01, that is, ' 1234 ' of the low 4 is stored in the computer's memory lower address bit, ' 1234 ' High 1 is stored in the high address bit of memory, so when the ' 1234 ' to the variable A, The system puts 4 of the address in memory to a, all the rest of it.
Again such as: Char b= ' ah '; because a Chinese character is double-byte, the system will intercept one byte to char, but the god knows what the intercepted byte is, so cout<<b will appear garbled.
When assigning a value to a char variable correctly with a character in a asii table, such as Char c= '! ', the contents of the single quotation mark, cout will output what.
When the char variable is initialized with an integer literal, such as Char d=55, then the system will enter anything according to the control character represented by the integer value in the Asii table, for example, 55 is the asii value of ' 7 ' in the Asii table,cout<<d; The result is 7. Char corresponds to the ASCII value can be output if it is a display character, if it is not displayed characters, it will not be able to lose. It's like 0x0d and 0x0a. They are shown in the carriage return and newline characters respectively, but there is no way to display them without a shape that can represent these words Fu De.
However, there are only 0 to 127 of these 128 ASCII tables, and if integers greater than 127 are even greater than 255 for Char, the system's running result is unknown.
The string literal, which is the double quotation mark, is generally referred to as the C-style string, and his type should be char*, so char e= "T"; a compilation error occurs.
for int variables, int f=10; it's natural to be right, that's not much to explain. However, if you give a character literal to an int, such as int g= ' A ', the ASCII value of character A in the ASCII table (that is, 97) gives a,cout<<g; the result of 97.
If you put a multi-character literal to an int, such as int h= ' abc ';cout<01100011
01100010
01100001
00000000
Read the time and the time of the deposit when the opposite, read the high address bit, and then the low address bit, that is, 00000000 01100001 01100010 01100011 This 32-bit represents the decimal integer value of the whole good is 6382179.

The difference between C + + char and int

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.