How can I print a quotation mark in C?
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?
Solution 1:
Try this:
#include <stdio.h>
int main()
{
printf("Printing quotation mark \" ");
}
Solution 2:
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.
Solution 3:
You have to escape the quotationmark:
printf("\"");