C Program to print the ASCII value of a character.
C Program to print the value of a character.
- In C programming, a character variable stores the ASCII value rather than the character itself.
- The ASCII value represents the character variable in numbers, and each character variable is assigned with some unique number range from 0 to 127.
- For eg., the ASCII value of "A" is 65.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", ch, ch);
return 0;
}
OUTPUT
Enter a character: L ASCII value of L = 76
Explanation:
- In the program, the user is asked to enter a character. The character is stored in variable ch.
- When the character is specified using %c format specifier then it will display the character itself.
- When a character is specified using %d format specifier then it will display the ASCII value of the respective character.
Comments
Post a Comment