开发者

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

This question already h开发者_运维知识库as answers here: Closed 12 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜