Lesson 8: Type conversion & casting
In C every value has a type, and the type decides how operations behave. When both sides of a division are int, the result is integer division: 5 / 2 is 2, not 2.5! When you mix int with double, the int is automatically promoted to double (implicit conversion), but when both sides are integers — we
Integer division is like splitting 5 cookies between 2 kids without breaking a cookie: each gets 2, and the leftover cookie just vanishes. A cast is like bringing a knife — suddenly halves are possible too.
- integer division
- int divided by int drops the remainder: 5 / 2 is 2, 7 / 4 is 1.
- implicit conversion (promotion)
- When int and double mix in an expression, the int is automatically promoted to double.
- explicit cast
- (double)a — converts a's value to double for the expression, without changing a itself.
- char as a number
- char stores a numeric code: 'A' is 65, so 'A' + 1 is 66 — the letter B.
- %c vs %d
- Same value, two views: %c prints a character, %d prints its numeric code.