Search code examples
c++cstringescapingstring-literals

How can I get double quotes into a string literal?


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?


Solution

  • 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.