Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print a quotation mark in C?

Tags:

In an interview I was asked

Print a quotation mark using the printf() function

I was overwhelmed. Even in their office there was a computer and they told me to try it. I tried like this:

void main() {     printf("Printing quotation mark " "); } 

but as I suspected it doesn't compile. When the compiler gets the first " it thinks it is the end of string, which is not. So how can I achieve this?

like image 340
Mistu4u Avatar asked Aug 02 '12 06:08

Mistu4u


People also ask

How do you use quotation marks in printf?

Since printf uses ""(double quotes) to identify starting and ending point of a message, we need to use \" escape sequence to print the double quotes.

How do I print a single quote in printf?

\' - escape sequence When we place \' escape sequence in printf, it has a special meaning. printf will print ' (single quote) instead \'.

How do you type a quotation mark?

To create the quote symbol using a U.S. keyboard hold down the Shift and press ' , which is on the same key as the single quote ( ' ) and typically to the left of the Enter key.

How do you print quotation marks in a string?

The first method to print the double quotes with the string uses an escape sequence, which is a backslash ( \ ) with a character. It is sometimes also called an escape character. Our goal is to insert double quotes at the starting and the ending point of our String.


2 Answers

Try this:

#include <stdio.h>  int main() {   printf("Printing quotation mark \" "); } 
like image 189
Sune Trudslev Avatar answered Oct 01 '22 15:10

Sune Trudslev


Without a backslash, special characters have a natural special meaning. With a backslash they print as they appear.

\   -   escape the next character "   -   start or end of string ’   -   start or end a character constant %   -   start a format specification \\  -   print a backslash \"  -   print a double quote \’  -   print a single quote %%  -   print a percent sign 

The statement

printf("  \"  ");  

will print you the quotes. You can also print these special characters \a, \b, \f, \n, \r, \t and \v with a (slash) preceeding it.

like image 37
Angus Avatar answered Oct 01 '22 13:10

Angus