C++ static_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.

#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 v  a  2s.co  m
    return 0;   
}



PreviousNext

Related