Home > Net >  Reading user input and displaying the ASCII equivalent
Reading user input and displaying the ASCII equivalent

Time:10-04

I'm writing a program that asks the user to input a number and displays the ASCII equivalent. It also displays the ASCII numbers of the 5 previous numbers and the five next numbers.

here's my code

#include <stdio.h>
displayASCIITABLE(int input);
int main()
{
   int input;
  printf("enter a number:\n");
 
 
displayASCIITABLE(input);
return 0;
}
displayASCIITABLE(int input)

{
    printf("Number    Character\n");
   for(int i=input-5;i<=input 5;i  )
{

  printf("%d         %c\n",i,i);
}

}

The code works perfectly but some of the numbers ASCII value is not showing here's an example enter image description here

for example, the ASCII value of 7 8 9 10 as shown in the image above

CodePudding user response:

These numbers are probably escape codes, for example 10 is a newline. These types of characters aren't supposed to be shown but affect things like formatting

CodePudding user response:

Not all character codes in ASCII are printable. Please, read about ASCII on Wikipedia, it will help you.

For instance, only characters from 32 ( , space) to 126 (~, tilde) are printable (i.e.: they are displayable on a terminal or in a text file, etc). There are also whitespace characters like \n, \r, \t, and others, that are considered control characters, but they do have some visual effect in a terminal (or in a text file, etc).

NOTE: the strange symbols you see in your terminal are not part of ASCII (intended as US-ASCII), but they are an extension to ASCII (but their codes are platform-dependent, since they rely on which character encoding your terminal has).

TL;DR: you cannot print any integer as it were a character; some character codes are just not displayable.

  • Related