Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Conversion of Char to Int in C++
2.1.
Typecasting
2.2.
Static_cast
2.3.
sscanf()
2.4.
stoi()
2.5.
atoi()
2.6.
stringstream
3.
Converting char Value to int by Adding 0
4.
Frequently Asked Questions
4.1.
What is typecasting?
4.2.
If stoi() function can be used in the case of string?
4.3.
Can a char become an int?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Convert char to int in C and C++

Author Sonu Deo
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In many cases, we encounter a situation where input is provided in string or character format which needs to be processed as a string. For example, when we collect information from a web page, we have to parse them in integer data type when required. 

Introduction

In this article, we will be exploring some methods to convert char to int in c++. You can also do a free certification of Basics of C++.

Conversion of Char to Int in C++

Let us learn about different methods to convert char to int in c++. 

In further sections, we will also be looking for some methods which convert strings in c++ to integers if those are in the range of the respective data type.

There are six most common methods to convert a character data type to an integer data type in C++:

  • Typecasting
  • Static_cast
  • sscanf()
  • stoi()
  • atoi()
  • String stream
     

Let us now understand each of these methods with the help of examples.

Typecasting

Typecasting is a method to convert the data type of a variable into another data type wherever it is applicable. It can be done manually and automatically. We will declare the character to be converted and initialize it. Typecasting it and assigning it to an integer variable will convert the char to int c++.

Following is the implementation of char to int in c++ using Typecasting.

#include <bits/stdc++.h>
using namespace std;
int main(){
    char c = 'a';
    int val = (int)c; //typecasting
    cout<<val;
}

 

Output:

97

 

This code will typecast and assign the variable c into integer val. And this value will be the ASCII value of the character set to c.

Also, see Literals in C.Fibonacci Series in C++
 

Static_cast

We will now look at a method of converting char to int in c++ using static_cast.

Static_cast converts between types using a combination of implicit and user-defined conversions. What it does is take a character that needs to be converted into an integer type as an argument and return its ASCII value.

Following is the implementation of char to int in c++ using the Static_cast.

#include <bits/stdc++.h>
using namespace std;
int main(){
    char c = 'a';
    int val = static_cast<int>(c);
    cout<<val;
}

 

Output:

97

 

You can try and compile with the help of online c++ compiler.

Here static_cast will convert the character passed as the argument in the static_cast function to its ASCII value.

sscanf()

Reads data from s and stores it in the locations specified by the additional arguments, just like scanf, but this time from s rather than the standard input (stdin). Its specialty is that it takes input from the buffer, which needs to be a pointer variable.

Following is the implementation of char to int in c++ using the sscanf() function.

#include <bits/stdc++.h>
using namespace std;
int main(){
    const char *s = "1411";
    int val;
    sscanf(s, "%d", &val);
    cout << val;
}

 

Output:

1411


You can practice by yourself with the help of online c++ compiler.

sscanf() works the same as scanf; here, we just passed the variable s instead of the standard input and assigned it to an integer type val. Here *s is in the buffer and its value is assigned to val with its data type being an integer.

stoi()

stoi stands for string to integer.

Parses str interpret its content as an integral number of the specified base, which is returned as an int value. What stio does, basically, is to take the string or character array which needs to be converted to integer type as an argument, and return an integer value of that string. 

Following is the implementation of char to int in c++ using the stoi() function.

#include <bits/stdc++.h>
using namespace std;
int main(){
    char s[] = "1411";
    int val = stoi(s);
    cout<<val;
}

 

Output:

1411

 

Here stoi function takes the argument char array which is going to be converted into integer type and then converts it into integer type val.

atoi()

Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int. What it does is take a sequence of characters as input and return its numerical value in integer format.

Following is the implementation of char to int in c++ using atoi() function.

#include <bits/stdc++.h>
using namespace std;
int main(){
    const char *s = "1411";
    int val = atoi(s);
    cout<<val;
}

 

Output:

1411

 

Here atoi() function will parse s into val, an integer type. Also if it encounters any character that is not a digit it will return the number formed with the previous characters.

stringstream

A stringstream is a connection between a string object and a stream that allows you to read from it like a stream (like cin). Stringstream requires the sstream header file to be included. When processing input, the stringstream class comes in handy.

Following is the implementation of char to int in c++ using stringstream.

#include <bits/stdc++.h>
using namespace std;
int main(){
    stringstream string;
    string << "1411";
    int val;
    string >> val;
    cout<<val;
}

 

Output:

1411

 

Here we are taking the input in the form of a string stream, then passing the value of the string into val, which is an integer.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Converting char Value to int by Adding 0

In C++, when you add 0 to a char, the character is implicitly converted to its integer ASCII value. This process is known as type promotion, where the char type is promoted to int by utilizing the ASCII value of the character. Here's a C++ example demonstrating this :

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

 

Output:

"The ASCII value of '5' is: 53"

Frequently Asked Questions

What is typecasting?

Typecasting is a method or process that converts a data type into another data type in both ways, manually and automatically.

If stoi() function can be used in the case of string?

Yes, we can convert the string into an integer. But the string should be in the range of integer data type used.

Can a char become an int?

In programming languages like C++ and Java, a char can indeed be converted to an int. This conversion happens implicitly, utilizing the ASCII value of the character. For example, when a character representing a digit, like '5', is converted to an integer, it becomes the corresponding ASCII value (53 in the case of '5'). This automatic type conversion is a feature of the language's type system, which facilitates operations between different data types.

Conclusion

In this article, we have discussed various methods to convert char to int in C++. We also learned each of these methods in depth with the help of an example of each.

If you think this blog has helped you enhance your knowledge about the above question, and if you would like to learn more, check out our articles. 


Visit our website to read more such blogs. Make sure you enroll in the courses we provide, take mock tests, solve problems, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations.

Previous article
Segmentation Fault in C/C++
Next article
What are Non-alphanumeric Characters?
Live masterclass