How to use the ASCII table in C language?

The ASCII table is a character encoding table that contains 128 characters, including numbers, letters, punctuation marks, and some special characters. In C language, you can obtain the ASCII code value of a character by converting a variable of character type to integer type.

Here are some examples using the ASCII table:

  1. Print the ASCII value of the character.
#include <stdio.h>

int main() {
    char ch = 'A';
    int ascii = ch;
    printf("ASCII value of %c is %d\n", ch, ascii);
    return 0;
}

Output:

ASCII value of A is 65
  1. Print characters according to their ASCII values.
#include <stdio.h>

int main() {
    int ascii = 65;
    char ch = ascii;
    printf("Character for ASCII value %d is %c\n", ascii, ch);
    return 0;
}

Result:

Character for ASCII value 65 is A
  1. Print characters and their corresponding ASCII values from the ASCII table using loops.
#include <stdio.h>

int main() {
    int i;
    for (i = 0; i <= 127; i++) {
        printf("Character for ASCII value %d is %c\n", i, i);
    }
    return 0;
}

This example will print all characters and their corresponding ASCII values from 0 to 127.

I hope the above examples can help you understand how to use the ASCII table.

bannerAds