C program to Find ASCII Value of a Character

ASCII value is a numeric value assigned to every defined character (including alphabets, punctuation marks, special symbols, numbers and control characters.).
For example, the ASCII value of ‘A’ is 65 whereas ASCII value of ‘a’ is 97.

In C programming a character variable holds the ascii value instead of the character itself. That means, if you assign ‘b’ to a charater type variable, it will hold the ASCII value of ‘b’ (i.e., 98).

In this program, we will learn how to find the ASCII value of a character.

To understand this program, you should have knowledge of the following topic in C :

  • Variables
  • Format Specifiers

Program

#include <stdio.h>
int main()
{
    char input;
    printf("Enter any character: ");
    scanf("%c", &input);
    printf("The ASCII value of %c is %d", input, input);
    return 0;
}

Output

Enter any character: T

The ASCII value of T is 84

Explanation

  • We have declared a character type variable named ‘input’ which will store the character entered by the user.
  • Then after, with the help of printf() function a message is displayed to the user “Enter a character”.
  • By using the scanf() function, we are collecting the user input and storing it in the variable named ‘input’.
  • Lastly, the character as well as its ASCII value are displayed by using ‘%c’ and ‘%d’ format specifiers respectively.

Keep in mind that the ASCII values of alphabets varies as the case changes. That means ASCII values of small letters are different from the capital letters. 

For example

#include <stdio.h>
int main()
{
    char inputone = 'a';
    char inputtwo = 'A';
    printf("The ASCII value of %c is %d \n", inputone, inputone);
    printf("The ASCII value of %c is %d", inputtwo, inputtwo);
    return 0;
}

Output

The ASCII value of a is 97

The ASCII value of A is 65