ASCII value of a character
C program to print ASCII value of a character.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf("Enter the character: ");
scanf("%c",&ch);
printf("\nASCII value of '%c' = %d", ch,ch);
getch();
}
Output
Enter the character: g
ASCII value of 'g' = 103
Explanation
In C programming, every character has its own ASCII value (Nothing but an integer). Whenever a character is stored in the variable, instead of storing the character itself, the ASCII value of that character is stored.
For example, ASCII value of 'A' is 65 and ASCII value of '7' is 55.
Note that, digits when stored are considered as a character type. Digit characters have ASCII code values that differ from their numeric equivalents. ASCII value of '0' is 48, '1' is 49, '2' is 50, and so on.
In the program, we just read a character and print it using %d format specifier, which is used to print an integer value.
When we print the character value using %d format specifier, the ASCII code is printed.
and When we print the character value using %c format specifier, the character itself is printed.
For example, ASCII value of 'A' is 65 and ASCII value of '7' is 55.
Note that, digits when stored are considered as a character type. Digit characters have ASCII code values that differ from their numeric equivalents. ASCII value of '0' is 48, '1' is 49, '2' is 50, and so on.
In the program, we just read a character and print it using %d format specifier, which is used to print an integer value.
When we print the character value using %d format specifier, the ASCII code is printed.
and When we print the character value using %c format specifier, the character itself is printed.