ASCII Table in C
This article will discuss ASCII codes and how to create a C program that prints an ASCII table. The American Standard Code for Information Interchange is referred to as ASCII. For the purpose of communicating information between computers and other electronic devices, the ASCII code character encoding system is employed to specify the value of fundamental character elements.
Additionally, 255 symbols in the character set make up the extended ASCII code, which is a subset of the standard ASCII code. The extended ASCII code, which runs from 128 to 255, is 8 bits long and extends the conventional ASCII code's range from 0 to 127. These characters are a mixture of control characters, uppercase and lowercase symbol letters (a-z, AZ), numbers (0–9), special characters (!, @, #, and others), punctuation marks, and special characters (! @, #, and others). As a result, we may claim that every character has a unique ASCII value.
American Standard Code for Information Interchange is referred to as ASCII.
In later years, ASCII Value codes were expanded to 8 bits by American standards. There are 256 ASCII codes in total, numbered 0 to 255.
The tables for the characters' expanded 7-bit and 8-bit ASCII codes are shown in the images below.
Each character will have a unique code when a character variable is defined, which will be stored in the memory.
American Standard Code for Information Interchange is referred to as ASCII. A character encoding scheme used in communications is called ASCII. Only 128 of the 256 ASCII characters (0 to 127) are used. These include numerals, punctuation marks, capital and lowercase characters, and more.
Note: The first 31 ASCII characters are ASCII control characters. These characters include NULL, BS (backspace), STX (Start of Text), etc. Therefore, these characters are not displayed in the console when we print them.
1. Program to get ASCII characters.
Code:
#include <stdio.h>
int main(void)
{
puts("Printable ASCII:");
for (int i = 32; i < 127; ++i) {
putchar(i);
putchar(i % 16 == 15 ? '\n' : ' ');
}
}
Output:
![ASCII Table in C](https://static.tutorialandexample.com/c/ascii-table-in-c1.png)
2. Program to get ASCII characters of capital letters.
Code:
#include <stdio.h>
int main()
{
//Declare a local variable
int caps;
// use for loop to print the capital letter from A to Z
for ( caps = 65; caps < 91; caps++)
{
printf (" \n The ASCII value of %c is %d ", caps, caps);
}
return 0;
}
Output:
![ASCII Table in C](https://static.tutorialandexample.com/c/ascii-table-in-c2.png)
![ASCII Table in C](https://static.tutorialandexample.com/c/ascii-table-in-c3.png)
3. Program to print all the small letters’ ASCII values.
Code:
#include <stdio.h>
int main()
{
//Declare a local variable
int small;
// use for loop to print the small alphabet letter from a to z
for ( small = 97; small < 123; small++)
{
// display ASCII values to its equivalent characters
printf (" \n The ASCII value of %c is %d ", small, small);
}
return 0;
}
Output:
![ASCII Table in C](https://static.tutorialandexample.com/c/ascii-table-in-c4.png)
![ASCII Table in C](https://static.tutorialandexample.com/c/ascii-table-in-c5.png)
4. Program to get ASCII characters of Random letter.
Code:
#include <stdio.h>
int main( void )
{
unsigned char char1 , char2 , char3 , char4 , char5 , char6 , char7 , char8 ;
printf("|---------------------------------------------------------------------------------------------------------|\n" );
printf("|extended ASCII table - excluding control characters |\n" );
printf("| Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch Dec Hex | Ch Dec Hex |\n" );
printf("|----------------|----------------|-------------|--------------|--------------|-------------|-------------|\n" );
for( int i = 0 ; i < 32; i++)
{
char1 = i;
char2 = i+32;
char3 = i+64;
char4 = i+96;
char5 = i+128; // extended ASCII characters
char6 = i+160;
char7 = i+192;
char8 = i+224;
printf( "| %char %3d %#x " ,
char2 , char2 , char2 );
printf( "| %char %3d %#x " ,
char3 , char3 , char3 );
if( char4 == 127 ) {
printf("|%s %3d %#x |" ,
"DEL" , char4 , char4 );
} else {
printf("| %c %3d %#x |" ,
char4 , char4 , char4 );
}
// Print extended ASCII for the current system.
printf(" %c %3d %#x | %c %3d %#x | %c %3d %#x | %c %3d %#x |" ,
char5 , char5 , char5 ,
char6 , char6 , char6 ,
char7 , char7 , char7 ,
char8 , char8 , char8 );
printf( "\n" );
}
}
Output:
![ASCII Table in C](https://static.tutorialandexample.com/c/ascii-table-in-c6.png)