Printing '%' with printf in C/C++ [duplicate]
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.
printf("A: %.2f%%", pc);
Just double the '%' in the format string and it will print '%'.
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.
%% will output % using printf. Check the example at the end of page here
精彩评论