How to print special characters explicitly in C?

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

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.


Something like this might be what you want. Run myprint(c) to print the character C or a printable representation of it:

#include <ctype.h>

void myprint(int c)
{
    if (isprint(c))
        putchar(c); // just print printable characters
    else if (c == '\n')
        printf("\\n"); // display newline as \n
    else
        printf("%02x", c); // print everything else as a number
}

If you're using Windows, I think all your newlines will be CRLF (carriage return, linefeed) so they'll print as 0d\n the way I wrote that function.

Tags:

C

Printf