27

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mistu4u
  • 5,132
  • 15
  • 53
  • 91
  • 12
    Remember the short section on escape characters at the beginning of that book...? – chris Aug 02 '12 at 06:41
  • Possible duplicate of [How to print special characters explicitly in C?](http://stackoverflow.com/questions/29477345/how-to-print-special-characters-explicitly-in-c) – phuclv Sep 01 '16 at 04:26

9 Answers9

34

Try this:

#include <stdio.h>

int main()
{
  printf("Printing quotation mark \" ");
}
Sune Trudslev
  • 964
  • 6
  • 11
24

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.

Angus
  • 12,133
  • 29
  • 96
  • 151
  • 1
    `\%` is wrong - it will be treated the same as `%`. `\'` isn't needed - you can put a quote within double quotes just like that. – ugoren Aug 02 '12 at 08:43
  • 1
    Good catch.But you cant print % within double quotes. But you can print a percentile sign using %%. Like printf("%%"); – Angus Aug 02 '12 at 09:01
  • 1
    You can also use `"%"` freely if it's not in the format string (e.g. `printf("print an int using %s\n", "%d")`) – ugoren Aug 02 '12 at 09:06
  • @Angus why won't `\%` work for printing a single `%`? – cokedude Oct 12 '15 at 02:23
  • @cokedude : The % sign inside the printf has a special meaning [Format specifier] and we cant print the "%" sign with printf("Printing the \% \n"); .. However, if you still wanted to print the % sign, you can follow the method of ugoren printf("Printing %s \n","%d"); => "%" is taken as a string and will be printed. – Angus Oct 14 '15 at 06:36
  • Including `%` in this list is pretty misleading. Escape sequences involving backslash are relevant to all strings, and are processed by the compiler, at compile time. But `%%` is completely different — it's only relevant to `printf` format strings, and it's handled by `printf`, at run time. – Steve Summit Nov 11 '21 at 17:17
14

You have to escape the quotationmark:

printf("\"");
halex
  • 16,253
  • 5
  • 58
  • 67
9

In the C programming language, \ is used to print some of the special characters which have special meaning in C. Those special characters are listed below

\\ - Backslash
\' - Single Quotation Mark
\" - Double Quatation Mark
\n - New line
\r - Carriage Return
\t - Horizontal Tab
\b - Backspace
\f - Formfeed
\a - Bell(beep) sound
FahimFBA
  • 27
  • 6
rashok
  • 12,790
  • 16
  • 88
  • 100
8

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", '"', '"');
jxh
  • 69,070
  • 8
  • 110
  • 193
5

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.

Dmytro Sirenko
  • 5,003
  • 21
  • 26
3

This one also works:

printf("%c\n", printf("Here, I print some double quotes: "));

But if you plan to use it in an interview, make sure you can explain what it does.

EDIT: Following Eric Postpischil's comment, here's a version that doesn't rely on ASCII:

printf("%c\n", printf("%*s", '"', "Printing quotes: "));

The output isn't as nice, and it still isn't 100% portable (will break on some hypothetical encoding schemes), but it should work on EBCDIC.

ugoren
  • 16,023
  • 3
  • 35
  • 65
  • This is wrong.The inner printf will be executed first.Then the outer printf will be executed.Normally nested printf's are used to find the total no of characters printed in the console. – Angus Aug 02 '12 at 09:36
  • 1
    This isn't wrong, just a bit twisted, and it works very well. Try it. As you say, the inner printf is executed first, the the outer - what's wrong with it? – ugoren Aug 02 '12 at 10:27
  • It will print the ascii value of the number(no of characters printed) outputted from the inner printf. – Angus Aug 02 '12 at 10:39
  • 1
    Yes, it will print the ASCII value of the number of characters printed in the inner printf. So count them and look it up in an ASCII table. – ugoren Aug 02 '12 at 11:40
  • 2
    I am tempted to vote up from amusement. However, the C standard does not specify that C uses ASCII. Thus, this answer is technically incorrect. – Eric Postpischil Aug 02 '12 at 12:14
  • @EricPostpischil, added a version which doesn't rely on ASCII. But it isn't as nice as the original. – ugoren Aug 02 '12 at 12:27
0
#include<stdio.h>
int main(){
char ch='"';
printf("%c",ch);
return 0;
}

Output: "

Digres
  • 74
  • 1
  • 7
0

you should use escape character like this:

printf("\"");
Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
  • 2
    Good solution, but it's already given in other answers, and the exact same code is given in halex's answer (https://stackoverflow.com/a/11772309/446106) – mwfearnley Dec 29 '18 at 13:44