ASCII Value

What is ASCII code?

• ASCII stands for American Standard Code for Information Interchange.

• It is a character encoding scheme used for electronic communication.

• Each character or special character is represented by an ASCII code, which occupies 7 bits in memory.

In C programming, a character variable contains the ASCII value of the character. The ASCII value represents the character in numbers, with a range from 0 to 127. For example, the ASCII value of 'A' is 65.

Example:

#include <stdio.h>

int main() {
    char ch = 'A'; // Character 'A'
    int asciiValue = ch; // ASCII value of 'A'

    printf("The ASCII value of %c is %d\n", ch, asciiValue);
    return 0;
}

Output:

The ASCII value of A is 65

Explanation:

• We assign the character 'A' to the variable ch.

• The ASCII value of 'A' is 65, so 65 will be stored in the character variable ch.

• The program prints: The ASCII value of A is 65.

Character Encoding with ASCII

ASCII uses a numerical representation for characters and special symbols. Here are some common ASCII values:


• 'A' = 65

• 'B' = 66

• 'a' = 97

• 'b' = 98

• '0' = 48

• '1' = 49

• ' ' (space) = 32

Using ASCII Values in C

You can perform various operations using ASCII values in C. For example, to convert a lowercase character to uppercase:

Example:

#include <stdio.h>

int main() {
    char lower = 'a'; // Lowercase 'a'
    char upper = lower - 32; // Convert to uppercase 'A'

    printf("Lowercase: %c, Uppercase: %c\n", lower, upper);
    return 0;
}

Explanation:

• The ASCII value of 'a' is 97.

• Subtracting 32 from 97 gives 65, which is the ASCII value of 'A'.

• The program prints: Lowercase: a, Uppercase: A.