Find ASCII Value of a Character in C

Exercise:

Write a C Program to find ascii value of a character.

Click Here to View the Solution!
#include <stdio.h>
int main() {
    char c;
    printf("Enter a character: ");
    scanf("%c", &c);

    // %d displays the integer value of a character
    // %c displays the actual character
    printf("ASCII value of %c = %d", c, c);
    return 0;
}
Click Here to View the Output!
 Enter a character: c
 ASCII value of c = 99
Click Here to View the Explanation!
  • This program is used to print the ASCII value of a character.
  • In the main() method, a character type variable c is initialized.
  • The program request the user to enter a character and store it in c using the scanf() function. %c is used in the parameter of scanf() that is used to take only characters.
  • A printf() function is used to print the character along with its ASCII value on the screen.
  • The function uses %d and %c conversion specifiers in the placeholder format %c = %d.
  • The %c placeholder displays the character itself whereas since each character as its corresponding integer value from 0 to 127, %d placeholder will display the ASCII value of the input character.
  • The return 0 statement exits the program.