- Learn C++ - https://www.learncpp.com -

4.4a — Explicit type conversion (casting)

In the previous section 4.4 -- Implicit type conversion (coercion) [1], you learned that the compiler will sometimes implicitly convert a value from one data type to another. When you want to promote a value from one data type to a larger similar data type, using the implicit type conversion system is fine.


In the case where you are using literal values (such as 10, or 4), replacing one or both of the integer literal value with a floating point literal value (10.0 or 4.0) will cause both operands to be converted to floating point values, and the division will be done using floating point math.

But what if you are using variables? Consider this case:

Variable f will end up with the value of 2. How do we tell the compiler that we want to use floating point division instead of integer division? The answer is by using a type casting operator (more commonly called a cast) to tell the compiler to do explicit type conversion. A cast represents an explicit request by the programmer to do a type conversion.

Type casting




Rule: Avoid const casts and reinterpret casts unless you have a very good reason to use them.

C-style casts

In standard C programming, casts are done via the () operator, with the name of the type to cast to inside. For example:

In the above program, we use a float cast to tell the compiler to convert i1 to a floating point value. Because i1 is a floating point value, i2 will then be converted to a floating point value as well, and the division will be done using floating point division instead of integer division!


Because C-style casts are not checked by the compiler at compile time, C-style casts can be inherently misused, because they will let you do things that may not make sense, such as getting rid of a const or changing a data type without changing the underlying representation (leading to garbage results).

Consequently, C-style casts should generally be avoided.

Rule: Avoid C-style casts


C++ introduces a casting operator called static_cast. You’ve previously seen static_cast used to convert a char into an int so that std::cout prints it as an integer instead of a char:

Static_cast is best used to convert one fundamental type into another.


Using casts to make implicit type conversions clear


Casting an int (4 bytes) to a char (1 byte) is potentially unsafe, and the compiler will typically complain. In order to announce to the compiler that you are explicitly doing something you recognize is potentially unsafe (but want to do anyway), you can use a cast:


To tell the compiler that we explicitly mean to do this:


Casting should be avoided if at all possible, because any time a cast is used, there is potential for trouble. But there are many times when it can not be avoided. In most of these cases, the C++ static_cast should be used instead of the C-style cast.


1) What’s the difference between implicit and explicit type conversion?

Quiz Answers

1) Show Solution [2]

4.4b -- An introduction to std::string [3]
Index [4]
4.4 -- Implicit type conversion (coercion) [5]