Example of static_cast

Below is the C++ program to implement static_cast:

C++




// C++ Program to demonstrate
// static_cast
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    float f = 3.5;
 
    // Implicit type case
    // float to int
    int a = f;
    cout << "The Value of a: " << a;
 
    // using static_cast for float to int
    int b = static_cast<int>(f);
    cout << "\nThe Value of b: " << b;
}


Output

The Value of a: 3
The Value of b: 3

static_cast in C++

A Cast operator is a unary operator which forces one data type to be converted into another data type.

C++ supports 4 types of casting:

This article focuses on discussing the static_cast in detail.

Similar Reads

Static Cast

This is the simplest type of cast that can be used. It is a compile-time cast. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions....

Syntax of static_cast

static_cast (source);...

Example of static_cast

Below is the C++ program to implement static_cast:...

The behavior of static_cast for Different Scenarios

...