Learn C++ - C++ Type Conversions






C++ converts values when you assign a value of one arithmetic type to a variable of another arithmetic type.

A zero value assigned to a bool variable is converted to false, and a nonzero value is converted to true.

Converting floating-point to integer results in truncating the number.

C++ converts values when you combine mixed types in expressions.

C++ converts values when you pass arguments to functions.

Example

The following code shows type changes on initialization


#include <iostream> 
using namespace std; 
int main() /*w  ww. j  a v a  2s .  co m*/
{ 
     cout.setf(ios_base::fixed, ios_base::floatfield); 
     float tree = 3;     // int converted to float 
     int guess(3.9);     // double converted to int 
     int debt = 7.2E12;  // result not defined in C++ 
     cout << "tree = " << tree << endl; 
     cout << "guess = " << guess << endl; 
     cout << "debt = " << debt << endl; 
     return 0; 
} 

The code above generates the following result.





Type Casts

C++ can force type conversions explicitly via the type cast.

To convert an int value to type long, you can use either of the following expressions:

(long) my_value    // returns a type long conversion of my_value 
long (my_value)    // returns a type long conversion of my_value 

The type cast doesn't alter the my_value variable itself.

It creates a new value of the indicated type, which you can then use in an expression, as in the following:

cout << int('Q');  // displays the integer code for 'Q' 

More generally, you can do the following:

(typeName) value     // converts value to typeName type 
typeName (value)     // converts value to typeName type 

The first form is straight C. The second form is pure C++.

C++ also introduces four type cast operators that are more restrictive in how they can be used.

static_cast<> operator can be used for converting values from one numeric type to another.

For example, to convert my_value to a type long value looks like this:

static_cast<long> (my_value)     // returns a type long conversion of my_value 

More generally, you can do the following:

static_cast<typeName> (value)        // converts value to typeName type 

The following code illustrates both the basic type cast (two forms) and static_cast<>.


#include <iostream> 
using namespace std; 
int main() /*from ww w  .  j  a  va 2 s . com*/
{ 
     int my_int, my_result, my_value; 

     // adds the values as double then converts the result to int 
     my_int = 19.99 + 11.99; 

     // these statements add values as int 
     my_result = (int) 19.99 + (int) 11.99;   // old C syntax 
     my_value = int (19.99) + int (11.99);  // new C++ syntax 
     cout << "my_int = " << my_int << ", my_result = " << my_result; 
     cout << ", my_value = " << my_value << endl; 

     char ch = 'Z'; 
     cout << "The code for " << ch << " is ";    // print as char 
     cout << int(ch) << endl;                    // print as int 
     cout << "Yes, the code is "; 
     cout << static_cast<int>(ch) << endl;       // using static_cast 
     return 0; 
} 

The code above generates the following result.