C++ examples for Data Type:Cast
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 va2 s . com return 0; }