Printing an ascii chart

I tried to print the full ASCII plot. In the meantime, I saw this code on tutorialsschool.com .

#include<stdio.h> void main() { int i; for(i=0;i<=255;i++){ printf("%d->%c\n",i,i); } } 

It looks perfect, but the problem is that it doesn't print characters for locations (I use Code ID + Blocks IDE) like 7,8,9,10 and 32. I'm really confused why it doesn't print values ​​in these places . And this gives some strange conclusion on online compilers. This is a Code :: Blocks issue. What could be another program to print these ASCII characters.

+5
source share
3 answers

You may be interested to know that not all ASCII characters can be printed.

For example, decimal numbers from 0 to 31 are non-printable ASCII values.

See this link for the same thing.

However, for a hosted environment, the expected signature of main() is int main(void) , at least.

+3
source

I am really confused why it does not print values ​​in these places.

Because this code is non-printable ASCII code. Note. The standard ASCII code has only 7 bits (for example, 128 characters), and some of them cannot be printed (control codes), so you cannot print them (for example, can you print Bell 0x07?)

http://www.asciitable.com/


And as Mojit Jain pointed out, you really need to use the isprint function to check if a character is allowed for the standard C language before printing it - a very handy feature.

+6
source

A subset of ASCII can be printed. Some of these are control characters such as string, call, etc.

Details: ASCII is defined for codes from 0 to 127. For a full ASCII scheme, only for(i=0;i<=127;i++) is required for(i=0;i<=127;i++) .

-

OTOH , maybe you want to print a full graph of all char . When char prints, they are first converted to unsigned char . So, let's create a diagram of all unsigned char .

Note. Using ASCII for character codes from 0 to 127 in a very general way, but not specified C.

To determine if unsigned char can be printed, use the isprint() function. For others, type the escape sequence.

 #include<ctype.h> #include<limits.h> #include<stdio.h> int main(void) { unsigned char i = 0; do { printf("%4d: ", i); if (isprint(i)) { printf("'%c'\n", i); } else { printf("'\\x%02X'\n", i); } } while (i++ < UCHAR_MAX); return 0; } 

Output example

  0: '\x00' 1: '\x01' ... 7: '\x07' 8: '\x08' 9: '\x09' 10: '\x0A' ... 31: '\x1F' 32: ' ' 33: '!' 34: '"' ... 65: 'A' 66: 'B' 67: 'C' ... 126: '~' 127: '\x7F' ... 255: '\xFF' 
+2
source

Source: https://habr.com/ru/post/1261124/


All Articles