How to use static_cast In C++

The integer can be converted to a character using the static_cast function. Below is the C++ program to convert int to char using static_cast:

C++




// C++ program to convert
// int to char using static_cast
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    int N = 65;
  
    char c = static_cast<char>(N);
    cout << c;
    return 0;
}


Output

A

C++ Program For int to char Conversion

In this article, we will learn how to convert int to char in C++. For this conversion, there are 5 ways as follows:

  1. Using typecasting.
  2. Using static_cast.
  3. Using sprintf().
  4. Using to_string() and c_str().
  5. Using stringstream.

Let’s start by discussing each of these methods in detail.

Examples:

Input: N = 65 
Output:

Input: N = 97 
Output: a

Similar Reads

1. Using Typecasting

Method 1:...

2. Using static_cast

...

3. Using sprintf()

...

4. Using to_string() and c_str()

The integer can be converted to a character using the static_cast function. Below is the C++ program to convert int to char using static_cast:...

5. Using stringstream

...

Contact Us