Write a C++ Program Which Prints Printable Ascii Characters and Integer Values

C++ Program to Print ASCII Value of Characters



In this article, you will learn and get code to print ASCII value of character in C++. Here are the list of programs on printing ASCII value, available in this article:

  • Print ASCII Value of a Character entered by User
  • Print ASCII Value of all Characters
  • Print ASCII Value of all Character in a String entered by User

What is ASCII ?

ASCII stands for American Standard Code for Information Interchange. It is a character encoding standard for electronic communication.

Print ASCII Value of a Character

This program receives a character as input from user at run-time, and prints its ASCII value.

            #include<iostream>            using namespace            std;            int            main() {            char            ch;            int            i;            cout<<"Enter the Character: ";            cin>>ch;     i = ch;            cout<<"\nASCII Value = "<<i;            cout<<endl;            return            0; }

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program print ascii values

Now supply the input say c as character to find and print its ASCII value as shown in the snapshot given below:

print ascii value of a character c++

The following statement:

initialized the ASCII value (an integer value) of ch to i. Because user entered c as character input, therefore c (small letter) gets stored in ch and using the above statement, ASCII value of ch (99) gets stored to (or initialized in) i. Because i is of int (integer) type variable. Now just print the value of i as ASCII value of c on output.

Print ASCII Values of All Characters

To print ASCII values of all characters in C++ programming, use a for loop to execute a block of code 255 times. That is, the loop variable i starts with 0 and ends with 255.

Everytime the value of i gets initialized to ch, which is of char (character) type variable. So the corresponding character to the value of i gets initialized to ch. Here the value of i is considered as ASCII value. Therefore, if i's value is 65, then the character whose ASCII value is 65 gets initialized to ch. Because 65 is the ASCII value of A. So at that time, ch=A

Print ch as character and the value of i as its ASCII value, each time while entering into the loop's body as shown in the program given below:

            #include<iostream>            using namespace            std;            int            main() {            char            ch;            int            i;            cout<<"Character\t\tASCII Value\n";            for(i=0; i<255; i++)     {         ch = i;            cout<<ch<<"\t\t\t"<<i<<endl;     }            cout<<endl;            return            0; }

Here is its sample output. The snapshot given below shows small part of sample output produced by this program:

print ascii value of all characters c++

Print ASCII Value of All Characters in String

This is the last program on printing ASCII value. This program receives a string input from user, and prints ASCII value of each and every character of the given string.

            #include<iostream>            #include<stdio.h>            using namespace            std;            int            main() {            char            ch, str[200];            int            i=0, val;            cout<<"Enter the String: ";            gets(str);            cout<<"\nCharacter\t\tASCII Value\n";            while(str[i])     {         ch = str[i];         val = ch;            cout<<ch<<"\t\t\t"<<val<<endl;         i++;     }            cout<<endl;            return            0; }

Here is its sample run with user input, codescracker:

print ascii value of characters in string

Here is another sample run with user input, codes cracker:

print ascii value of characters c++

Same Program in Other Languages

  • C Print ASCII Values
  • Java Print ASCII Values
  • Python Print ASCII Values

C++ Online Test


« Previous Program Next Program »




Write a C++ Program Which Prints Printable Ascii Characters and Integer Values

Source: https://codescracker.com/cpp/program/cpp-program-print-ascii-values.htm

0 Response to "Write a C++ Program Which Prints Printable Ascii Characters and Integer Values"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel