C++ programs to print all ASCII values and their equivalent characters

Introduction

Welcome to our exploration of ASCII values and their equivalent characters in C++. ASCII (American Standard Code for Information Interchange) is a widely used character encoding standard that assigns numeric codes to represent characters. Understanding ASCII values and their corresponding characters is fundamental in programming, especially when dealing with text processing, character manipulation, and encoding conversions.

In this article, we will delve into writing C++ programs to print all ASCII values and their equivalent characters. Whether you’re a beginner learning the basics of programming or an experienced developer seeking to refresh your knowledge, this guide is designed to help you understand ASCII encoding and its practical applications in C++ programming.

Prerequisites

  • Basic knowledge of C++ syntax and concepts.
  • A C++ compiler installed on your system (such as GCC for Linux, MinGW for Windows, or Clang for macOS).

Now, let’s dive into three different examples of C++ programs to print all ASCII values and their corresponding characters, each showcasing a unique approach to implementation.

Example 1: Using a Loop to Print ASCII Values and Characters

C++
#include <iostream>
using namespace std;

int main() {
    cout << "ASCII Values and Their Equivalent Characters:" << endl;
    for (int i = 0; i <= 127; ++i) {
        cout << "ASCII Value: " << i << " | Character: " << char(i) << endl;
    }

    return 0;
}

Explanation

  • We include the iostream library for input and output operations.
  • Inside the main function, we use a for loop to iterate through ASCII values from 0 to 127.
  • Using the char(i) function, we convert each ASCII value to its equivalent character and print them in the desired format.

Output (Partial)

C++
ASCII Values and Their Equivalent Characters:
ASCII Value: 0 | Character: 
ASCII Value: 1 | Character: ☺
ASCII Value: 2 | Character: ☻
...
ASCII Value: 65 | Character: A
ASCII Value: 66 | Character: B
...
ASCII Value: 97 | Character: a
ASCII Value: 98 | Character: b
...
ASCII Value: 126 | Character: ~
ASCII Value: 127 | Character: ⌂

Example 2: Using Arrays to Store ASCII Values and Characters

C++
#include <iostream>
using namespace std;

int main() {
    int asciiValues[128];
    char characters[128];

    for (int i = 0; i <= 127; ++i) {
        asciiValues[i] = i;
        characters[i] = char(i);
    }

    cout << "ASCII Values and Their Equivalent Characters:" << endl;
    for (int i = 0; i <= 127; ++i) {
        cout << "ASCII Value: " << asciiValues[i] << " | Character: " << characters[i] << endl;
    }

    return 0;
}

Explanation

  • We declare two arrays: asciiValues to store ASCII values and characters to store equivalent characters.
  • Using a for loop, we populate these arrays with ASCII values and their corresponding characters.
  • Finally, we iterate through the arrays to print the ASCII values and characters.

Output

C++
ASCII Values and Their Equivalent Characters:
ASCII Value: 0 | Character: 
ASCII Value: 1 | Character: ☺
ASCII Value: 2 | Character: ☻
...
ASCII Value: 65 | Character: A
ASCII Value: 66 | Character: B
...
ASCII Value: 97 | Character: a
ASCII Value: 98 | Character: b
...
ASCII Value: 126 | Character: ~
ASCII Value: 127 | Character: ⌂

Example 3: Using Functions to Print ASCII Values and Characters

C++
#include <iostream>
using namespace std;

void printASCII() {
    cout << "ASCII Values and Their Equivalent Characters:" << endl;
    for (int i = 0; i <= 127; ++i) {
        cout << "ASCII Value: " << i << " | Character: " << char(i) << endl;
    }
}

int main() {
    printASCII();

    return 0;
}

Explanation

  • We define a function printASCII to encapsulate the logic for printing ASCII values and characters.
  • Inside the function, we use a for loop to iterate through ASCII values and print them.

Output

C++
ASCII Values and Their Equivalent Characters:
ASCII Value: 0 | Character: 
ASCII Value: 1 | Character: ☺
ASCII Value: 2 | Character: ☻
...
ASCII Value: 65 | Character: A
ASCII Value: 66 | Character: B
...
ASCII Value: 97 | Character: a
ASCII Value: 98 | Character: b
...
ASCII Value: 126 | Character: ~
ASCII Value: 127 | Character: ⌂

Conclusion

In this article, we explored different approaches to write C++ programs for printing all ASCII values and their equivalent characters. Understanding ASCII encoding is essential for working with text-based data and character manipulation tasks in programming. By experimenting with these examples and exploring further, you can deepen your understanding of ASCII encoding and its applications in C++ programming.