c - 需要有关 c 中 printf() 的帮助

标签 c printf

预测以下程序的输出。

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

答案是“测试%C%程序”

为什么?我明白 %% 意味着它将打印 % ,但是 printf 中的 "\"\"东西怎么样?

最佳答案

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

"Taha Paksu was here"

但是如果您需要在字符串中使用引号怎么办?喜欢:

""Taha Paksu" was here" (you can see that the code highlighter is confused too)

那么编译器就会对字符串的开头和结尾感到困惑。

为了防止这种情况发生,存在转义序列。要在引号分隔的字符串中写入引号,您需要先对其进行转义。喜欢

"\"Taha Paksu\" was here"

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

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

如果想以字符串形式输出\;您还需要使用 \\

对其进行转义

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

关于c - 需要有关 c 中 printf() 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48208739/

相关文章:

c++ - OpenGL 中的全光灯?

int 的 C 命令行参数

c - 无法在动态链接库中找到过程入口点axiom_attribute_create

c - 通过头文件使用时 C 中源文件的奇怪输出

c - c中printf中的多个赋值语句

c - 如何分割一个int

c - "static"在 C 中是什么意思?`

c - 为什么在引用 int 变量的大小时必须使用 %ld?

C幂函数和scanf

c - printf 类型提升和符号扩展