在一次采访中我被问到
使用
函数打印引号printf()
我不知所措。即使在他们的办公室里也有一台电脑,他们让我尝试一下。我尝试过这样的:
void main()
{
printf("Printing quotation mark " ");
}
但正如我怀疑的那样,它无法编译。当编译器得到第一个
"
时,它认为这是字符串的结尾,但事实并非如此。那么我怎样才能实现这个目标呢?
试试这个:
#include <stdio.h>
int main()
{
printf("Printing quotation mark \" ");
}
没有反斜杠,特殊字符就有天然的特殊含义。使用反斜杠,它们会按照出现的样子进行打印。
\ - 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
声明
printf(" \" ");
将打印报价给您。 您还可以打印这些特殊字符 , , , , , 和 前面有一个(斜杠)。
你必须转义引号:
printf("\"");
在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
除了转义字符之外,您还可以使用格式
%c
,并使用字符文字作为引号。
printf("And I quote, %cThis is a quote.%c\n", '"', '"');
你必须使用字符的转义。这是先有鸡还是先有蛋问题的解决方案:如果我需要它来终止字符串文字,我该如何编写“”?因此,C 创建者决定使用一个特殊字符来更改下一个字符的处理方式:
printf("this is a \"quoted string\"");
您还可以使用“\”来输入特殊符号,例如“ ", " ", " ", 输入'\'本身: "\" 等等。
这个也有效:
printf("%c\n", printf("Here, I print some double quotes: "));
但是如果您打算在面试中使用它,请确保您可以解释它的作用。
编辑:根据 Eric Postpischil 的评论,这是一个不依赖于 ASCII 的版本:
printf("%c\n", printf("%*s", '"', "Printing quotes: "));
输出不是那么好,而且它仍然不是 100% 可移植的(在某些假设的编码方案上会中断),但它应该可以在 EBCDIC 上工作。
#include<stdio.h>
int main(){
char ch='"';
printf("%c",ch);
return 0;
}
输出:“
你应该使用这样的转义字符:
printf("\"");
#include <stdio.h>
int main () {
printf("\"Hello World\"");
return 0;}
我想就是这样。顺便说一句,我刚刚开始学习编程。