如何在C中显式打印特殊字符

如何在C中显式打印特殊字符

本文介绍了如何在C中显式打印特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下代码时:

#include <stdio.h>

int main(void)
{
    printf("%s","Hello world\nHello world");
    return 0;
}

它打印为:

 Hello world
 Hello world

如何防止这种情况并将其打印为 C 中的原始字符串文字?我的意思是它应该在终端窗口中显示,如下所示:

How can I prevent this and print it as raw string literal in C? I mean it should be displayed as it is in terminal window like below:

Hello world\nHello world

我知道我可以通过对 printf 使用反斜杠来实现这​​一点,但是有没有其他 C 函数或方法可以在不使用反斜杠的情况下做到这一点?阅读文件时会很有帮助.

I know I can achieve this by using backslash for printf but is there any other C function or way to do this without backslashing? It would be helpful when reading files.

推荐答案

没有内置机制来执行此操作.您必须逐个字符地手动执行此操作.但是,ctype.h 中的函数可能会有所帮助.具体来说,在C"语言环境中,isprint 函数保证对基本执行字符集中的所有图形字符都为真,这实际上与7 位 ASCII 中的所有图形字符,加上空格;并且对于 7 位 ASCII 中的所有 控制 字符(包括制表符、回车符等)保证为真.

There is no built-in mechanism to do this. You have to do it manually, character-by-character. However, the functions in ctype.h may help. Specifically, in the "C" locale, the function isprint is guaranteed to be true for all of the graphic characters in the basic execution character set, which is effectively the same as all the graphic characters in 7-bit ASCII, plus space; and it is guaranteed not to be true for all the control characters in 7-bit ASCII, which includes tab, carriage return, etc.

这是一个草图:

#include <stdio.h>
#include <ctype.h>
#include <locale.h>

int main(void)
{
    int x;
    setlocale(LC_ALL, "C"); // (1)

    while ((x = getchar()) != EOF)
    {
        unsigned int c = (unsigned int)(unsigned char)x; // (2)

        if (isprint(c) && c != '\\')
            putchar(c);
        else
            printf("\\x%02x", c);
    }
    return 0;
}

这不会转义 '",但它会转义 \,如果需要,可以直接扩展它.

This does not escape ' nor ", but it does escape \, and it is straightforward to extend that if you need it to.

为 U+000A 打印 \n,为 U+000D 打印 \r 等作为练习.处理基本执行字符集之外的字符(例如 U+0080 到 U+10FFFF 的 UTF-8 编码)也留作练习.

Printing \n for U+000A, \r for U+000D, etc. is left as an exercise. Dealing with characters outside the basic execution character set (e.g. UTF-8 encoding of U+0080 through U+10FFFF) is also left as an exercise.

该程序包含两件东西,它们对于完全符合标准的 C 库来说是不必要的,但根据我的经验,在实际操作系统上却是必需的.它们用 (1)(2) 标记.

This program contains two things which are not necessary with a fully standards-compliant C library, but in my experience have been necessary on real operating systems. They are marked with (1) and (2).

1) 这以应该默认设置的方式显式设置区域设置"配置.

1) This explicitly sets the 'locale' configuration the way it is supposed to be set by default.

2) getchar 返回的值是一个 int.应该unsigned char(通常包括0-255)可以表示的范围内的数字,或者是特殊值EOF(它不在unsigned char表示的范围内).但是,众所周知,有缺陷的 C 库会为设置了最高位的字符返回负数.如果发生这种情况,printf 会在它应该打印 \xa1 时打印(例如)\xffffffa1.将 x 转换为 unsigned char 然后再转换回 unsigned int 可以纠正这个问题.

2) The value returned from getchar is an int. It is supposed to be either a number in the range representable by unsigned char (normally 0-255 inclusive), or the special value EOF (which is not in the range representable by unsigned char). However, buggy C libraries have been known to return negative numbers for characters with their highest bit set. If that happens, the printf will print (for instance) \xffffffa1 when it should've printed \xa1. Casting x to unsigned char and then back to unsigned int corrects this.

这篇关于如何在C中显式打印特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:35