Hamza Azam
Hamza Azam

Reputation: 17

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.

i.e like this:

printf("  \" \"  ");

or like this:

printf("  \"  " ");

which one is correct?

I need to print (“Whatever!” He said “The Sparrows are flying again.”)

Upvotes: 0

Views: 3238

Answers (4)

This is a double-quote as recognized by the C compiler: ". None of the following are: “”. See the difference?

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");

Upvotes: 0

CIsForCookies
CIsForCookies

Reputation: 12837

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

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"

Upvotes: 0

durbnpoisn
durbnpoisn

Reputation: 4669

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.\”");

Upvotes: 0

Scott Hunter
Scott Hunter

Reputation: 49920

For each double-quote you wish to appear within the string, you need both the backslash and the double quote.

Upvotes: 1

Related Questions