How to Print Double Quotes(“”) in C?

In C programming language, double quotes(“”) are used to define a string literal. The double quotes tell the compiler that everything between them is a part of the string.

For example, the text “Hello world” in the below program is a string:

#include <stdio.h>

int main() {
    // Print the string
    printf("Hello world");
    return 0;
}

Output:

Hello world

However, if you want to print the double quotes(“”) as a part of the text, for example, Hello" world or "Hello" world, the compiler gets confused.

It thinks that the double quote is the end of the string, therefore, the remaining part of the string becomes unknown to it and therefore you get a compilation error.

For example, the following program will give you a compilation error because the compiler is looking for the end of the string but it can’t find it:

#include <stdio.h>

int main() {
    // Try to print the double quote(") directly
    printf("Hello" world"); // Will throw error
    
    return 0;
}

Output:

[Error] expected ')' before 'world'

So, what is the fix for it?

In C programming, if you want to print a double quote in a string, you have to put a backslash(\) in front of it. The backslash(\) tells the compiler to treat the double quote as a regular character and not an end of the string.

In C programming, \" is called an escape sequence. An escape sequence is a combination of characters that represent a special character or symbol, such as a newline, tab, or double quote.

See the modified code below:

// C program to print the double quotes
#include <stdio.h>

int main() {
    
    // Print string with a double quote
    printf("Hello\" world"); 

    return 0;
}

Output:

Hello" world

You can use any number of escape sequences in a string.

For example, if you want to print "Hello", world, you have to use two escape sequences:

// C program to print the double quotes
#include <stdio.h>

int main() {
    
    // Print string with double quotes
    printf("\"Hello\" world"); 

    return 0;
}

Output:

"Hello" world

We have also attached an image below as a result of the above C program:

Print double quotes in c programming

List of Escape Sequences

Below is the list of all the escape sequences that we commonly use in our C programs:

Escape Sequencecharacter Represented
\aAlert(bell, alarm)
\bBackspace
\fForm Feed(New Page)
\nNew Line
\rCarriage Return
\tHorizontal Tab
\vVertical Tab
\’Single Quotation Mark
\”Double Quotation Mark
\?Question Mark
\\Backslash

Conclusion

In this article, we learned why we can’t directly print the double quotes in C programming language.

In summary, double quotes represent a string literal in C programming language. So, if we directly try to print them, we get a compilation error because the compiler gets confused in finding the end of the string.

Therefore, we use an escape sequence i.e. \" to print the double quote. The escape sequence tells the compiler that treat it as a regular character, not as an end of the string.

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.