5

I am trying to print a % sign using printf.

I have tried with no luck:

printf("\%");

I'm sure it's really simple, but I've just started C.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mitchellt
  • 954
  • 4
  • 13
  • 26
  • 5
    It's `%%`. [Read the manual](http://pubs.opengroup.org/onlinepubs/000095399/functions/printf.html). – netcoder Nov 28 '13 at 20:42
  • 1
    Related: [Why is percentage character not escaped with backslash in C?](http://stackoverflow.com/questions/17811531/why-is-percentage-character-not-escaped-with-backslash-in-c) (though not necessarily duplicate) – Kninnug Nov 28 '13 at 20:42

3 Answers3

15

Use printf("%%"); The backslash is the escape character for C strings; the compiler interprets it. The percent sign is printfs escape character; the printf routine interprets it.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
5

Here are two ways:

printf("%%\n");
printf("%c\n", '%');
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
3

Try by using the following escape sequence:

%%

Check out all escapes usable in *rintf() functions family

The Mask
  • 17,007
  • 37
  • 111
  • 185