In this tutorial we will discuss how to typecast int to char and char to int in C/C++. Typecasting is an operation performed on one type of data to transform it into some other type supported by a particular programming language to serve some purpose in the program.
Table of Contents
Typecasting in C/C++
In this article we will be typecasting:
- int to char, and
- char to int
Typecasting int to char
When we typecast integer type of data into character type, the result is an ASCII representation of the corresponding digit. This means that you can’t produce exact representation of an integer into a character (i.e. 65 => ‘65’), rather you will be having its ASCII code in output (i.e. 65=> ‘A’).
#include <iostream>
using std::cout;
using std::cin;
int main(){
cout<< char ( 65 ) <<"\n";
cout<< char ( 66 ) <<"\n";
cout<< char ( 67 ) <<"\n";
}

Typecasting char to int
On typecasting a character into an integer, again the result is the ASCII correspondence of that character (i.e. ‘A’ => 65). Following is a code snippet to demonstrate this.
#include <iostream>
using std::cout;
using std::cin;
int main(){
char a = 'A';
char b = 'B';
char c = 'C';
int num = (int) a;
cout<< num <<"\n";
num = (int) b;
cout<< num <<"\n";
num = (int) c;
cout<< num <<"\n";
}

Conclusion
In this article, we went through the concept of type casting and how does it practically look like when we type cast integer (int) type of data to character(char) type and vice versa, in C/C++ programming.
As observed it’s to be noted that these types of data can’t be directly typecast, instead they are transformed into their ASCII representations first.