Write a program to print ASCII table in C language

Exploring ASCII Characters: A "C Program to Print ASCII Table"

Introduction:
ASCII (American Standard Code for Information Interchange) is a character encoding standard used to represent text in computers and communication equipment. Each ASCII character is represented by a unique 7-bit binary number (0-127), allowing computers to understand and display a wide range of characters, including letters, digits, punctuation marks, and control characters. In this blog post, we'll explore ASCII characters and demonstrate how to write a C program to print the ASCII table, providing a visual representation of ASCII characters and their corresponding values.

Understanding ASCII Characters:
ASCII characters are divided into several categories:
  • Printable Characters: Represented by ASCII codes 32 to 126, including letters, digits, punctuation marks, and special symbols.
  • Control Characters: Represented by ASCII codes 0 to 31 and 127, used to control peripheral devices and communication protocols.
Writing a C Program to Print ASCII Table:
To print the ASCII table, we'll create a C program that iterates through ASCII characters and prints their corresponding values in a tabular format.

#include <stdio.h>
int main() {
    printf("ASCII Table:\n");
    printf("+--------+----------+\n");
    printf("| ASCII  | Character|\n");
    printf("+--------+----------+\n");
    // Iterate through ASCII characters and print their values
    for (int i = 0; i <= 127; i++) {
        // Print non-printable characters as space
        if (i < 32 || i == 127) {
            printf("|  %3d   |    %c     |\n", i, ' ');
        } else {
            printf("|  %3d   |    %c     |\n", i, i);
        }
    }
    printf("+--------+----------+\n");
    return 0;
}


In this program:
  • We use the `printf()` function to print the ASCII table header and separator lines.
  • We iterate through ASCII characters from 0 to 127.
  • For each ASCII character, we print its ASCII code and corresponding character value.
  • We handle non-printable characters (ASCII codes 0 to 31 and 127) by printing them as spaces.
Running the Program:
Compile the program using a C compiler (e.g., gcc) and execute the generated executable. The program will print the ASCII table in a tabular format, displaying ASCII codes and corresponding characters.

Conclusion:
In this blog post, we've explored ASCII characters and demonstrated how to write a C program to print the ASCII table. By iterating through ASCII characters and printing their values, we can visualize the ASCII character set and understand the correspondence between ASCII codes and characters. Whether you're learning about character encoding standards, debugging character-related issues, or simply curious about ASCII characters, printing the ASCII table programmatically using C provides a convenient way to explore and understand ASCII characters and their representations.

Comments