如何转义 C 的 printf 中的 %(百分号)符号

发布于 2024-08-13 23:34:20 字数 98 浏览 4 评论 0原文

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

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

How do you escape the % sign when using printf in C?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

吻安 2024-08-20 23:34:20

您可以通过发布一个双“%”来转义它,如下所示: %%

使用您的示例:

printf("hello%%");

转义“%”符号仅适用于 printf。如果你这样做:

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

它将打印:This is a's value: %%

You can escape it by posting a double '%' like this: %%

Using your example:

printf("hello%%");

Escaping the '%' sign is only for printf. If you do:

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

It will print: This is a's value: %%

筑梦 2024-08-20 23:34:20

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

但请注意,您永远不应该这样做:

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

每当您必须打印字符串时,总是,总是,总是使用它来打印它,

printf("%s", c)

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

As others have said, %% will escape the %.

Note, however, that you should never do this:

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

Whenever you have to print a string, always, always, always print it using

printf("%s", c)

to prevent an embedded % from causing problems (memory violations, segmentation faults, etc.).

忘你却要生生世世 2024-08-20 23:34:20

如果字符串中没有格式,您可以使用 puts (或fputs ):

puts("hello%");

如果字符串中有格式:

printf("%.2f%%", 53.2);

如注释中所述,puts 会将 \n 附加到输出,而 fputs 不会。

If there are no formats in the string, you can use puts (or fputs):

puts("hello%");

if there is a format in the string:

printf("%.2f%%", 53.2);

As noted in the comments, puts appends a \n to the output and fputs does not.

诗酒趁年少 2024-08-20 23:34:20

与自己...

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

With itself...

printf("hello%%"); /* like this */
战皆罪 2024-08-20 23:34:20

使用 %%

printf("hello%%");

Use a double %%:

printf("hello%%");
倾听心声的旋律 2024-08-20 23:34:20

吹毛求疵:
您并没有真正转义字符串中指定 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 the printf() (and scanf()) family of functions.

The %, in the printf() (and scanf()) 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).

随梦而飞# 2024-08-20 23:34:20

像这样:

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

Like this:

printf("hello%%");
//-----------^^ inside printf, use two percent signs together
单身情人 2024-08-20 23:34:20

可以使用%%:

printf("100%%");

结果是:

100%

You can use %%:

printf("100%%");

The result is:

100%

烟花易冷人易散 2024-08-20 23:34:20

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

printf("hello%%");  

阅读更多 all C 中使用的格式说明符

You are using the incorrect format specifier. You should use %% for printing %. Your code should be:

printf("hello%%");  

Read more all format specifiers used in C.

优雅的叶子 2024-08-20 23:34:20

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 %.

三生一梦 2024-08-20 23:34:20

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

示例:

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

You can simply use % twice, that is "%%"

Example:

printf("You gave me 12.3 %% of profit");
a√萤火虫的光℡ 2024-08-20 23:34:20

是的,使用 printf("hello%%"); 就完成了。

Yup, use printf("hello%%"); and it's done.

寂寞美少年 2024-08-20 23:34:20

双“%”也适用于“.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*%"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文