Cprogramming 简明教程
Escape Sequence in C
Escape Sequence in C
escape sequence in C 是由单引号中包含多个字符组成的 literal 。通常,字符文本仅由单引号中的单个字符组成。但是,转义序列为反斜杠字符后面出现的字符附加了特殊含义( \ )。
An escape sequence in C is a literal made up of more than one character put inside single quotes. Normally, a character literal consists of only a single character inside single quotes. However, the escape sequence attaches a special meaning to the character that appears after a backslash character (\).
The \ symbol causes the compiler to escape out of the string and provide meaning attached to the character following it.
以 \n 为例。当放在字符串中时,\n充当换行符,产生按 Enter 键的效果。以下语句 -
Look at \n as an example. When put inside a string, the \n acts as a newline character, generating the effect of pressing the Enter key. The following statement −
printf(" Hello \n World ");
将生成此输出 -
Will produce this output −
Hello
World
新行是一个不可打印的字符。 \n 转义序列可用于生成其效果。类似地,转义序列 \t 等同于在键盘上按 Tab 键。
The new line is an unprintable character. The \n escape sequence is useful to generate its effect. Similarly, the escape sequence \t is equivalent to pressing the Tab key on the keyboard.
escape sequence 是一系列字符,在字符或字符串文本中使用时不能表示自身,但会被翻译成另一个字符或一系列字符,这些字符可能很难或无法直接表示。
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.
All Escape Sequences in C
在 C 中,所有 escape sequences 都由两个或多个字符组成,其第一个字符是反斜杠 \ (称为“Escape 字符”);其余字符按照下表解释转义序列。
In C, all escape sequences consist of two or more characters, the first of which is the backslash \ (called the "Escape character"); the remaining characters have an interpretation of the escape sequence as per the following table.
以下是 C 中可用的转义序列列表 -
Here is a list of escape sequences available in C −
Escape sequence |
Meaning |
|\ character |
\' |
' character |
\" |
" character |
\? |
? character |
\a |
Alert or bell |
\b |
Backspace |
\f |
Form feed |
\n |
Newline |
\r |
Carriage return |
\t |
Horizontal tab |
\v |
Vertical tab |
\ooo |
Octal number of one to three digits |
\xhh . . . |
让我们借助一组示例了解这些转义序列如何工作。
Let us understand how these escape sequences work with the help of a set of examples.
Newline Escape Sequence (\n)
由 C 中的转义序列 \n 表示的新行字符用于在输出屏幕上插入回车效果。您可以使用此转义序列在单独的行中打印文本并提高输出的可读性。
The newline character, represented by the escape sequence \n in C, is used to insert the effect of carriage return on the output screen. You would use this escape sequence to print text in separate lines and improve the readability of output.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
printf("Hello.\nGood morning.\nMy name is Ravi");
}
运行此代码,您将获得以下输出−
On running this code, you will get the following output −
Hello.
Good morning.
My name is Ravi
Tab Escape Sequence (\t)
制表符 (\t) 表示键盘上的 Tab 键。当在字符串中遇到制表符时,光标会移至下一个水平制表位。水平制表位通常以八个字符的间隔设置。
The tab character (\t) represents the Tab key on the keyboard. When the tab character is encountered in a string, it causes the cursor to move to the next horizontal tab stop. Horizontal tab stops are usually set at intervals of eight characters.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
printf("Name:\tRavi\tMarks:\t50");
}
运行代码并检查其输出:
Run the code and check its output −
Name: Ravi Marks: 50
Backslash Escape Sequence (\\)
要将反斜杠字符本身添加为字符串的一部分,它必须以另一个反斜杠开头。第一个反斜杠转义出字符串,第二个反斜杠产生效果。
To add backslash character itself as a part of a string, it must precede by another backslash. First backslash escapes out of the string, and the second one takes the effect.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
printf("Directory in Windows: C:\\users\\user");
}
运行此代码,您将获得以下输出−
On running this code, you will get the following output −
Directory in Windows: C:\users\user
Double and Single Quotes Escape Sequences (\" and \')
这些字符在 C 中具有特殊含义,因为“和”分别用于表示字符文本和字符串文本。因此,要将这些字符视为字符串的一部分,必须使用附加的反斜杠对它们进行转义。
These characters have a special meaning in C since " and ' symbols are used for the representation of a character literal and a string literal respectively. Hence, to treat these characters as a part of the string, they must be escaped with an additional backslash preceding them.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
printf("Welcome to \"TutorialsPoint\"\n");
printf ("\'Welcome\' to TutorialsPoint");
}
运行代码并检查其输出:
Run the code and check its output −
Welcome to "TutorialsPoint"
'Welcome' to TutorialsPoint
Backspace Escape Sequence (\b)
转义序列“\b”表示退格字符。它用于擦除已在屏幕上打印的字符或文本的特定部分。
The escape sequence "\b", represents the backspace character. It is used erase a character or a specific portion of a text that has already been printed on the screen.
检查以下示例代码 -
Check the following example code −
#include <stdio.h>
int main(){
printf("Welcome to\b TutorialsPoint");
}
运行代码并检查其输出:
Run the code and check its output −
Welcome t TutorialsPoint
请注意: o 已从 to 中删除。
Note that o from to has been erased.
C 还有 \r 转义序列。换行转义序列 (\n) 将光标移动到下一行的开头,而回车转义序列 (\r) 将光标移动到当前行的开头。
C also has a \r escape sequence. The newline escape sequence (\n) moves the cursor to the beginning of the next line, while the carriage return escape sequence (\r) moves the cursor to the beginning of the current line.
Octal Number Escape Sequence (\ooo)
此转义序列用于一位到三位八进制数。八进制转义序列是反斜杠后面跟一个、两个或三个八进制数字 (0-7)。它使用这些数字指定的值匹配目标序列中的一个字符。
This escape sequence is used for Octal numbers of one to three digits. An octal escape sequence is a backslash followed by one, two, or three octal digits (0-7). It matches a character in the target sequence with the value specified by those digits.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
printf("%c", '\141');
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
a
Hexadecimal Number Escape Sequence (\xhh)
十六进制转义序列是反斜杠后面跟字母“x”,再后面跟两个十六进制数字 (0-9a-fA-F)。它使用这两个数字指定的值匹配目标序列中的一个字符。
A hexadecimal escape sequence is a backslash followed by the letter "x" followed by two hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the two digits.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
printf("%c", '\x41');
return 0;
}
在这里,您将获得以下输出 −
Here, you will get the following output −
A
Alert or Bell Number Escape Sequence (\a)
转义序列 \a 表示警报或铃声字符。执行时,它会产生声音或视觉警报,具体取决于所使用的终端或控制台。
The escape sequence \a represents the alert or bell character. When executed, it produces a sound or visual alert depending on the terminal or console being used.
请看以下示例:
Take a look at the following example −
#include <stdio.h>
int main(){
printf("Hello \a world\n");
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
Hello world
转义序列广泛应用于许多其他编程语言,如 Java、PHP、C# 等。
Escape sequences are used widely in many other programming languages such as Java, PHP, C#, etc.