ASCII value in C

ASCII stands for American Standard Code for Information Interchange. It is a character encoding scheme used for electronics communication.

In C programming language, a character variable does not contain a character value itself rather the ASCII value of the character variable.

In this lesson, you will learn how to find the ASCII value of a character.

Each character or a special character is represented by some ASCII code, and each ASCII code occupies 7 bits in memory.

The ASCII value represents the character variable in numbers. For example, the ASCII value of ‘A’ is 65.

Example – Display the ascii value of the character

#include <stdio.h>  
int main()  
    char ch;    // variable declaration  
    printf("Enter a character: ");
    scanf("%c",&ch);  // user input  
    printf("\n The ascii value of the ch variable is : %d", ch);  
    return 0;  
  • C code: We use format specifier here to give the numeric value of character. Here %d is used to convert character to its ASCII value.
  • In the above example, first user will give the character input, and then input will get stored in the ‘ch’ variable.
  • If we print the value of the ‘ch’ variable by using %c format specifier, then it will display ‘A’ because we have given the character input as ‘A’, and if we use the %d format specifier then its ASCII value will be displayed, i.e., 65.
Example – Display the ascii value of the character – Output

The above output shows that the user gave the input as ‘A’, and after giving input, the ascii value of ‘A’ will get printed, i.e., 65.