Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

printf - How to print special characters explicitly in C?

When I use below code:

#include <stdio.h>

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

it prints as:

 Hello world
 Hello world

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
Hello world

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Here is a sketch:

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

Printing for U+000A, 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.

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) This explicitly sets the 'locale' configuration the way it is supposed to be set by default.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...