Thursday, 21 August 2014

Filled Under: ,

C program to find ASCII value of any Character

Share

Here is a simple C program to find ASCII value of any character is given below.AS you know every character in C programming is given an integer value to represent it. That integer value is known as ASCII value of that character. For example: ASCII value of ‘A’ is 65. When a character is stored in variable of type char, the ASCII value of character is stored instead of that character itself character itself. For example: If you try to store character ‘a’ in a char type variable, ASCII value of that character is stored which is 97.

C program to find ASCII value of any character

In, this program user is asked to enter a character and this program will display the ASCII value of that character.
#include<stdio.h>
int main()
{
char c;
printf("Enter a character : ");
scanf("%c",&c); // takes a character from user
printf("ASCII value of %c = %d ",c,c);
return 0;
}
Output:
Enter a character : E
ASCII value of E = 69

Explanation
In this program, user is asked to enter a character and this character will be stored in variable c, i.e., the ASCII value of that character is stored in variable c. When, this value is displayed using conversion format string %c, the actual variable is displayed but, when this variable is displayed using format string %d, the ASCII value of that character is displayed.