如何转义 C 的 printf 中的 %(百分号)符号
在 C 中使用 printf 时如何转义 % 符号?
printf("hello\%"); /* not like this */
How do you escape the % sign when using printf
in C?
printf("hello\%"); /* not like this */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您可以通过发布一个双“%”来转义它,如下所示:
%%
使用您的示例:
转义“%”符号仅适用于 printf。如果你这样做:
它将打印:
This is a's value: %%
You can escape it by posting a double '%' like this:
%%
Using your example:
Escaping the '%' sign is only for printf. If you do:
It will print:
This is a's value: %%
正如其他人所说, %% 将转义 %。
但请注意,您永远不应该这样做:
每当您必须打印字符串时,总是,总是,总是使用它来打印它,
以防止嵌入的 % 导致问题(内存违规,分段错误等)。
As others have said, %% will escape the %.
Note, however, that you should never do this:
Whenever you have to print a string, always, always, always print it using
to prevent an embedded % from causing problems (memory violations, segmentation faults, etc.).
如果字符串中没有格式,您可以使用
puts
(或fputs
):如果字符串中有格式:
如注释中所述,
puts
会将\n
附加到输出,而fputs
不会。If there are no formats in the string, you can use
puts
(orfputs
):if there is a format in the string:
As noted in the comments,
puts
appends a\n
to the output andfputs
does not.与自己...
With itself...
使用 双
%%
:Use a double
%%
:吹毛求疵:
您并没有真正转义字符串中指定
printf()
(和scanf()
)系列函数格式的%
。printf()
(和scanf()
)函数系列中的%
启动转换规范。转换规范的规则之一规定,%
作为转换说明符(紧跟在启动转换规范的%
之后)会导致'%' 要写入的字符,不转换参数。
字符串 really 内部有 2 个
'%'
字符(与转义字符相反:"a\bc"
是一个包含 3 个非 null 的字符串字符;"a%%b"
是一个包含 4 个非空字符的字符串)。Nitpick:
You don't really escape the
%
in the string that specifies the format for theprintf()
(andscanf()
) family of functions.The
%
, in theprintf()
(andscanf()
) family of functions, starts a conversion specification. One of the rules for conversion specification states that a%
as a conversion specifier (immediately following the%
that started the conversion specification) causes a'%'
character to be written with no argument converted.The string really has 2
'%'
characters inside (as opposed to escaping characters:"a\bc"
is a string with 3 non null characters;"a%%b"
is a string with 4 non null characters).像这样:
Like this:
可以使用%%:
结果是:
You can use %%:
The result is:
您使用的格式说明符不正确。您应该使用
%%
来打印%
。您的代码应该是:阅读更多 all C 中使用的格式说明符。
You are using the incorrect format specifier. You should use
%%
for printing%
. Your code should be:Read more all format specifiers used in C.
C 中的反斜杠用于转义字符串中的字符。字符串不会将
%
识别为特殊字符,因此不需要转义。printf
是另一回事:使用%%
打印一个%
。The backslash in C is used to escape characters in strings. Strings would not recognize
%
as a special character, and therefore no escape would be necessary.printf
is another matter: use%%
to print one%
.您可以简单地使用
%
两次,即"%%"
示例:
You can simply use
%
twice, that is"%%"
Example:
是的,使用 printf("hello%%"); 就完成了。
Yup, use
printf("hello%%");
and it's done.双“%”也适用于“.Format(…)”。
示例(iDrawApertureMask == 87,fCornerRadMask == 0.05):
csCurrentLine.Format("\%ADD%2d%C,%6.4f*\%",iDrawApertureMask,fCornerRadMask) ;
给出 csCurrentLine(中的字符串内容)的期望值和预期值;
“%ADD87C,0.0500*%”
The double '%' works also in ".Format(…).
Example (with iDrawApertureMask == 87, fCornerRadMask == 0.05):
csCurrentLine.Format("\%ADD%2d%C,%6.4f*\%",iDrawApertureMask,fCornerRadMask) ;
gives the desired and expected value of (string contents in) csCurrentLine;
"%ADD87C, 0.0500*%"