Harkirat Kaur
PRO
2 months ago
Harkiratcountry asked

I don’t understand why we write (int) doubleValue when we're already assigning it to an int. Isn’t that repeating the same thing twice?

Udayan Shakya
Expert
last month
Udayan Shakya answered

Hi Harkirat,

Great question — it’s something many learners wonder about.

When you write:

int intValue = (int) doubleValue;

the part (int) is called explicit type casting. You're telling the compiler clearly: "Yes, I know doubleValue is a double, and I want to convert it to an integer."

Even though you're storing the result in an int, if you skip the cast:

int intValue = doubleValue;

C will do an implicit conversion — it still works, but the compiler may give you a warning, especially if there’s a chance of losing data (like dropping decimal points).

Using (int) makes your intention clear and avoids confusion. It’s also a good habit when converting between types, especially when precision matters.

If you have more questions, I am here to help.

C
This question was asked as part of the Learn C Programming course.