如何将转义字符打印为字符?

发布于 2024-11-24 08:56:19 字数 595 浏览 1 评论 0原文

我正在尝试使用此代码将转义字符打印为字符或字符串:

while((c = fgetc(fp))!= EOF)
{
    if(c == '\0')
    {
        printf("   \0");
    }
    else if(c == '\a')
    {
        printf("   \a");
    }
    else if(c == '\b')
    {
        printf("   \b");
    }
    else if(c == '\f')
    {
        printf("   \f");
    }
    else if(c == '\n')
    {
        printf("   \n");
    }
    else if(c == '\r')
    {
        printf("   \r");
    }
    else if(c == '\t')
    {
        printf("   \t");
    }
    else if(c == '\v')
    {
        printf("   \v");
    }
}

但是当我尝试时,它实际上打印了转义序列。

I'm trying to print escape characters as characters or strings using this code:

while((c = fgetc(fp))!= EOF)
{
    if(c == '\0')
    {
        printf("   \0");
    }
    else if(c == '\a')
    {
        printf("   \a");
    }
    else if(c == '\b')
    {
        printf("   \b");
    }
    else if(c == '\f')
    {
        printf("   \f");
    }
    else if(c == '\n')
    {
        printf("   \n");
    }
    else if(c == '\r')
    {
        printf("   \r");
    }
    else if(c == '\t')
    {
        printf("   \t");
    }
    else if(c == '\v')
    {
        printf("   \v");
    }
}

but when i try it, it actually prints the escape sequence.

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

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

发布评论

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

评论(4

浊酒尽余欢 2024-12-01 08:56:20

为此,我们需要使用双反斜杠。

示例:

if(c == '\0')
{
    printf("   \\0");
}
else if(c == '\a')
{
    printf("   \\a");
}
else if(c == '\b')
{
    printf("   \\b");
}
else if(c == '\f')
{
    printf("   \\f");
}
else if(c == '\n')
{
    printf("   \\n");
}
else if(c == '\r')
{
    printf("   \\r");
}
else if(c == '\t')
{
    printf("   \\t");
}
else if(c == '\v')
{
    printf("   \\v");
}

应该适合你!

For that we need to use double backslash.

Examples:

if(c == '\0')
{
    printf("   \\0");
}
else if(c == '\a')
{
    printf("   \\a");
}
else if(c == '\b')
{
    printf("   \\b");
}
else if(c == '\f')
{
    printf("   \\f");
}
else if(c == '\n')
{
    printf("   \\n");
}
else if(c == '\r')
{
    printf("   \\r");
}
else if(c == '\t')
{
    printf("   \\t");
}
else if(c == '\v')
{
    printf("   \\v");
}

Should work for you!

眉目亦如画i 2024-12-01 08:56:20

如果您想在 printf 中转义 %d 以允许实际打印字符“%d”:

printf("%%d"); 

If you want to escape %d within printf to allow you to actually print the characters "%d":

printf("%%d"); 
把回忆走一遍 2024-12-01 08:56:19

转义斜杠(使用 " \\a"),这样它们就不会被特殊解释。此外,您可能至少想使用查找表或开关

switch (c) {
case '\0':
    printf("   \\0");
    break;
case '\a':
    printf("   \\a");
    break;
/* And so on. */
}

Escape the slashes (use " \\a") so they won't get interpreted specially. Also you might want to use a lookup table or a switch at least.

switch (c) {
case '\0':
    printf("   \\0");
    break;
case '\a':
    printf("   \\a");
    break;
/* And so on. */
}
幻梦 2024-12-01 08:56:19

字符串文字中的反斜杠需要转义;您需要 "\\0",而不是 "\0"

查找表可能会减轻这个痛苦:

const char *ecs[256] = {NULL}; // assumes ASCII - may not be a valid assumption
int c;

ecs['\0'] = "\\0";
ecs['\a'] = "\\a";
ecs['\b'] = "\\b";
...
while ((c = fgetc(fp)) != EOF)
{
  if (ecs[c] == NULL)
    printf("%c", c);
  else
    printf("%s", ecs[c]);
}

是的,ecs 中的大多数条目都将为 NULL;权衡是我不必担心将字符值映射到数组索引。

Backslashes in string literals need to be escaped; instead of "\0", you need "\\0".

A lookup table might make this less painful:

const char *ecs[256] = {NULL}; // assumes ASCII - may not be a valid assumption
int c;

ecs['\0'] = "\\0";
ecs['\a'] = "\\a";
ecs['\b'] = "\\b";
...
while ((c = fgetc(fp)) != EOF)
{
  if (ecs[c] == NULL)
    printf("%c", c);
  else
    printf("%s", ecs[c]);
}

Yes, the majority of entries in ecs are going to be NULL; the tradeoff is that I don't have to worry about mapping the character value to array index.

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