Character data for C language

Source: Internet
Author: User
Tags character set constant

2.5.1 Characters Constants Quantity

Word constants is a character enclosed in single quotes. such as ' A ', ' 9 ', '! '. Single quotation marks in the constants of characters do not represent the characters themselves. The characters in single quotes cannot be single quotes (') and backslashes (\), and their special representations are described in the escape character.

In the CC language, characters are stored according to their corresponding ASCII code values, and one character occupies one byte. For example: Character ASCII code value (decimal)!33048149957a65b66a97b98

Note the difference between the character ' 9 ' and the number 9, which is the character constants, which is an integer constant, and their meaning is different from how they are stored in the computer.

Because the character constants in C is stored as an integer (short), the constants amount of the word can be used as an integer to participate in the relevant operation in the program. For example:

'a'-32;/*执行结果97-32=65*/
'A'+32;/*执行结果65+32=97*/
'9'-9;/*执行结果57-9=48*/

2.5.2 String Constants

A string constant is a string of characters enclosed in a pair of double quotes. Double quotes are only bounded, and the strings enclosed in double quotes cannot be double quotes (") and backslashes (\), and their special representations are described in the escape character. For example: "The", "Cprogram", "Yes&no", "33312-2341", "A" and so on. In C, when a string constant is stored in memory, the system automatically adds a "string end flag" to the end of the string, that is, a character null with an ASCII value of 0, which is usually represented by a. Therefore, in the program, the length of the string constant of n characters, in memory occupies n+1 bytes of storage space. For example, the string China has 5 characters, as the string constant "China" is stored in memory, a total of 6 bytes, the system automatically followed by a null character, which is stored in the form:

To pay special attention to the difference between string and string constants, the storage properties are not the same except for the representations, and the character ' a ' occupies only 1 bytes, while the string constant "a" is 2 bytes.

2.5.3 Escape character

An escape character is a special form of a character that is represented in the C language. An escape character is typically used to denote a character that is not printable in the ASCII character set, such as a single apostrophe (') used to represent the constants quantity of a character, a double apostrophe (") and a backslash (\), and so on, for a string constant. The escape character is represented by a backslash \ followed by a character or a octal or hexadecimal number. Table 2-4 shows the commonly used escape characters in C language.

When you use double quotes and backslashes in the amount of constants in a single quotation mark and a backslash, and in the character constants quantity, you must use an escape character to denote that the character is preceded by a backslash.

Using the escape character \ddd or \xhh in a C program can easily and flexibly represent any character. The \DDD is followed by the three-bit octal number, and the value of the three-bit octal number is the corresponding octal ASCII code value. \x is followed by a two-bit hexadecimal number, which is the hexadecimal ASCII value of the corresponding character for the two-bit hexadecimal number.

You need to be aware of the following issues when using escape characters:

1 The escape character can only be used in lowercase letters, and each escape characters can only be treated as one character.

2 \v Vertical tabulation and \f page breaks have no effect on the screen, but affect the printer's response action.

3 in C programs, when using nonprinting characters, it is usually represented by an escape character.

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.