Get to know and use static_cast - C++ Data Type

C++ examples for Data Type:Cast

Introduction

We can use static_cast to convert large type to smaller type.

char smallNumber = static_cast<char>(largeNumber);

static_cast is evaluated by the compiler at compile time and will cause a compilation error for converting between incompatible types.

Demo Code

#include <iostream> 
#include <string> 
  
using namespace std; 
  
int main(int argc, char *argv [])
{ 
    long largeNumber = 9999999999;
    char smallNumber = static_cast<char>(largeNumber); 
 
    cout << smallNumber;//from  w  w  w.j  a va2  s  . com
    return 0;   
}

Result


Related Tutorials