如何在C中打印引号?

Mis*_*u4u 20 c printf

在接受采访时我被问到了

使用该printf()功能打印引号

我不堪重负.即使在他们的办公室里也有一台电脑,他们告诉我试一试.我试过这样的:

void main()
{
    printf("Printing quotation mark " ");
}
Run Code Online (Sandbox Code Playgroud)

但我怀疑它不编译.当编译器得到第一个"它认为它是字符串的结尾时,它不是.那我该怎么做呢?

Sun*_*lev 25

试试这个:

#include <stdio.h>

int main()
{
  printf("Printing quotation mark \" ");
}
Run Code Online (Sandbox Code Playgroud)


Ang*_*gus 16

没有反斜杠,特殊字符具有自然的特殊含义.使用反斜杠,它们会在出现时打印.

\   -   escape the next character
"   -   start or end of string
’   -   start or end a character constant
%   -   start a format specification
\\  -   print a backslash
\"  -   print a double quote
\’  -   print a single quote
%%  -   print a percent sign
Run Code Online (Sandbox Code Playgroud)

该声明

printf("  \"  "); 
Run Code Online (Sandbox Code Playgroud)

会打印出报价.您还可以打印这些特殊字符\ a,\ b,\ f,\n,\ r,\ t和\ v,并在其前面加上(斜杠).


hal*_*lex 14

你必须逃避引号:

printf("\"");
Run Code Online (Sandbox Code Playgroud)


jxh*_*jxh 8

除了转义字符外,您还可以使用该格式%c,并使用字符文字作为引号.

printf("And I quote, %cThis is a quote.%c\n", '"', '"');
Run Code Online (Sandbox Code Playgroud)


ras*_*hok 8

在C编程语言中,\用于打印一些在C中具有特殊含义的特殊字符.下面列出了这些特殊字符

\\ - Backslash
\' - Single Quotation Mark
\" - Double Quatation Mark
\n - New line
\r - Carriage Return
\t - Horizontal Tab
\b - Backspace
\f - Formfeed
\a - Bell(beep) sound
Run Code Online (Sandbox Code Playgroud)


Dmy*_*nko 5

您必须使用字符转义。这是先有鸡还是先有蛋问题的解决方案:如果我需要它来终止字符串文字,我该如何编写“”?因此,C 创建者决定使用一个特殊字符来更改下一个字符的处理:

printf("this is a \"quoted string\"");
Run Code Online (Sandbox Code Playgroud)

还可以使用“\”输入“\n”、“\t”、“\a”等特殊符号,输入“\”本身:“\\”等。