Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include double-quote (") in C-string [duplicate]

Tags:

c

string

I would like a to define a variable string in C that contains the following set of characters: 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?

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

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

like image 200
Dragos Rizescu Avatar asked Dec 08 '13 20:12

Dragos Rizescu


People also ask

How can you include a double quote inside a double quoted string?

The basic double-quoted string is a series of characters surrounded by double quotes. If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters.

How do you write a double quote as a string literal?

To represent a double quotation mark in a string literal, use the escape sequence \". The single quotation mark (') can be represented without an escape sequence. The backslash (\) must be followed with a second backslash (\\) when it appears within a string.

How do you escape quotation marks in a string?

' You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.


2 Answers

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:

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

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

How does it add up to 67?

a-z 26
A-Z 26
0-9 10
'-_" 4
'\0' 1
    ---
    67
like image 178
Glenn Teitelbaum Avatar answered Oct 08 '22 20:10

Glenn Teitelbaum


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

like image 30
Bilal Syed Hussain Avatar answered Oct 08 '22 18:10

Bilal Syed Hussain