在C中使用printf时如何转义%符号?

printf("hello\%"); /* not like this */

当前回答

你可以使用%%:

printf("100%%");

结果是:

100%

其他回答

使用双%%:

printf("hello%%");

与自身……

printf("hello%%"); /* like this */

您使用的格式说明符不正确。您应该使用%%来打印%。你的代码应该是:

printf("hello%%");  

阅读更多C语言中使用的格式说明符。

正如其他人所说,%%将转义%。

然而,请注意,你永远不应该这样做:

char c[100];
char *c2;
...
printf(c); /* OR */
printf(c2);

当你需要打印字符串时,总是,总是,总是使用

printf("%s", c)

防止嵌入的%引起问题(内存违规,分段错误等)。

双字符“%”也适用于“. format(…)”。 示例(iDrawApertureMask == 87, fCornerRadMask == 0.05): csCurrentLine.Format(“\ %添加% 2 d C %, % 6.4 f * \ %”,iDrawApertureMask, fCornerRadMask); 给出csCurrentLine中(字符串内容)的期望值和期望值; “% ADD87C, 0.0500 * %”