HomeC++C++ Program to Find the ASCII Value of a Character

C++ Program to Find the ASCII Value of a Character

In this program, we will learn how to find the ASCII Value of a Character, using the C++ programming language.

What is the ASCII Value?

  • The ASCII or the American Standard Code For Information Interchange is a character encoding scheme used for electronics communication.
  • The ASCII value represents the character variable in numbers, and each character variable is assigned with some number range from 0 to 127.
  • Each Character is individually assigned with a value, and for alphabets the values are assigned without considering whether it is in upper case or lower case.
  • For Example – The Alphabet ‘p’ is assigned with ‘112’ and ‘P’ is assigned wit ’80’. The Character ‘?’ is assigned with ’63’ and ‘#’ is assigned with ’35’.

How to Find the ASCII Value of a Character in C++?

Let’s look at the below Source code, to implement the function to find the ASCII Value.

RUN CODE SNIPPET

Source Code

#include <iostream>
using namespace std;
int main()
{
    char b = 'p';
    cout <<"The ASCII value of " << b << " is: " << int(b);
    return 0;
}

Output

The ASCII value of p is: 112

Let’s break down the code and elaborate the steps, to understand better

#include <iostream>

  • This line which is called the header file is used in every C++ codding. #include statement tells the compiler to use available files.
  • The <iostream> is the name of the file which stands for Input and Output statement. This file includes the output statement ‘cout’, ‘cin’, and various other statements.

using namespace std;

  • The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

  • This line usually controls the function of the code, as it calls the functions to perform their tasks.
  • The int main()shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.

{ }

  • The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
  • Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.

  1. First declare b as a Character with char b and assign the values to be found  char b='p'.
  2. The statement char is used to hold a character as the input value.
  3. Next display the output statements with the cout and the Insertion Operator ‘ << ‘.
  4. When using characters/sentences, write them within quotes ” “, "The ASCII value" followed by ‘ << ‘ and the character to display them.
  5. Using the output statement cout and <<b<<to display the character in the statement, we then end the statement with <<int (b). This will find and display the ASCII value of the character ‘p’ as an integer.
  6. End the function with return 0; this returns the function to main( ).

Now that you have understood how the source code performs its function, try it yourself with different values to it understand better.

Note: The ‘ \n ‘ in the code is used to insert a new line, to understand how it works exclude it from the code and work with it.

Additional Source Code

You can also try the source code given below, the following code is structured as to receive the values from the user rather than include the values directly in the code.

RUN CODE SNIPPET
#include <iostream>
using namespace std;
int main()
{
    char b;
    cin >> b;
    cout <<"Enter a Character: "<< b;
    cout <<"\nThe ASCII value of " << b << " is: " << int(b);
    return 0;
}

Input

p

Output

Enter a Character: p
The ASCII value of p is: 112
  • The structure is similar to the previous code, the extra function we include is the cin>>b , this statement is used to collect and store the values that the user enters the assigned integer.
  • We use <<b to display the stored values in the output, and the int b displays the answer as an integer.
  • The rest of the statements and functions are similar, to find the ASCII value of the character.

Share:

Leave a Reply

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find GCD of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022