C++ Programming/Programming Languages/C++/Code/Keywords/static cast

The static_cast keyword can be used for any normal conversion between types. Conversions that rely on static (compile-time) type information. This includes any casts between numeric types, casts of pointers and references up the hierarchy, conversions with unary constructor, and conversions with conversion operator. For conversions between numeric types no runtime checks are performed if the current content fits the new type. Conversion with unary constructor will be performed even if it is declared as explicit.


 * Syntax

It can also cast pointers or references down and across the hierarchy as long as such conversion is available and unambiguous. For example, it can cast  to the appropriate pointer type or vice-versa. No runtime checks are performed.

Performing arithmetical operations with varying types of data type without an explicit cast means that the compiler has to perform an implicit cast to ensure that the values it uses in the calculation are of the same type. Usually, this means that the compiler will convert all of the values to the type of the value with the highest precision.
 * Common usage of type casting

The following is an integer division and so a value of 2 is returned.

To get the intended behavior, you would either need to cast one or both of the constants to a float.

Or, you would have to define one or both of the constants as a float.