简体   繁体   English

C语言中的双引号

[英]Double quotes in C language

Whenever we need to print double quotes using backslash (escape character) do we have to use it twice while opening the quotes and closing it or just once for both. 每当我们需要使用反斜杠(转义字符)打印双引号时,在打开引号和关闭引号时都必须使用两次,或者对两者都一次。

ie like this: 即像这样:

printf("  \" \"  ");

or like this: 或像这样:

printf("  \"  " ");

which one is correct? 哪一个是正确的?

I need to print ( “Whatever!” He said “The Sparrows are flying again.” ) 我需要打印( “Whatever!” He said “The Sparrows are flying again.”

对于希望出现在字符串中的每个双引号,都需要反斜杠和双引号。

You escape whatever quote you wish to be ignored as code and used as string. 您可以转义希望忽略的任何引号,将其作为代码并用作字符串。 So in your case it would look like: 因此,在您的情况下,它看起来像:

printThis(“\"Whatever!\” He said \“The Sparrows are flying again.\”");

the backslash is what allows you to use double quotes and other special charachters. 反斜杠使您可以使用双引号和其他特殊字符。 For each one, you should use a single backslash: 对于每个,都应使用一个反斜杠:

printf("\"") --> "
printf("\'") --> '
printf("\\") --> \

and so on 等等

so printf("\\“Whatever!\\” He said \\“The Sparrows are flying again.\\”") would give you your desired output 所以printf("\\“Whatever!\\” He said \\“The Sparrows are flying again.\\”")会给你想要的输出

EDIT: As Chux mentioned, is no regular double quotes " and in this case there is no need for a backslash and printf("“Whatever!\\” He said \\“The Sparrows are flying again.”") would suffice. BUT , using the backslash would not cause a misbehaviour even though the double quotes are "smart quotes" 编辑:作为Chux提到, 没有定期双引号"在这种情况下没有必要对一个反斜杠和printf("“Whatever!\\” He said \\“The Sparrows are flying again.”")就足够了BUT ,即使双引号是“智能引号”,使用反斜杠也不会导致错误行为

This is a double-quote as recognized by the C compiler: " . None of the following are: “” . See the difference? 这是C编译器认可的双引号: " 。以下都不是: “” 。看到区别了吗?

Thus, your code should read, simply: 因此,您的代码应简单地读为:

printf("“Whatever!” He said “The Sparrows are flying again.”\n");

If you don't want to use the "smart" quotes, you'll need to change to regular quotes and escape all of them: 如果您不想使用“智能”引号,则需要更改为常规引号并转义所有引号:

printf("\"Whatever!\" He said \"The Sparrows are flying again.\"\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM