在C中使用printf时如何转义%符号?
printf("hello\%"); /* not like this */
在C中使用printf时如何转义%符号?
printf("hello\%"); /* not like this */
当前回答
双字符“%”也适用于“. format(…)”。 示例(iDrawApertureMask == 87, fCornerRadMask == 0.05): csCurrentLine.Format(“\ %添加% 2 d C %, % 6.4 f * \ %”,iDrawApertureMask, fCornerRadMask); 给出csCurrentLine中(字符串内容)的期望值和期望值; “% ADD87C, 0.0500 * %”
其他回答
与自身……
printf("hello%%"); /* like this */
正如其他人所说,%%将转义%。
然而,请注意,你永远不应该这样做:
char c[100];
char *c2;
...
printf(c); /* OR */
printf(c2);
当你需要打印字符串时,总是,总是,总是使用
printf("%s", c)
防止嵌入的%引起问题(内存违规,分段错误等)。
是这样的:
printf("hello%%");
//-----------^^ inside printf, use two percent signs together
C语言中的反斜杠用于转义字符串中的字符。字符串不会将%识别为特殊字符,因此不需要转义。Printf则是另一回事:使用%%来打印一个%。
您使用的格式说明符不正确。您应该使用%%来打印%。你的代码应该是:
printf("hello%%");
阅读更多C语言中使用的格式说明符。