ASCII Value in C
ASCII (American Standard Code for Information Interchange) is a character encoding standard that assigns a unique numerical value (an integer) to each printable and control character in the English alphabet and some other special characters. The ASCII standard uses a 7-bit binary code to represent these characters, allowing for a total of 128 different characters.
In C programming, you can find the ASCII value (also called ASCII code) of a character using the int data type because the ASCII value is represented as an integer. You can do this by simply casting the character to an int data type.
Here's a simple example in C to illustrate the use of ASCII values:
#include <stdio.h>
int main() {
char character;
printf("Enter a character: ");
scanf("%c", &character);
// Get the ASCII value of the character
int asciiValue = (int)character;
// Display the character and its ASCII value
printf("Character: %c\n", character);
printf("ASCII Value: %d\n", asciiValue);
return 0;
}
In this program, we:
-
Declare a
character
variable to store the user's input.
- Prompt the user to enter a character using printf and read the character using
scanf
.
- Convert the character to its ASCII value by casting it to an
int
.
- Display both the original character and its ASCII value using
printf
.
When you run this program and input a character, it will display the character you entered and its corresponding ASCII value. For example, if you input 'A', it will display:
Enter a character: A
Character: A
ASCII Value: 65
This demonstrates how you can use C to find and display the ASCII value of a character.