When writing a C program you might have the need to print the %
percentage character using printf()
.
编写C程序时,可能需要使用printf()
打印%
百分比字符。
Perhaps you are working on a program that calculates percentages, which is kind of common when you’re just learning the language and you’re creating small programs.
也许您正在开发一个计算百分比的程序,这在您只是学习语言并创建小型程序时很常见。
How do you do so?
你如何做?
If you try to use it like this:
如果您尝试像这样使用它:
printf("%");
it will not work, and the compiler will give you a warning like
它不起作用,编译器会给您类似的警告
hello.c:9:14: warning: incomplete format specifier
[-Wformat]
printf("%");
^
1 warning generated.
and it is not printed.
并且不打印。
To make it work, you need to write %%
, like this:
要使其正常工作,您需要像这样写%%
:
printf("%%");