简体   繁体   中英

How to use sprintf to output the symbol % in a string?

I want to convert a percentage to a string via sprintf. A workable way is like this:

sprintf(str, "%d", num);
strcat(str, "%");

But is there a way to only use sprintf to make it done?

(Compiling an answer from comments, just to get this out of the list of unanswered questions.)

Printing a % via sprintf() is done via "%%" , ie in your case thus:

sprintf(str, "%d%%", num);

Here is a quote from the helpful documentation:

A '%' is written. No argument is converted. The complete conversion specification is '%%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM