Explore C-language characters and character denominations

Source: Internet
Author: User

This article is from the shimachao blog http://blog.csdn.net/shimachao/article/details/8379676.


Char:

The character type in C language is actually an integer. You can perform arithmetic operations on the char (character) variable like the int type. The specific interpretation of char variables into char variables or int variables depends on the program. Int and char variables can also be assigned values (as long as they do not exceed the char expression range ). For example:

char ch = 'a';printf("%c,%d\n",ch,ch);

Print a, 97

That is to say, ch can be interpreted as both the character a and the Integer 97.


Character nominal value(Sometimes called a character constant ):

Let's talk about the meaning of the literal value: the literal value is just a mark, such as 3,100, a, c, and 3.14. Because we cannot modify it (Can you change 3 to 5 ?) So sometimes it is called a constant. For example:

Int a = 100; a is an int variable, and 100 is a literal value. The literal value 100 can only be used as the right value, not the left value.

In C, the character nominal value is a single character enclosed by a pair of single quotes, such as 'A' and 'B '. In fact, the character nominal value is the same as the integer literal value. The compiler treats all character denominations as integer denominations.

Define a character variable and use the literal value for initialization: char ch = 'a'; or char ch = 97;

Define an int type variable and use the literal value to initialize the variable: int a = 'a.

If you use multiple characters enclosed in single quotes to initialize a char variable. Char ch = 'abcd'; the C language does not explicitly define this behavior, and the compiler will give a warning but will not report an error. So we should not use this method. Let's test it.

int main(void){char ch_1 = 'a';char ch_2 = 'abcd';return 0;}

Use the VC compiler to check the compiled code.

Cl/FAsu/c test. the relevant Assembly Code generated by c is as follows: (this ('abdc ') is not standard, and the compiler will give a warning. In order to facilitate this and the following, no warning will be provided)

; 3    : char ch_1 = 'a';movBYTE PTR _ch_1$[ebp], 97; 00000061H; 4    : char ch_2 = 'abcd';movBYTE PTR _ch_2$[ebp], 100; 00000064H

No vc compiler can handle char ch_2 = 'abcd' only by taking the last character of 'abcd' (the 'D' ascii code value is 100,000 00064H, which is 100 of hexadecimal notation) as the value of ch_2.

Let's take a look at how GCC handles this situation.

The compilation code obtained by gcc-S-fverbose-asm test. c is as follows:

movb$97, -1(%ebp) #  ch_1movb$100, -2(%ebp) #  ch_2

It can be seen that GCC is the same as vc compiler in the case that such single quotes reference multiple characters.
Summary:

1. The between type and integer type can be converted to each other. In C language, the struct type is an integer.

2. The character nominal value is a single character enclosed by single quotes.

3. Do not put multiple characters in a pair of single quotes. C language does not define this situation and relies entirely on the compiler. From the experiment above, we can see that both the GCC and VC compilers only take the nominal value of the last character. It is not a string literal value (as described later ).

What if I use 'abc' to initialize int variables?

int main(void){char ch_1 = 'a';char ch_2 = 'abcd';int a = 'a';int b = 'ab';int c = 'abcd';return 0;}

The compilation code generated by cl/FAsu/c test. c is as follows:

; 3: Char ch_1 = 'a'; movbyte PTR _ ch_1 $ [EBP], 97; 00000061 H (note the comment 00000061 h here); 4: char CH_2 = 'abcd'; movbyte PTR _ CH_2 $ [EBP], 100; 00000064 h; 5: int A = 'a'; movdword PTR _ A $ [EBP], 97; 00000061 h; 6: int B = 'AB'; movdword PTR _ B $ [EBP], 24930; 00006162 h; 7: int c = 'abcd '; movdword PTR _ C $ [EBP], 1633837924; 61626364 H

Let's take a look at the processing situation of the GCC compiler. The compilation code obtained by GCC-S-fverbose-ASM test. C is as follows:

movb$97, -1(%ebp) #  ch_1movb$100, -2(%ebp) #  ch_2movl$97, -8(%ebp) #  amovl$24930, -12(%ebp) #  bmovl$1633837924, -16(%ebp) #  c

From the above results, we can see that the two compilers have done the same for situations where single quotes contain multiple characters: getting the desired value from the last character, for 'abcd ', char variables only take the last 'D', and INT variables take the ASCII code value of ABCD as its value.


If it is int A = 'abcde', the combination of their ASCII values has exceeded the int type (assuming it is in a 32-bit system. What will the compiler do? Is 'bdce 'supported or an error reported? Let's test:

Int main (void) {int ch_1 = 'bcde'; int CH_2 = 'abcde'; // compare the differences between ch_1 and CH_2. Return 0 ;}

First, the VC compiler, CL/fasu/C test. C, directly reports error c2015: the constant contains too many characters. It seems that VC compilation cannot let this happen.

In the case of the GCC compiler, there is a warning after gcc-S-fverbose-ASM test. C:

Test. C: 3: 20: Warning: Multi-character constant
Test. C: 4: 20: Warning: character constant too long for its type

But it still passed. The compilation code is as follows:

movl$1650680933, -4(%ebp) #  ch_1movl$1650680933, -8(%ebp) #  ch_2

It seems that the GCC compiler only needs bcde for 'abcde.

Therefore, add the following content after the above summary:

4. Different compilers handle different single quotes containing multiple characters like 'abcd'. Do not use this code. Although the character nominal value can be converted to the integer literal value, we should not rely on this behavior in the code.



Note: This is just a blog post from my dear brother. Seeing that he discussed the situation like 'abcd', he felt that this was unnecessary. But I need to know how to handle it during compilation. So I analyzed it.

This article is from the shimachao blog http://blog.csdn.net/shimachao/article/details/8379676.


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.