2

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

How can I print '%' in C?

E.g.:

printf("A: %.2f%", pc);

...fails, the compiler complains there is an invalid conversion. Of course an easy way is;

printf("A: %.2f%c", pc, '%');

But it's rather inelegant...

I looked on the web, but I didn't find any escape sequence for %. I thought % would work, but it doesn't.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kern
  • 21
  • 1

4 Answers4

8
printf("A: %.2f%%", pc);
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

Just double the '%' in the format string and it will print '%'.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HeDo
  • 21
  • 1
1

For future printf reference, type:

man 3 printf

on any Linux command prompt. It can do a lot of crazy stuff that most people just aren't aware of.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rannick
  • 598
  • 5
  • 19
0

%% will output % using printf. Check the example at the end of page here

Praveen S
  • 10,355
  • 2
  • 43
  • 69