Open In App

C++ Program For char to int Conversion

Last Updated : 26 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, we cannot directly perform numeric operations on characters that represent numeric values. If we attempt to do so, the program will interpret the character's ASCII value instead of the numeric value it represents. We need to convert the character into an integer.

Example:

Input: '9'
Output: 9
Explanation: The character '9' is converted into integer.

In C++, there are several ways to convert a character to an integer:

Using ASCII Code Conversion

C++ uses the ASCII character set, which uses a 7-bit integer (ASCII values) to represent 128 characters. It stores the numeric (digit) characters in a sequential manner. The ASCII value of '0' is 48, '1' is 49, and so on up to '9' as 57. So, by subtracting the character '0' from a character representing a digit, we can convert the character to integer.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    char ch = '9';

    // Converting the char 
    // to integer
    int num = ch - '0';
    cout << num;
    return 0;
}

Output
9

If you want to convert multiple characters representing multi-digit number, refer to the following article - Convert String to int in C++

Using atoi() Function

In C++, we can also use the atoi() function to convert a character into an integer; it takes a pointer to a character as an argument.

Example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    char ch = '9';

    // Converting the char 
    // to integer
    int num = atoi(&ch);
    cout << num;
    return 0;
}

Output
9

Next Article
Practice Tags :

Similar Reads