Write a C Program to Print the ASCII Value of a Character.
In this Article, you will learn how to print the ASCII value of a character.
In this C program, we'll read a character from the user and then print its corresponding ASCII value without using any functions other than the standard I/O functions provided by stdio.h.
Here's a C program to print the ASCII value of a character:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
// Prompt the user to enter a character
printf("Enter a character: ");
scanf("%c", &ch);
// Print the ASCII value of the character
printf("The ASCII value of '%c' is %d\n", ch, ch);
}
- We declare a variable ch of type char to store the user's input character.
- We then use printf to prompt the user to enter a character.
- Next, we use scanf to read a character from the user, and we store it in the variable ch.
- Finally, we use another printf statement to print the ASCII value of the character. In C, characters are represented using their ASCII values, so we can simply print the variable ch as an integer to display its ASCII value.
When you run this program, they will be asked to enter a character. After entering the character, the program will display its corresponding ASCII value on the screen.
Output: