C语言 在 C 字符串中包含双引号 (")

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20458489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:28:46  来源:igfitidea点击:

Include double-quote (") in C-string

cstring

提问by Dragos Rizescu

I would like a to define a variable string in C that contains the following set of characters: a-zA-Z0-9'-_”.

我想在 C 中定义一个包含以下字符集的变量字符串:a-zA-Z0-9'-_”.

Therefore I would do it like this:

因此,我会这样做:

char str[64] = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789'-_""

As you can see the problem is at the end with "character.

如您所见,问题在"字符的末尾。

Question 1:How can I work around that?

问题 1:我该如何解决这个问题?

Question 2:Is there a better way than my way to define a string like that?

问题 2:有没有比我定义这样的字符串更好的方法?

PS:I didn't really know how to title my question, so if you got a better one, please edit it.

PS:我真的不知道如何命名我的问题,所以如果你有更好的,请编辑它。

回答by Glenn Teitelbaum

use the backslash: "\""is a string containing "

使用反斜杠:"\""是一个包含"

like this:

像这样:

char str[67] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'-_\"";

added one for the implicit '\0' at the end (and put in the missing vV) - this could also be:

最后为隐式 '\0' 添加了一个(并放入丢失的 vV) - 这也可能是:

char str[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'-_\"";

and let the compiler count for you - then you can get the count with sizeof(str);

并让编译器为您计数 - 然后您可以使用sizeof(str);

How does it add up to 67?

它如何加起来为 67?

a-z 26
A-Z 26
0-9 10
'-_" 4
'##代码##' 1
    ---
    67

回答by Bilal Syed Hussain

Use "\""(backslash") for putting "in a string

使用"\""(backslash") 放入"字符串