In an interview I was asked
在一次采访中,有人问我
Print a quotation mark using the
printf()
function使用printf()函数打印一个引号
I was overwhelmed. Even in their office there was a computer and they told me to try it. I tried like this:
我是不知所措。甚至在他们的办公室里也有一台电脑,他们叫我去试试。我试着像这样:
void main()
{
printf("Printing quotation mark " ");
}
but as I suspected it doesn't compile. When the compiler gets the first "
it thinks it is the end of string, which is not. So how can I achieve this?
但正如我所怀疑的那样,它并不编译。当编译器得到第一个“它认为它是字符串的结束,而不是。那么我该如何做到这一点呢?
9 个解决方案
#1
22
Try this:
试试这个:
#include <stdio.h>
int main()
{
printf("Printing quotation mark \" ");
}
#2
16
Without a backslash, special characters have a natural special meaning. With a backslash they print as they appear.
没有反斜杠,特殊字符有自然的特殊含义。使用反斜杠,它们会在出现时打印出来。
\ - 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
The statement
该声明
printf(" \" ");
will print you the quotes. You can also print these special characters \a, \b, \f, \n, \r, \t and \v with a (slash) preceeding it.
将打印你的报价。你也可以打印这些特殊的字符a, \b, \f, \n, \r, \t和\v,在它之前加上(斜杠)。
#3
14
You have to escape the quotationmark:
你得避开引号:
printf("\"");
#4
7
Besides escaping the character, you can also use the format %c
, and use the character literal for a quotation mark.
除了转义字符之外,还可以使用格式%c,并将字符文字用作引号。
printf("And I quote, %cThis is a quote.%c\n", '"', '"');
#5
7
In C programming language, \
is used to print some of the special characters which has sepcial meaning in C. Those special characters are listed below
在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
#6
4
You have to use escaping of characters. It's a solution of this chicken-and-egg problem: how do I write a ", if I need it to terminate a string literal? So, the C creators decided to use a special character that changes treatment of the next char:
你必须使用转义字符。这是一个鸡和蛋的问题的解决方案:如果我需要它来终止字符串文字,我如何写一个" ?因此,C创作者决定使用一个特殊字符来改变下一个字符的处理方式:
printf("this is a \"quoted string\"");
Also you can use '\' to input special symbols like "\n", "\t", "\a", to input '\' itself: "\\" and so on.
你也可以使用“\”输入特别的符号,例如“\n”、“\t”、“\a”,输入“\”本身:“\”等等。
#7
1
This one also works:
这个也适用:
printf("%c\n", printf("Here, I print some double quotes: "));
But if you plan to use it in an interview, make sure you can explain what it does.
但如果你计划在面试中使用它,一定要解释它的作用。
EDIT: Following Eric Postpischil's comment, here's a version that doesn't rely on ASCII:
编辑:在Eric推迟评论之后,这里有一个不依赖ASCII的版本:
printf("%c\n", printf("%*s", '"', "Printing quotes: "));
The output isn't as nice, and it still isn't 100% portable (will break on some hypothetical encoding schemes), but it should work on EBCDIC.
输出不是很好,而且也不是100%可移植性的(会破坏一些假设的编码方案),但是它应该可以在EBCDIC上工作。
#8
0
#include<stdio.h>
int main(){
char ch='"';
printf("%c",ch);
return 0;
}
Output: "
输出:“
#9
-1
you should use as: printf("\"");
您应该使用as: printf(“\”);
#1
22
Try this:
试试这个:
#include <stdio.h>
int main()
{
printf("Printing quotation mark \" ");
}
#2
16
Without a backslash, special characters have a natural special meaning. With a backslash they print as they appear.
没有反斜杠,特殊字符有自然的特殊含义。使用反斜杠,它们会在出现时打印出来。
\ - 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
The statement
该声明
printf(" \" ");
will print you the quotes. You can also print these special characters \a, \b, \f, \n, \r, \t and \v with a (slash) preceeding it.
将打印你的报价。你也可以打印这些特殊的字符a, \b, \f, \n, \r, \t和\v,在它之前加上(斜杠)。
#3
14
You have to escape the quotationmark:
你得避开引号:
printf("\"");
#4
7
Besides escaping the character, you can also use the format %c
, and use the character literal for a quotation mark.
除了转义字符之外,还可以使用格式%c,并将字符文字用作引号。
printf("And I quote, %cThis is a quote.%c\n", '"', '"');
#5
7
In C programming language, \
is used to print some of the special characters which has sepcial meaning in C. Those special characters are listed below
在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
#6
4
You have to use escaping of characters. It's a solution of this chicken-and-egg problem: how do I write a ", if I need it to terminate a string literal? So, the C creators decided to use a special character that changes treatment of the next char:
你必须使用转义字符。这是一个鸡和蛋的问题的解决方案:如果我需要它来终止字符串文字,我如何写一个" ?因此,C创作者决定使用一个特殊字符来改变下一个字符的处理方式:
printf("this is a \"quoted string\"");
Also you can use '\' to input special symbols like "\n", "\t", "\a", to input '\' itself: "\\" and so on.
你也可以使用“\”输入特别的符号,例如“\n”、“\t”、“\a”,输入“\”本身:“\”等等。
#7
1
This one also works:
这个也适用:
printf("%c\n", printf("Here, I print some double quotes: "));
But if you plan to use it in an interview, make sure you can explain what it does.
但如果你计划在面试中使用它,一定要解释它的作用。
EDIT: Following Eric Postpischil's comment, here's a version that doesn't rely on ASCII:
编辑:在Eric推迟评论之后,这里有一个不依赖ASCII的版本:
printf("%c\n", printf("%*s", '"', "Printing quotes: "));
The output isn't as nice, and it still isn't 100% portable (will break on some hypothetical encoding schemes), but it should work on EBCDIC.
输出不是很好,而且也不是100%可移植性的(会破坏一些假设的编码方案),但是它应该可以在EBCDIC上工作。
#8
0
#include<stdio.h>
int main(){
char ch='"';
printf("%c",ch);
return 0;
}
Output: "
输出:“
#9
-1
you should use as: printf("\"");
您应该使用as: printf(“\”);