In an interview I was asked
Print a quotation mark using the
printf()
function
I was overwhelmed. Even in their off
you should use escape character like this:
printf("\"");
#include<stdio.h>
int main(){
char ch='"';
printf("%c",ch);
return 0;
}
Output: "
Try this:
#include <stdio.h>
int main()
{
printf("Printing quotation mark \" ");
}
Besides escaping the character, you can also use the format %c
, and use the character literal for a quotation mark.
printf("And I quote, %cThis is a quote.%c\n", '"', '"');
You have to use escaping of characters. It's a solution of this chicken-and-egg problem: how do I write a ", if I need it to terminate a string literal? So, the C creators decided to use a special character that changes treatment of the next char:
printf("this is a \"quoted string\"");
Also you can use '\' to input special symbols like "\n", "\t", "\a", to input '\' itself: "\\" and so on.
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.