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++:
- Using Typecasting.
- Using static_cast.
- Using sscanf().
- Using stoi().
- Using atoi().
- Using string stream.
Let’s discuss each of these methods in detail.
1. Using Typecasting
Method 1:
- Declare and initialize our character to be converted.
- Typecast the character to convert character to int using int.
- Print the integer using cout.
Below is the C++ program to convert char to int value using typecasting:
C++
#include <iostream>
using namespace std;
int main()
{
char ch = 'A';
cout << int(ch);
return 0;
}
|
The time complexity is O(1) and The auxiliary space is also O(1)
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++
#include <iostream>
using namespace std;
int main()
{
char ch = '5';
cout << int(ch) - 48 << "\n";
cout << int(ch - '0');
return 0;
}
|
Method 2:
- Declare and initialize our character to be converted.
- Declare another variable as int N and assign the character ch to the N.
- Print the integer using cout.
Below is the C++ program to convert char to int value using typecasting:
C++
#include <iostream>
using namespace std;
int main()
{
char ch = 'a';
int N = int(ch);
cout << N;
return 0;
}
|
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++
#include <iostream>
using namespace std;
int main()
{
char ch = 'A';
int N = static_cast<int>(ch);
cout << N;
return 0;
}
|
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++
#include <iostream>
using namespace std;
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++
#include <iostream>
#include <string>
using namespace std;
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++
#include <iostream>
using namespace std;
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++
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
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++
#include <iostream>
using namespace std;
int main()
{
char charvalue = 'a';
int number = (int(charvalue)+0);
cout << number;
return 0;
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
27 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...