需要关于c中printf()的帮助

Ayu*_*lia -4 c printf

预测以下程序的输出.

#include <stdio.h>
int main()
{
printf(" \"TEST %% C %% PROGRAM\"");
return 0;
}
Run Code Online (Sandbox Code Playgroud)

答案是"TEST%C%PROGRAM"

为什么?我理解%%意味着它会打印%,但是printf中的"\"\"是什么意思?

Tah*_*ksu 6

您需要使用字符串分隔符来定义字符串的开头和结尾

"Taha Paksu was here"
Run Code Online (Sandbox Code Playgroud)

但是如果你需要在字符串中使用引号呢?喜欢:

""Taha Paksu" was here" (you can see that the code highlighter is confused too)
Run Code Online (Sandbox Code Playgroud)

然后编译器会在字符串开始和结束的任何地方被混淆.

为了防止这种情况,存在转义序列.要在引号分隔的字符串中编写引号,您需要先将其转义.喜欢

"\"Taha Paksu\" was here"
Run Code Online (Sandbox Code Playgroud)

\字符用于描述转义序列,如:

Escape sequence Hex value in ASCII  Character represented
\a  07  Alert (Beep, Bell) (added in C89)[1]
\b  08  Backspace
\f  0C  Formfeed
\n  0A  Newline (Line Feed); see notes below
\r  0D  Carriage Return
\t  09  Horizontal Tab
\v  0B  Vertical Tab
\\  5C  Backslash
\'  27  Single quotation mark
\"  22  Double quotation mark
\?  3F  Question mark (used to avoid trigraphs)
\nnn       note 1 any The byte whose numerical value is given by nnn interpreted as an octal number
\xhh…      any  The byte whose numerical value is given by hh… interpreted as a hexadecimal number
\e         note 2 1B    escape character (some character sets)
\Uhhhhhhhh note 3 none  Unicode code point where h is a hexadecimal digit
\uhhhh     note 4 none  Unicode code point below 10000 hexadecimal
Run Code Online (Sandbox Code Playgroud)

如果你想输出\一个字符串; 你还需要逃避它\\

表格取自:https://en.wikipedia.org/wiki/Escape_sequences_in_C