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?
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.
\' - escape sequence When we place \' escape sequence in printf, it has a special meaning. printf will print ' (single quote) instead \'.
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.
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.
Try this:
#include <stdio.h> int main() { printf("Printing quotation mark \" "); }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With