C Program to find ASCII value of a character
We will learn here how to write a C program to find ASCII value of a character. When we assign a character to a character variable in C . It does not holds the character, it holds that character’s ASCII value. For example char cv = B . In this example , variable cv will hold ASCII value of B i.e. 66 . If we print cv it will output 66 rather than B .
Let us see with a practical program how to write a C program to find ASCII value of a character.
Program to print ASCII Value
/****************************************************************************** C Program to find ASCII value of a character https://bptutorials.com *******************************************************************************/ #include <stdio.h> int main() { char cv; printf("Enter a character: "); scanf("%c", &cv); // %d displays integer value of a character // %c displays the character itself printf("ASCII value of %c = %d", cv, cv); return 0; }
Output :
Enter a character: B ASCII value of B = 66
First we declare character variable cv and asked user to input a character and store user input in variable cv. %d is used to display integer value of character and %c is used to display the character itself.