C Program to Find ASCII Value of a Character

CodingTute

Updated on:

C Program to Find ASCII Value of a Character

In this example, you will learn how to find an ASCII value of a character. ASCII stands for American Standard Code for Information Interchange. It is one of the standards of representing characters to transfer information electronically.

You can get to know more about ASCII here.

Program to find ASCII Value of a Character

#include <stdio.h>
int main() {  
    char ch;
    printf("Enter a character: ");
    scanf("%c", &ch);  
    
    // %c displays the actual character
    // %d displays the integer value of a character
    printf("ASCII value of %c is %d", ch, ch);
    
    return 0;
}
Run Code on Online C Compiler

Output

Enter a character: B 
ASCII value of B is 66

Explanation

This C program asks the user to enter a character, reads the character using scanf(), and then displays the ASCII value of the character using printf().

The program first declares a variable ch of type char to store the user input. Then, it prints a message to the console asking the user to enter a character using printf(). The scanf() function is used to read the character entered by the user, and the address of ch is passed as the second argument to scanf() using the & operator to ensure that the value entered by the user is stored in the memory location reserved for the ch variable.

Next, the program uses printf() to display the ASCII value of the character entered by the user. The %c format specifier is used to display the actual character, and the %d format specifier is used to display the integer value of the character in ASCII encoding. The value of the ch variable is passed as the first argument to printf() to display the character itself, and it is also passed as the second argument to printf() with %d format specifier to display its ASCII value.

Finally, the program returns 0 to indicate successful completion of the main() function.

As per the ASCII, each character holds a unique ASCII value between 0-127.

Example: ‘A’ holds the value 65, ‘B’ holds the value 66 etc. For further values please refer the ASCII Table.

Follow us on Facebook, YouTube, Instagram, and Twitter for more exciting content and the latest updates.