Explicit Type Conversion - C++ Data Type

C++ examples for Data Type:Cast

Introduction

To convert the value of an expression to a given type, you write the following:

static_cast<type_to_convert_to>(expression)

Here's another example of the use of static_cast<>():

Demo Code


#include <iostream>
#include <cmath>                       // For square root function
using std::cout;/*from   w ww.  j  a  va2  s. c o m*/

int main()
{
    double value1 {10.5};
    double value2 {15.5};
    int whole_number {static_cast<int>(value1) + static_cast<int>(value2)};

    cout << whole_number;
}

Result


Related Tutorials