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

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

当前回答

与自身……

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

其他回答

你可以简单地使用两次%,即%%

例子:

printf("You gave me 12.3 %% of profit");

C语言中的反斜杠用于转义字符串中的字符。字符串不会将%识别为特殊字符,因此不需要转义。Printf则是另一回事:使用%%来打印一个%。

您可以通过发布两个'%'来转义它,就像这样:%%

举个例子:

printf("hello%%");

转义'%'符号只适用于printf。如果你有:

char a[5];
strcpy(a, "%%");
printf("This is a's value: %s\n", a);

这是a的值:%%

是这样的:

printf("hello%%");
//-----------^^ inside printf, use two percent signs together

与自身……

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