Understanding of ASCII Values in C Programming
In C programming, ASCII (American Standard Code for Information Interchange) is a character encoding standard that
assigns a unique numeric value to each printable and control character. ASCII values are represented as integers, making it easy to work with characters in a computer program.Here’s a brief overview of how ASCII values work in C, along with an example:
ASCII Character Set:
ASCII represents characters using 7-bit binary numbers, resulting in 128 unique characters. These characters include the English alphabet (both uppercase and lowercase), digits, punctuation marks, control characters (like newline and tab), and special symbols.
ASCII Values:
Each character in the ASCII character set is assigned a unique decimal value. For example:
- ‘A’ is represented by the ASCII value 65.
- ‘a’ is represented by the ASCII value 97.
- ‘0’ is represented by the ASCII value 48.
- ‘!’ is represented by the ASCII value 33.
Using ASCII Values in C:
In C, you can find the ASCII value of a character by using the int
data type. For example, you can assign the ASCII value of a character to a variable like this:
char myChar = 'A';
int asciiValue = myChar;
Now, the variable asciiValue
will contain the ASCII value of ‘A’, which is 65.
Example Program:
Here’s a simple C program that demonstrates how to use ASCII values to convert characters to their corresponding numeric values and vice versa:
#include <stdio.h>
int main() {
char myChar = 'A';
int asciiValue = myChar;
printf("Character: %c\n", myChar);
printf("ASCII Value: %d\n", asciiValue);
// Convert an ASCII value back to a character
int anotherAsciiValue = 97;
char anotherChar = (char)anotherAsciiValue;
printf("ASCII Value: %d\n", anotherAsciiValue);
printf("Character: %c\n", anotherChar);
return 0;
}
When you run this program, it will output:
Character: A
ASCII Value: 65
ASCII Value: 97
Character: a
As shown in the example, you can convert a character to its ASCII value and vice versa using simple assignments in C. This is a fundamental concept when working with characters and strings in C programming.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.