Back to Curriculum

7. Type Conversion

Implicit vs Explicit Type Conversion

Type conversion (also called type casting) allows you to convert a value from one data type to another.

Implicit conversion happens automatically when the compiler can safely convert one type to another:

int number = 5;
double decimal = number; // Implicit conversion: int → double
// decimal now contains 5.0

Explicit conversion (casting) is when you manually tell the compiler to convert a type:

double pi = 3.14159;
int rounded = (int)pi; // Explicit conversion: double → int
// rounded now contains 3 (truncated)

Note: Be careful with explicit conversions as they can result in data loss!

Your Task

Create a double variable named temperature with value 98.6, then convert it to an int and store the result in a variable called tempInt.

🎉

Lesson Complete!

Great job! You now understand how to convert between different data types in C++.

Continue to Lesson 8: User Input
main.cpp
Output
Click "Check Answer" to run your code.