Type conversion is needed when you assign a value of one data type to a variable of another data type.
This type of casting is done automatically by the compiler. It is safe and do not cause data loss when conversion is from a smaller type to a larger type. For example:
short to an int is safe because an int can represent all possible short values. int to a double is safe because a double can represent all possible int values without loss of precision.float to a double is safe because a double has a larger range and higher precision than a float. #includeusing namespace std; int main () { int c = 7; float a = 3.142f; double res = c * a; cout << "Answer is: " << res; return 0; }
int (c) by a float (a), implicit type conversion happens. Here, the integer c is implicitly promoted to a float. This is called type promotion. The multiplication is performed between these two floating-point precision variables and saved in a double-precision floating-point variable.Answer is: 21.994
This type of casting is done manually by the programmer. It is also only safe and does not cause data loss when conversion is from a smaller type to a larger type, as in implicit Type Casting. Then the question arises, why we need explicit type casting? The reasons are given below:
If you want to manually convert a type, you can do it easily in 2 ways.
variable = (int)expression;
expression will be converted into an int and assigned to the variable.4.19), the decimal part will be removed, and only 4 will be stored.#includeusing namespace std; int main () { double d = 3415.3124; int i = (int)d; cout <<"d= " <
d= 3415.31
i= 3415
In this program, the value of the double variable d is stored in an int-type variable. The C-style cast (int)d truncates the decimal part of d, storing only the integer portion in i.
But C-style type casting is not safe because it does not provide compile-time checks. It is better to use cast operators provided by C++ to fulfill the purpose of typecasting. They are also known as named casts. C++ provides four cast operators.
variable = static_cast (expression)
dynamic_cast (expression)
const_cast(expression)
reinterpret_cast(expression)
#includeusing namespace std; int main () { double d = 3415.3124; int i = static_cast (d); cout <<"d= " <
d= 3415.31
i= 3415
Explanation for dynamic cast, const_cast are not given here. Because they are of advanced level then this topic. To see their details. Please click here.
Type conversion (whether implicit or explicit) can lead to data loss, truncation, or even undefined behavior when converting from a larger type to a smaller one. For example, converting a double (e.g., 3.99) to an int will truncate the decimal part, resulting in 3. The value after conversion can also exceed the smaller type’s limit, causing overflow. For example, Converting long long to int. Another example is converting a very large floating-point number to an integer. It can cause overflow because the number could be too large for the integer type to store.
No Comments