Converting char value to int by adding 0

C++




// C++ program to convert
// char to int using typecasting by adding zero
 
#include <iostream>
using namespace std;
 
 //Driver code
int main()
{
    char charvalue = 'a';
    int number = (int(charvalue)+0);
    cout << number;
    return 0;
}
 
 //this code is contributed by uomkar369


Output

97


C++ Program For char to int Conversion

Here we will see how to convert char to int using a C++ program. There are 6 ways to convert char to int in C++:

  1. Using Typecasting.
  2. Using static_cast.
  3. Using sscanf().
  4. Using stoi().
  5. Using atoi().
  6. Using string stream.

Let’s discuss each of these methods in detail.

Similar Reads

1. Using Typecasting

Method 1:...

2. Using static_cast

...

3. Using sscanf

...

4. Using stoi

...

5. Using atoi

The character can be converted to an integer using the static_cast function. Below is the C++ program to convert char to int value using static_cast:...

6. Using stringstream

...

6. Method: Converting char value to int by adding 0

Reads data from s and stores it in the places specified by the additional arguments in the parameter format. Below is the C++ program to convert char to int using sscanf():...