How to print Extended ASCII characters 127 to 160 in through a C program?

CodeCodeCode picture CodeCodeCode · May 3, 2013 · Viewed 8.1k times · Source

I am trying below code to print all the ASCII characters, but it does not print anything for 127 to 160. I know they are Control Chracters set or some Latin/ Spanish characters. If the same characters are copy pasted from Windows, it prints well in unix. Why not throug a C program?

#include <stdio.h>

int main()
{
    int i;
    char ch;

    for(i = 0; i < 256; i++)
    {
        printf("\n%03d %02x %02c",i ,i ,i);
    }
}

Answer

Lee Daniel Crocker picture Lee Daniel Crocker · May 3, 2013

ASCII is a 7-bit code. The interpretation of byte values above 128 is dependent upon the OS, your locale/language settings, and so on. They are not standard. Under Windows in English, they are most commonly defined by CP1252; on Linux they are more commonly ISO-8859-1. Some OSs use UTF-8, which is not a character set itself but a way to encode Unicode into an 8-bit stream by using more than one byte for most characters. If you really need to work with characters outside the standard ASCII 32-126, you should really be using wide characters and locale stuff.

BTW, character 127 is a special case: it's the control character "rubout", which denotes erased data. (This was done so that a section of paper tape could be erased by punching all the holes!--yes, some of us are old enough to remember paper tape).