Convert Char to Int in C and C++



In C/C++, char represents a single character, whereas int is an integer that can store both positive and negative values, but not fractional or decimal values.

If you are given a character and want to convert it into an integer, it is easy and can be achieved with the different approaches. In this article, we will learn how we can convert a given character to an integer with different approaches.

The following are some of the ways to convert a character to an integer:

Converting Character to Integer Using sscanf() Function

The sscanf() function is used to read formatted input from a string. You can use it to extract an integer value from a string containing numeric text.

Example

In this example, we demonstrate the sscanf() function to convert the character into an integer form:

C C++
#include <stdio.h>

int main() {
   char ch = '7';
   int num;
   
   sscanf(&ch, "%d", &num);
   
   printf("The result of converted integer is %d\n", num);
   return 0;
}

Output

The above program produces the following result:

The result of converted integer is 7
#include <iostream>
#include <cstdio>
using namespace std;

   int main() {
   char ch = '1';
   int num;
   sscanf(&ch, "%d", &num);
   cout << "The result of converted integer is " << num << endl;
   return 0;
}

Output

The above program produces the following result:

The result of converted integer is 1

Converting Character to Integer Using atoi() Function

If string contains numeric value only. You can use atoi() function to convert it into an integer. The atoi() function takes a string as input and converts it into an integer.

Example

The program uses the atoi() function to convert the character into an integer:

C C++
#include <stdio.h>
#include <stdlib.h>

int main() {
   char x[] = "-3";
   int num = atoi(x);
   
   // display the result
   printf("The result of converted integer is %d\n", num);
   
   return 0;
}

Output

The above program produces the following result:

The result of converted integer is -3
#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
   char y[] = "809";
   int num = atoi(y);
   
   // display the result
   cout << "The result of converted integer is " << num << endl;
   
   return 0;
}

Output

The above program produces the following result:

The result of converted integer is 809

Converting Character to Integer Using Typecasting

Typecasting is the process of converting a value from one data type to another. You can also use the typecasting to convert a character to an integer. To convert a digit character into its corresponding integer value, you can simply subtract '0' from it.

Example

In this example, we use the typecasting process to solve the conversion of a character into an integer:

C C++
#include <stdio.h>

int main() {
   char ch = '5';
   int num = ch - '0';
   
   // Display the result
   printf("Result: %d\n", num);
   return 0;
}

Output

The above program produces the following result:

Result: 5
#include <iostream>
using namespace std;
int main() {
   char ch = '6';
   int num = ch - '0';
   cout << "Result: " << num << endl;
   return 0;
}

Output

The above program produces the following result:

Result: 6
Updated on: 2025-06-04T14:12:57+05:30

24K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements