0

Possible Duplicate:
How to escape the % (percent) sign in C's printf

I want to print the % symbol like this: "8%2F16"

How can I print this string?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bTagTiger
  • 1,261
  • 5
  • 23
  • 38

3 Answers3

4

If you're trying to print using printf, you'll need to use %%: escape the % with another %:

printf("8%%2F16");

% is an escape character with a special meaning in the printf format string, and so itself needs to be escaped if you're trying to print it.

pb2q
  • 58,613
  • 19
  • 146
  • 147
3

Besides using %%, you can also use %c:

printf("8%c2F16\n", '%');

The %c trick is a good fallback if you can't remember how to escape a character properly in your string. (Although, off the top of my head, the only tricky ones are " and %.)

jxh
  • 69,070
  • 8
  • 110
  • 193
2

With the suitable escape sequence, like so: printf("%%");

(Or of course just as puts("%");, but I suppose you're talking about formatted output.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084