C escape characters in the language
In a character set, there is a class of characters that can be displayed on the display when the character is entered from the keyboard, that is, what is displayed when the input is entered. Such characters are called display characters, such as a, B, C, $, +, and space characters, and so on.
Another type of character does not have this feature. They either cannot find a corresponding key on the keyboard (which can, of course, be entered in a special way), or the characters on the key face cannot be displayed after the key is pressed. In fact, this kind of character is designed for controlling function, so it is called control character.
In the C language, the control character that constitutes a character constant must be represented by an escape character. An escape character is a character that begins with "\". For example backspace with ' \b ', the newline character is denoted by ' \ n '. The ' \ ' in the escape character indicates that the character behind it has lost its original meaning and is transformed into another specific meaning. A backslash is a specific character along with its subsequent characters.
An escape character is a special form of a character that is represented in the C language. The escape character begins with a backslash ' \ ' followed by a character or an octal or hexadecimal number. An escape character is called an escape character, which has a specific meaning that differs from the original meaning of the character.
Typically, an escape character is used to represent a non-printable control character in the ASCII character set and a character of a particular function, such as a single apostrophe (\ ') used to denote a character constant, a double-apostrophe (\ ") and a backslash (\) for a string constant.
For example, the following example, in the format string of the printf function, is an escape character, meaning "carriage return". Escape characters are primarily used to denote control codes that are not easily represented by ordinary characters.
Commonly used escape characters and their meanings
\ddd three-bit octal
\XHH two-bit hex
Ascaii Code Range Table
The number 0---9 means: 48--57
Capital Letter: 90---
Small Letter: 122---
NUL null character CR return to LF line break
As you can see from the table, there are three escape characters in the C language: General escape characters, octal escape characters, and hexadecimal escape characters.
\b、\f、\ n、\ r、\ tAnd\vare output device control characters. Backspace\b) to move the active position of the current row back one position. Page Break (\f) to jump the active position to the beginning of the next page. Note: The page break can be used to control the printer page break, but does not causePCThe display of the machine is replaced by a page. Line Break (\ nThe active position jumps to the beginning of the next line. Carriage return character (\ r) to return the active position to the beginning of the current line. Horizontal tab (\ t) to move the active position in several locations (typically8One). Vertical tab (\v) To change the active position to several lines. Note:\vCan be used to control the printer to change several lines, but does not causePCThe display line of the machine is changed.
If you want to print the following sentence: "\ is called ' backslash '."
We need to use the following statement:
printf ("\" \ \ is called \ 'backslash\ '. \ "); or
printf ("\" \ \ is called 'backslash'. \ ");
Note that in string literals, the output is the same regardless of the write \ ' or '. However, when assigning a value to a character variable, be sure to write \ '. For example:
char ch = ";
char ch = '\ ';
\0oo and \xhh are two special representations of ASCII code. If you want to represent a character in octal ASCII , you can precede the octal number with \ and enclose it in single quotation marks. For example:
beep = ' \007 ';
those that begin with 0 can be omitted, that is to say , written as or ' \7 ' Are the same. Whether there is no beginning 0 &NBSP;, 7 will be treated as octal numbers.
starting with C89 ,C provides a way to denote character constants in hexadecimal: Write an x behind the backslash, and then write 1 to 3 A hexadecimal number. For example:
nl = ' \xa ';
Note: useASCIICode, be careful to distinguishDigitalAndNumeric characters。 For example:character4of theASCIICode is the,' 4 'representative character4, rather than numbers4. In addition, although' \ n 'and the' \xa ',' \a 'and the' \007 'is equivalent, but we should use it whenever possible.' \ n 'and the' \a ', but not with' \xa 'and the' \007 '. This is because the former is easy to understand, easy to remember, and highly portable.
1. General Escape characters
This escape character, although formally composed of two characters, represents only one character. Commonly used general escape characters are:
\a \ t \v \b \ r \f \ \ \ "
The attentive reader may have found that the escape character ' \ \ ' represents the backslash "\", the escape character ' \ ' represents the character "'" and the escape character ' \ ' represents the character "" ", which is itself a display character, and why should it be escaped?
This is because their original character form has been used for it, where single quotation marks are used as parentheses to differentiate character constants, and double quotes are used as parentheses to differentiate strings (which are described below), and backslashes themselves are used to represent the beginning of escaped characters, so they must be re-declared with escape characters.
2. Eight binary escape characters
It is a sequence of characters consisting of a backslash ' \ ' and subsequent octal digits. For example, ' \60 ', ' \101 ', ' \141 ' represent characters ' 0 ', ' a ' and ' a ' respectively. Because the octal values of ASCII codes for characters ' 0 ', ' a ' and ' a ' are 60, 101, and 141, respectively.
All characters in a character set can be represented by octal escape characters.
If you prefer, you can precede the octal digits with a number of eight to indicate the octet transfer character.
3.16 Binary escape characters
It is a sequence of characters consisting of a backslash ' \ ' and a letter x (or x) and then one or two hexadecimal digits. For example, ' \x30 ', ' \x41 ', ' \x61 ' represent characters ' 0 ', ' a ' and ' a ' respectively. Because the hexadecimal values of the ASCII codes for the characters ' 0 ', ' a ' and ' a ' are 0x30, 0x41, and 0x61, respectively.
Visible, all characters in a character set can be represented by hexadecimal escape characters.
From the above, the octal escape character and the hexadecimal escape character can be used to represent not only control characters, but also characters that can be displayed. However, because the character set used on different computer systems may be different, in order to make the program can be easily ported to other computer systems to run, the program should be less in this form of escape characters.
Use of escape characters
void Main ()
a=5; b=6; c=7;
printf ("%d\n\t%d%d\n%d%d\t\b%d\n", a,b,c,a,b,c);
This program exercises the use of escape characters:
A, B, and C are integers, respectively 5,6,7
Call printf to display the program run result:
The program is "\ n" after the first column outputs a value of 5. So the carriage return to the line, then again "\ t", then jump to the next tab position (Set tab position interval is 8), then output B value 6 and C value 7, and then "\ n", and then return to the line, and then output a value of 5 and B value 6; Jumps to the next tab position (6 aligned to the previous line), but the next escape character "\b" returns one box, so it's next to 6 and then output C value 7.
The following issues need to be noted when using escape characters:
1) Only lowercase letters can be used in escape characters, and each escape character can only be treated as one character.
2) \v Vertical tabs and \f page breaks have no effect on the screen, but affect the printer's response actions.
3) in C programs, when non-printable characters are used, they are usually represented by escape characters.
4) The escape character ' \ s ' means null, and its value is 0. The ASCII code value of the character ' 0 ' is 48. Therefore, the null character ' \ s ' is not a character 0. Additionally, the null character is not equal to the space character, and the ASCII value of the space character is 32 instead of 0. When compiling procedures, the reader should distinguish clearly.
5) If the character after the backslash and it does not form an escape character, then ' \ ' does not escape the function will be ignored.
6) An escape character can also appear in a string, but only as a character.
Example to find the length of the following string
"\026[12,m" Length is 6
The "\0mn" length is 0.
Escape characters in the C language