Integer and Floating-point Division
You have yet to see the rest of the story about the division operator(/). The behavior of this operator depends on the type of the operands.★ If both operands are integers, C++ performs integer division. That means any fractional part of the answer is discarded, making the result an integer.
★ If one or both operands are floating-point values, the fractional part is kept, making the result floating-point.
integerDivision.cpp
Output:
Integer division: 9/5 = 1
Floating-point division: 9.0/5.0 = 1.8
Mixed division: 9.0/5 = 1.8
Type Conversion in C++
C++ operations occur on same type operands. If operands are of different types, C++ will convert one.★ Implicit Conversion
Implicit conversion do not require any operator. They are automatically performed.implicitConversion.cpp
→   No data loss   → | ||
---|---|---|
Lower Data Type | char → short → int → long → float → double → long double | Higher Data Type |
←   Data loss   ← |
★ Explicit Type Casting
typeCast.cpp