Skip to content
Related Articles
Open in App
Not now

Related Articles

C++ Program For char to int Conversion

Improve Article
Save Article
  • Last Updated : 15 Oct, 2022
Improve Article
Save Article

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.

1. Using Typecasting

Method 1:

  1. Declare and initialize our character to be converted.
  2. Typecast the character to convert character to int using int.
  3. Print the integer using cout.

Below is the C++ program to convert char to int value using typecasting: 

C++




// C++ program to convert
// char to int (ASCII Value) using typecasting
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    char ch = 'A';
    cout << int(ch);
    return 0;
}


Output

65

If a numeric character needs to be typecasted into the integer value then either we can subtract 48 or ‘0’ and then typecast the numeric character into int.

Below is the C++ program to convert char to integer value using typecasting:

C++




// C++ program to convert
// char to int (integer value) using typecasting
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    char ch = '5';
 
    // Subtracting 48 will produce desired results
    cout << int(ch) - 48 << "\n";
 
    // Also subtracting '0' will result in same output
    cout << int(ch - '0');
    return 0;
}
 
// This code is contributed by Susobhan Akhuli


Output

5
5

Method 2:

  1. Declare and initialize our character to be converted.
  2. Declare another variable as int N and assign the character ch to the N. 
  3. Print the integer using cout. 

Below is the C++ program to convert char to int value using typecasting:

C++




// C++ program to convert
// char to int (ASCII value) using typecasting
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    char ch = 'a';
    int N = int(ch);
    cout << N;
    return 0;
}


Output

97

2. Using static_cast

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:

C++




// C++ program to convert char
// to int (ASCII Value) using static_cast
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    char ch = 'A';
    int N = static_cast<int>(ch);
    cout << N;
    return 0;
}


Output

65

3. Using sscanf

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():

C++




// C++ program to convert char
// to int using sscanf()
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
  const char *s = "1234";
  int x;
  sscanf(s, "%d", &x);
  cout << "\nThe integer value of x : " << x;
  return 0;
}


Output

The integer value of x : 1234

4. Using stoi 

The stoi() function in C++ converts a string to an integer value. Below is the C++ program to convert char to int using stoi():

C++




// C++ program to convert char
// to int using stoi()
#include <iostream>
#include <string>
using namespace std;
 
// Driver code
int main()
{
  char s1[] = "45";
  int x = stoi(s1);
  cout << "The integer value of x : " << x;
  return 0;
}


Output

The integer value of x : 45

5. Using atoi

If the execution is successful, the atoi() method returns the converted integer value. If the given string cannot be converted to an integer, it will return 0. Below is the C++ program to convert char to int using atoi():

C++




// C++ program to convert char
// to int using atoi()
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
  const char *str = "1234";
 
  int y = atoi(str);
  cout << "\nThe integer value of y :" << y;
  return 0;
}


Output

The integer value of y :1234

6. Using stringstream 

A stringstream connects a string object to a stream, allowing you to read from it as if it were a stream (like cin). Stringstream requires the inclusion of the sstream header file. The stringstream class comes in handy when processing input.
Below is the C++ program to convert char to int using string stream:

C++




// C++ program to convert char
// to int using string stream
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
 
// Driver code
int main()
{
    stringstream string;
    string << "5";
    int n;
    string >> n;
    cout << "Integer value is: " << n;
    return 0;
}


Output

Integer value is: 5

6. Method: 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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!