4

The output of

printf("%%%%%%%%");

is

%%%%

I used % eight times. Why does the output only have four %s?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nitin kumar
  • 642
  • 12
  • 21
  • 6
    The % should be escaped.. %% represents one % – Maroun Jan 20 '15 at 11:25
  • 4
    Albeit simple, the question doesn't deserve downvotes. It's concise, to the subject and clear. Also it's not trivial to find on google if you don't know the terms to search for. – Luchian Grigore Jan 20 '15 at 11:27
  • 2
    @LuchianGrigore: `man printf`? (and search for `%%` on the page). – Karoly Horvath Jan 20 '15 at 11:32
  • @KarolyHorvath not everyone (including me) has `man` built-in the command line. – Luchian Grigore Jan 20 '15 at 11:36
  • 2
    Not sure about the downvotes to question but sometimes SO amazes me with Votes on simple answers going as high as `9-10`. While, well thought and answers which need painful effort to write get max `2-3` votes. – Sadique Jan 20 '15 at 11:37
  • 1
    @LuchianGrigore: You asked for a google search term. I gave one. And searching *only* for `printf` works just as well (look, what a lovely chart..) Here's another one: `print % with printf` or `printf print %` – Karoly Horvath Jan 20 '15 at 11:46
  • 1
    FWIW the first Google hit for `man printf` is [this page](http://linux.die.net/man/3/printf). Note that even with this web page in hand and knowing what to look for, it's still tough (but not impossible) to find it. – Bob Jarvis - Слава Україні Jan 20 '15 at 11:47
  • each language has it's own limitation and the escape characters are one of them. But each language also provides an other way round to handle it, Same is with C++. You have to write twice to print it once. – Bector Jan 20 '15 at 12:00
  • @Bector: not all specialised languages are so smart as to handle escaping escapes properly. Most are, but you still need to include it explicitly and some forgot. – Martijn Pieters Jan 20 '15 at 12:11
  • Did you ever search for printf function format before asking this? – phuclv Jan 20 '15 at 12:14
  • http://en.cppreference.com/w/cpp/io/c/fprintf – phuclv Jan 20 '15 at 12:16

3 Answers3

14

Because % is a format specifier escape sequence (as in %d would print an int).

To get the program to print the actual symbol, you write %%. The same goes for \ (although fundamentally different, you still need to to print one).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

The "%" is a special character, and it's used to specify format specifiers. To print a literal "%", you need a sequence of two "%%".

The "%" is used in the format string to specify a placeholder that will be replaced by a corresponding argument passed to the functions that format their output, like printf()/fprintf()/sprintf().

So how can you print a literal "%"?

  • Escaping it with another "%".

If you for example wish to print an integral percentage value, you need to specify as the "%d" the format specifier and a literal "%", so you would do it this way:

printf("%d%%\n", value):

Read this to learn more about it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1
"%%"

in printf is an escaped '%' sign. See this Stack Overflow answer: How to escape the % (percent) sign in C's printf

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael
  • 979
  • 6
  • 13