How can I get double quotes into a string literal?

kevthanewversi picture kevthanewversi · Sep 9, 2012 · Viewed 130.5k times · Source

I have the following output created using a printf() statement:

printf("She said time flies like an arrow, but fruit flies like a banana.");

but I want to put the actual quotation in double-quotes, so the output is

She said "time flies like an arrow, but fruit flies like a banana".

without interfering with the double quotes used to wrap the string literal in the printf() statement.

How can I do this?

Answer

Mark Byers picture Mark Byers · Sep 9, 2012

Escape the quotes with backslashes:

printf("She said \"time flies like an arrow, but fruit flies like a banana\"."); 

There are special escape characters that you can use in string literals, and these are denoted with a leading backslash.