codescracker
c++

C++ Program to Print ASCII Value of Characters



« Previous Program Next Program »

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:

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.

// C++ Program to Print ASCII Value of a Character
// ----codescracker.com----

#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:

i = ch;

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:

// C++ Print ASCII Values of all Characters
// ----codescracker.com----

#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.

// C++ Print ASCII Values of all Characters in String
// ----codescracker.com----

#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++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker