Write a C Program To Print The ASCII Value Of All Characters. ASCII is one of the most common and widely used alphabets in computers. There are two main types of the alphabet, the English alphabet, and the Roman alphabet. The English alphabet consists of 26 letters and the Roman alphabet consists of 22 letters.
So, the ASCII values of these letters are from 65 to 90. If we try to print all the letters in the English alphabet using the print function of the C programming language, then it will take a lot of time. So, I have made this C program to print the ASCII value of all the characters in the English alphabet.
For simplicity, I have used a char array to store the ASCII value of each letter. I have written the entire program and tested it. You can download the program from the link given below.
C Program To Print The ASCII Value Of All Characters
To change the language of the program, you need to modify the ‘LANGUAGE’ variable in the source code.
char *languages[] = {"English", "Spanish"};
int i;
// The character array to hold the ASCII value of all the letters
char c[256];
printf("Enter the character to be printed: ");
for (i=65; i<=90; i++) {
if (scanf("%c", &c)==1)
printf("%c\n", c);
}
printf("\n");
printf("%s\n", languages[i]);
printf("Press any key to continue... \n");
getch();
return 0;
We all know that the ASCII value of each character varies, but still you can print all the ASCII values of the characters in a single line. In this tutorial, you will learn how to write a C program that prints the ASCII values of all characters.
To start the C program, you have to include the header file named as stdio.h. The header file is used to include all the standard library functions required to access the input/output functions.
The first step is to declare the variable in the main() function.
int main()
{
char a[100];
// Declare the variable a.
for(int i=0;i<100;i++)
{
a[i]=' ';
}
// Loop through the array and print the ASCII value of each character.
printf("\n");
for(int i=0;i<100;i++)
{
printf("%c",a[i]);
}
return 0;
}
Now, if you run the program, then you will get the output like this.
Another Method #1:
If we want to know what each code represents we will have to look into the table of ASCII values. The table consists of 128 codes and each code represents a symbol.
To write the code we will have to use some basic programming concepts. Programming is the art of writing a computer program or software to perform a specific task. The purpose of programming is to convert the thought of the programmer into the machine language of the computer. C program is a series of instructions that tells the computer how to perform a specific task. The C language is the most commonly used programming language for writing programs. The C language is mainly used for creating a program for a personal computer.
Let us see how we can write a C program to print the ASCII value of all the characters.
Step 1: Declare variables
We will be using a character pointer to store the address of each character in the string.
char *str;
Step 2: Initialize the pointer
Initialize the pointer variable str to the null pointer. We will be using this pointer to print the ASCII value of all characters.
str = NULL;
Step 3: Read characters from the string
We will be using a for loop to iterate through the characters in the string and print the ASCII value.
for (i = 0; i < strlen(str); i++)
printf("%c\n", str[i]);
Step 4: Print the string
Print the string using printf.
printf("%s", str);
Step 5: Free memory
We will free the allocated memory using free.
free(str);
Another Method #2: print the ASCII value
If we want to know what each code represents we will have to look into the table of ASCII values. The table consists of 128 codes and each code represents a symbol.
To write the code we will have to use some basic programming concepts.
Programming is the art of writing a computer program or software to perform a specific task. The purpose of programming is to convert the thought of the programmer into the machine language of the computer.
A C program is a series of instructions that tells the computer how to perform a specific task.
The C language is the most commonly used programming language for writing programs. The C language is mainly used for creating a program for a personal computer.
Let us see how we can write a C program to print the ASCII value of all the characters.
Step 1: Declare variables
We will be using a character pointer to store the address of each character in the string.
char *str;
Step 2: Initialize the pointer
Initialize the pointer variable str to the null pointer. We will be using this pointer to print the ASCII value of all characters.
str = NULL;
Step 3: Read characters from the string
We will be using a for loop to iterate through the characters in the string and print the ASCII value.
Step 4: Print the string
for (i = 0; i < strlen(str); i++)
printf("%c\n", str[i]);
Print the string using printf.
printf("%s", str);
Step 5: Free memory
We will free the allocated memory using free.
free(str);
Conclusion:
I hope this program will help you to learn the ASCII values of all the characters. If you don’t like it then you can write a new C program by modifying the program according to your needs.