A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
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 denote 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 (\) that represents 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 causethe display of the PC is replaced by a page. A newline character (\ n) causes the active position to jump to the beginning of the next line. The carriage return ( \ r ) returns the active position to the beginning of the current line. The horizontal tab (\ t) moves the active position several positions (usually 8). Vertical tab (\v) changes the active position to several lines. Note: The\v can be used to control the printer to change several lines, but will not cause the PC's display to wrap.
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 ';
the ones that begin 0 can be omitted, that is, written as ' \07 ' or ' \7 ' are the same. No matter if there is a heading of 0 ,7 will be treated as octal number.
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 hexadecimal digits. For example:
nl = ' \xa ';
Note: useWhen ASCII code, 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 ' \ n ' and ' \a 'as much as possible, Instead of ' \xa ' and ' \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. (Think about it: why not 2
Escape characters in the C language
Start building with 50+ products and up to 12 months usage for Elastic Compute Service