Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing '%' with printf in C/C++ [duplicate]

Tags:

c

printf

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.

like image 702
Kern Avatar asked Mar 20 '26 19:03

Kern


2 Answers

printf("A: %.2f%%", pc);
like image 149
vmonteco Avatar answered Mar 25 '26 00:03

vmonteco


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

like image 33
HeDo Avatar answered Mar 24 '26 23:03

HeDo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!