Type Conversion in C++ - Google Docs
Type Conversion in C++ - Google Docs
T
Type conversion refers to converting a variable from one data type to another. This is
commonly used when we need to perform operations on variables of different types or
ensure compatibility between data types.
It is of two types:-
1. Automatic Type Conversion
2. Type casting
. Automatic Type Conversion (Implicit Conversion):-Automatic type conversion, also
1
known asimplicit conversionortype promotion, is a process where the compiler
automatically converts a variable or expression from one data type to another. This happens
when:
1. Data types are compatible, and
2. The destination type is larger or more precise than the source type.
The main goal of automatic type conversion is to avoid data loss and ensure compatibility
during operations involving mixed data types.
Rules of Automatic Type Conversion
1. Promotion to Larger Data Types: Smaller data types (e.g., int
,char ) are
float
automatically promoted to larger data types (e.g., double
, ) to avoid precision
loss.
2. Order of Promotion: C++ follows a hierarchy of data types for promotion:
ool → char → short → int → unsigned int → long → float → double → long double
b
3. Happens Automatically: The programmer does not need to write explicit code for the
conversion.
Examples of Automatic Type Conversion
1. Integer to Float Conversion
include <iostream>
#
using namespace std;
int main() {
int a = 10;
float b = a; // 'a' is automatically converted to float
cout << "Value of b: " << b << endl; // Output: 10.0
return 0;
}
Advantages of Automatic Type Conversion
1. Convenience: Reduces the need for explicit type casting by the programmer.
2. Avoids Errors: Ensures compatibility in expressions by promoting smaller types to
larger types.
3. Improves Readability: Simplifies code when dealing with mixed-type operations.
Limitations of Automatic Type Conversion
1. Data Loss in Some Cases:-When converting a large integer to a floating-point type
with insufficient precision.
int largeValue = 123456789;
float converted = largeValue; // Precision may be lost
2. Unintended Behavior:-In mixed operations, unintended results may occur if the
programmer does not account for promotions.
unsigned int a = 10;
int b = -20;
cout << a + b; // May result in unexpected behavior due to unsigned conversion
3. Type Promotion Order:-Can lead to unexpected promotions in complex expressions.
2. Type Casting:-Type casting is the process of converting a variable or expression from
one data type to another. It can be done by the user as per the requirements.
In C++, this can be done in two ways:
1. Implicit Type Casting(also called type promotion, handled automatically by the
compiler).
2. Explicit Type Casting(manually specified by the programmer).
Type Casting in C++
Type casting is the process of converting a variable or expression from one data type to
another. In C++, this can be done in two ways:
1. Implicit Type Casting(also called type promotion, handled automatically by the
compiler).
2. Explicit Type Casting(manually specified by the programmer).
1. Implicit Type CastingPerformed automatically by the compiler.Converts smaller data
types to larger or more compatible types.Examples include converting an intto a
floator
charto an
a int .
Example of Implicit Type Casting
include <iostream>
#
using namespace std;
int main() {
int a = 10;
float b = a; // Implicit conversion from int to float
cout << "Value of b: " << b << endl; // Output: 10.0
return 0;
}
2. Explicit Type Casting
● Requires the programmer to specify the type to which a variable or expression should
be converted.
● Useful when you want to enforce a specific conversion, even if data loss might occur.
Syntax for Explicit Casting
(datatype) expression
Examples of Explicit Type Casting
Integer to Float:
include <iostream>
#
using namespace std;
int main() {
int a = 5;
float b = (float)a; // Explicit casting from int to float
cout << "Value of b: " << b << endl; // Output: 5.0
return 0;
}
Advantages of Explicit Type Casting
1. Precision Control: Helps manage the exact representation of data types.
2. Avoids Ambiguity: Makes the programmer's intent clear.
3. Overrules Compiler Behavior: Allows conversions that the compiler would otherwise
restrict.
Disadvantages:-
1. Undefined Behavior: Using reinterpret_castcarelessly can lead to
unpredictable results.
2. Complex Code: Overuse of explicit casting can reduce code readability.
Type Casting in C++: Specialized Casts
C++ provides four specific type casting operators to handle different type conversion
scenarios. These are more explicit and safer than traditional C-style casting. Each cast
serves a distinct purpose and is used in appropriate contexts.
1. static_cast
● Used forwell-defined conversionsbetween types.
● Performs compile-time checks but cannot cast between unrelated types.
● Commonly used for:
○ Converting between numeric types.
○ Upcasting or downcasting in class hierarchies (without polymorphism).
○ Explicit conversions between compatible types.
Syntax:-static_cast<new_type>(expression)
Example:
include <iostream>
#
using namespace std;
int main() {
float x = 3.14;
int y = static_cast<int>(x); // Converts float to int
cout << "Value of y: " << y << endl; // Output: 3
return 0;
}