Type Casting in C++

Imagine you have two variables—one stores a whole number, and the other stores a decimal value. If you try to use them together in the same calculation, C++ needs to decide how the values should be treated. This is where type casting comes into the picture.

Type Casting in C++ is the process of converting a value from one data type to another. Sometimes the compiler performs this conversion automatically, while in other situations, you have to tell the compiler exactly how you want the conversion to happen.

You’ll use type casting quite often while working with calculations, user input, functions, and mathematical expressions. Understanding when and how a value is converted will help you avoid unexpected results in your programs.

Why Do We Need Type Casting?

Different data types occupy different amounts of memory and store data in different ways. When you perform operations using multiple data types, C++ must convert them into a compatible form before completing the operation.

For example, if you divide two integers, you’ll get an integer result. But if you want a decimal answer, you need to convert one of the values into a floating-point number.

This conversion process is known as C++ Type Casting.

Types of Type Casting in C++

There are two main types of type casting in C++:

  • Implicit Type Casting
  • Explicit Type Casting

Let’s understand both with examples.

Implicit Type Casting

Implicit type casting, also known as automatic type conversion, is performed by the compiler. It usually happens when a smaller data type is converted into a larger data type to avoid data loss.

Example

#include <iostream>
using namespace std;

int main() {
    int number = 25;
    double value = number;

    cout << value;

    return 0;
}

Output

25ㅤ

Here, the integer value is automatically converted into a double. Since no information is lost, the compiler performs the conversion without any instruction from the programmer.

Explicit Type Casting

Sometimes the compiler cannot decide how you want a conversion to happen. In such cases, you need to convert the value manually. This is called explicit type casting.

Example

#include <iostream>
using namespace std;

int main() {
    double marks = 89.75;
    int totalMarks = (int)marks;

    cout << totalMarks;

    return 0;
}

Output

89ㅤ

Notice that the decimal part is removed after converting the value into an integer. This happens because the int data type can store only whole numbers.

Type Casting in C++ with Examples

Let’s look at another example that beginners often find useful.

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 3;

    float result = (float)a / b;

    cout << result;

    return 0;
}

Output

3.33333

If the type casting wasn’t used, the output would be 3 because both variables are integers. By converting one value into a floating-point number, C++ performs decimal division and returns a more accurate result.

Implicit and Explicit Type Casting in C++

Both types of type casting have their own purpose.

  • Implicit type casting happens automatically whenever the conversion is considered safe.
  • Explicit type casting is used when you want to control the conversion yourself.

As a beginner, you’ll mostly use explicit type casting while performing calculations or converting decimal values into whole numbers.

💡 Quick Tip

Type casting can sometimes lead to data loss. For example, converting 15.99 into an integer gives 15, not 16. Always check whether losing the decimal part is acceptable before performing the conversion.

Key Points

  • Type casting means converting one data type into another.
  • C++ supports both implicit and explicit type casting.
  • Implicit conversion is done automatically by the compiler.
  • Explicit conversion is performed by the programmer.
  • Use type casting carefully to avoid losing important data.

Frequently Asked Questions (FAQs)

Q1. What is type casting in C++?

Type casting in C++ is the process of converting a value from one data type to another. It is commonly used when performing operations involving different data types.

Q2. What are the types of type casting in C++?

C++ supports two main types of type casting: implicit type casting, which is performed automatically by the compiler, and explicit type casting, which is performed manually by the programmer.

Q3. What is the difference between implicit and explicit type casting in C++?

Implicit type casting happens automatically when the compiler safely converts one data type to another. Explicit type casting requires the programmer to manually convert the data type using casting operators or syntax.

Q4. Why is type casting used in C++?

Type casting is used to convert data between different types, improve compatibility in expressions, prevent unwanted errors, and control how calculations are performed.

Q5. Can type casting cause data loss in C++?

Yes. Converting a larger data type to a smaller one, such as from double to int, may result in a loss of precision or truncation of the original value.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top