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); } } 
4

2 Answers

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

1

You might want to look into setlocale. I don't know which set of characters you're looking for but you could try setlocale (LC_ALL,""); to set your printed characters to match the environment (which seems to match your requirement since copy-pasting worked).

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.