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

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

当前回答

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

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

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

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

printf("%s", c)

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

其他回答

是这样的:

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

使用双%%:

printf("hello%%");

你可以使用%%:

printf("100%%");

结果是:

100%

与自身……

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

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

举个例子:

printf("hello%%");

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

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

这是a的值:%%