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:
- Static Cast
- Dynamic Cast
- Const Cast
- Reinterpret Cast
This article focuses on discussing the static_cast in detail.