7

I was trying to print complete ASCII chart . Meanwhile I saw this code on tutorialsschool.com website .

#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 is not printing symbols for locations (I'm using Code::Blocks IDE) such as 7,8,9,10 and 32. I am really confused why it not printing values at those locations.And it is giving some weird output on online compilers.Is it the problem of Code::Blocks. What possibly could be the other program to print these ASCII symbols.

q-l-p
  • 4,304
  • 3
  • 16
  • 36
Sandip Kumar
  • 241
  • 3
  • 25
  • 6
    Possible duplicate of [Printing chars and their ASCII-code in C](http://stackoverflow.com/questions/1472581/printing-chars-and-their-ascii-code-in-c) – msc Dec 12 '16 at 10:47
  • 4
    [man isprint](https://linux.die.net/man/3/isprint) – Mohit Jain Dec 12 '16 at 10:47
  • 2
    32 is the space character, 9 is tab, etc. You can at least try '%c' too see some more. – SurDin Dec 12 '16 at 10:54
  • 1
    The group 0 to 31 in the ASCII table is known as "the non-printable characters". Your question is therefore why your program isn't printing the non-printable characters. Number 32 is space, are you sure it isn't printed? – Lundin Dec 12 '16 at 12:45
  • Welcome to the world of misinformation on character encodings. The link you give shows a table that is not ASCII. It looks like CP437. In C or C++, see the locale functions for the character encoding your program is using. To see the encoding your terminal (console) is using, go `locale` (Linux) or `chcp` (Windows). Neither are likely to be ASCII. If your IDE is showing output in a non-console window, that might have a different setting that is controlled through the IDE. – Tom Blodget Dec 13 '16 at 00:40

3 Answers3

6

I am really confused why it not printing values at those locations.

Because those code is non-printable ASCII code. Note standard ASCII code has only 7 bit (ie 128 characters) - and several of them non-printable (control codes) - so you are not expected to be able print them (eg, can you print the Bell 0x07 ?)

http://www.asciitable.com/


And as Mohit Jain pointed out, you really need to use isprint function to check if a character is printable on standard C locale before printing it out - very handy function.

artm
  • 17,291
  • 6
  • 38
  • 54
3

You might be interested in knowing that not all the ASCII characters are printable.

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

See this reference, which mentions the same.

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

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

Only a subset of ASCII characters are printable. Some are control characters such as line-feed, bell, etc..

Detail: ASCII is defined for codes 0 to 127. Loop only needs for(i=0;i<=127;i++) for a complete ASCII chart.

--

OTOH, perhaps one wants to print a complete chart of all char. When char are printed, they are converted to unsigned char first. So let us create a chart of all unsigned char.

Note: Using ASCII for characters code 0 to 127 in very common, but not specified by C.

To determine if the unsigned char is printable, use the isprint() function. For others, print an 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;
}

Sample output

   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'
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256