How to use stoi In C++

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

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

Contact Us