Escape characters in C Language
A character set has the following characteristics: When you enter this character on the keyboard, the display displays this character, that is, what you enter. These characters are calledPrintable charactersSuch as A, B, C, $, +, and space characters.
Another type of character does not have this feature. Either they cannot find a corresponding key on the keyboard (you can enter it in a special way), or they cannot display the characters on the key surface after the key is pressed. In fact, these types of characters are designed for control purposes.Control characters.
In C,Control characters that constitute character constants must be represented by escape characters. An escape character starts. For example, the escape character is represented by '\ B', And the linefeed is represented by '\ n.'\'Indicates that the character after it has lost its original meaning and is converted into another specific meaning.A backslash is a specific character along with the character following it.
Escape characters are a special form of characters in C. Escape characters with backslashes'\', Followed by a character or an octal or hexadecimal number. The escape character has a specific meaning. It is called an escape character because it is different from the original meaning of the character.
Generally, escape characters are used to indicate non-printable control characters in the ASCII character set and special function characters. For example, a single marker (') is used to represent a character constant ('), used to represent the double apostrophes (") and backslash (\. For example,' \ N' is an escape character, which means "carriage return ". Escape characters are mainly used to indicate the control that is not easily expressed by general characters.Code. The following table showsCommon escape characters and their meanings:
||ASCII value (decimal)
||Return to BS to move the current position to the previous column.
||Change page (ff), move the current position to the beginning of the next page
||Line feed (LF), move the current position to the beginning of the next line
||Press enter (CR) to move the current position to the beginning of the line
||Horizontal tabulation (HT) (jump to the next tab)
||Vertical tabulation (VT)
||Represents a backslash character ''\'
||Represents a single quotation mark (apostrophes) character
||Represents a double quotation mark character
||Any character from 1 to 3 octal digits
||Three octal nodes
||Any character in hexadecimal notation between 1 and 2
It can be seen from the table that there are three escape characters in the C language: General escape characters, octal escape characters and hexadecimal escape characters.
General escape characters
This escape character, although formally composed of two characters, only represents one character. The common escape characters are:
Careful readers may have discovered that the escape character'\/'Indicates the Backslash "/" and escape character'\''Represents the character" '"And escape character'\"'Represents the character" ", which is itself a printable character. Why should we escape it?
This is because their original character form has been used for it, where single quotation marks are used to distinguish character constants and double quotation marks are used to distinguish strings (strings will be introduced below, the backslash itself is used to indicate the beginning of the escape character, so it must be declared again with the escape character.
Octal escape characters
It consists of the backslash '/' and the subsequent 1 ~ A string of three Octal numbers. For example ,'\60 ','\101 ','\141 characters: '0', 'A', and 'A '. Because the ASCII values of '0', 'A', and 'A' are 60, 101, and 141, respectively. All characters in the character set can be represented by octal escape characters. If you want to, you can add 0 in front of the octal number to indicate the octal transfer character.
Hexadecimal escape characters
It consists of the backslash '/' and the letter X (or X) and the subsequent 1 ~ A string of two hexadecimal numbers. For example ,'\X30 ','\X41 ','\The x61 characters are '0', 'A', and 'A' respectively '. The hexadecimal values of the ASCII codes '0', 'A', and 'A' are 0x30, 0x41, and 0x61, respectively.
Note the following when using escape characters:
It can be seen that all characters in the character set can be expressed by hexadecimal escape characters.
We can see that the octal Escape Character and hexadecimal escape character can not only represent control characters, but also can also represent display characters. However, different computer systems may use different character sets.ProgramIt can be easily transplanted to other computer systems for running, and the program should use less escape characters in this form.
- 1) only lowercase letters can be used in escape characters. Each escape character can only be considered as one character.
- 2) The \ v vertical tabulation and the \ f tab have no impact on the screen, but will affect the printer's response.
- 3) when non-printable characters are used in C Programs, escape characters are usually used.
- 4) Escape Character '\ 0' indicates null, and its value is 0. The ASCII value of the character '0' is 48. Therefore, the null character '\ 0' is not the character 0. In addition, the null character is not equal to the space character. The ASCII value of the space character is 32 rather than 0. Readers should be clear about the differences during programming.
- 5) if the character after the backslash does not constitute an escape character, '\' cannot escape and will be ignored.