Loading lessons...
Implicit Type Conversions & Operator Precedence
Implicit Type Conversions & Operator Precedence
When an expression mixes types, such as int + double, C++ converts one operand automatically before computing. And when an expression has several operators, such as a + b * c, precedence and associativity decide the order. Both are classic sources of bugs and of short exam questions.
1. Implicit (Automatic) Type Conversion
In a mixed expression the compiler converts the operand of the "smaller" type to the "wider" type, so no information is lost. This is called implicit conversion, automatic conversion or type promotion. The rules apply to each operator separately.
Step 1: integral promotion. Before anything else, char, short, bool and enum values in an expression are converted to int (or to unsigned int if int cannot hold them).
Step 2: the usual arithmetic conversions. If the two operands still differ, the "lower" one is converted to the type of the "higher" one, following this order (the water-fall model):
Water-fall model of implicit type conversion
| Expression | Operand types | Converted to | Result type |
|---|---|---|---|
'A' + 1 | char, int | char promoted to int | int (66) |
5 / 2 | int, int | no conversion | int (2) |
5 / 2.0 | int, double | 5 becomes 5.0 | double (2.5) |
2.5f * 2 | float, int | 2 becomes 2.0f | float (5) |
10L + 3.0f | long, float | 10L becomes 10.0f | float |
Conversion in assignment
In an assignment the value on the right is converted to the type of the variable on the left. Converting to a narrower type can lose information:
| Assignment | What happens |
|---|---|
double d = 7; | 7 becomes 7.0: safe (widening) |
int i = 7.9; | the fraction is truncated: i = 7 |
char c = 300; | only the low 8 bits are kept: value changes |
unsigned u = -1; | wraps round to the largest unsigned value |
#include <iostream>
using namespace std;
int main() {
char ch = 'A';
int i = ch + 1; // 'A' (65) promoted to int
cout << "ch + 1 = " << i << endl;
cout << "5 / 2 = " << 5 / 2 << endl; // integer division
cout << "5 / 2.0 = " << 5 / 2.0 << endl; // 5 converted to double
int marks = 7.9; // fraction lost in assignment
cout << "int marks = 7.9 gives " << marks << endl;
int total = 17, count = 4;
double avg1 = total / count; // division done in int first!
double avg2 = (double)total / count; // explicit cast first
cout << "avg1 = " << avg1 << ", avg2 = " << avg2 << endl;
unsigned int u = 1;
int n = -1;
cout << "(n < u) is " << (n < (int)u) << " with a cast" << endl;
return 0;
}Output:
ch + 1 = 66
5 / 2 = 2
5 / 2.0 = 2.5
int marks = 7.9 gives 7
avg1 = 4, avg2 = 4.25
(n < u) is 1 with a castdouble avg = total / count; still gives 4: the division happens in int before the result is converted to double. Cast one operand first: (double)total / count. Comparing a negative int with an unsigned is another trap: without a cast, -1 would be converted to a huge unsigned number and -1 < 1u would be false.
Implicit conversion is the opposite of explicit conversion (casting), where the programmer asks for the conversion: (double)total or double(total) (C++ functional notation). Explicit conversions, including conversions to and from class types and the static_cast family, are covered in Lesson 21.
2. Operator Precedence and Associativity
Precedence decides which operator is applied first when different operators appear together: in a + b * c the * binds tighter. Associativity decides the order when operators of the same precedence appear together: a - b - c means (a - b) - c (left to right), but a = b = c means a = (b = c) (right to left).
| Level | Operators | Associativity |
|---|---|---|
| 1 (highest) | :: | left to right |
| 2 | () call, [], ., ->, postfix ++ - , typeid, casts like static_cast | left to right |
| 3 | prefix ++ - , unary + -, !, ~, * (indirection), & (address), sizeof, new, delete, C-style cast | right to left |
| 4 | .* ->* | left to right |
| 5 | * / % | left to right |
| 6 | + - | left to right |
| 7 | << >> | left to right |
| 8 | < <= > >= | left to right |
| 9 | == != | left to right |
| 10 | & (bitwise AND) | left to right |
| 11 | ^ | left to right |
| 12 | bitwise OR (one vertical bar) | left to right |
| 13 | && | left to right |
| 14 | logical OR (two vertical bars) | left to right |
| 15 | ?: | right to left |
| 16 | = += -= *= /= and the other assignments | right to left |
| 17 (lowest) | , | left to right |
A short way to remember it: unary > multiplicative > additive > shift > relational > equality > bitwise > logical > conditional > assignment > comma.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 4, c = 3;
cout << "a + b * c = " << a + b * c << endl; // * first: 10 + 12
cout << "(a + b) * c = " << (a + b) * c << endl; // brackets first
cout << "a - b - c = " << a - b - c << endl; // left to right: (10-4)-3
cout << "a / b * c = " << a / b * c << endl; // (10/4)*3 = 2*3
int x, y, z;
x = y = z = 7; // right to left
cout << "x y z = " << x << " " << y << " " << z << endl;
bool r = (a > b && b > c) || c > a; // > first, then &&, then ||
cout << "(a > b && b > c) || c > a = " << r << endl;
int m = 5;
int n = -m++ + 2; // unary minus of m++ (5), then + 2
cout << "n = " << n << ", m = " << m << endl;
cout << "(a & 1) == 0 is " << ((a & 1) == 0) << endl; // brackets needed: == beats &
return 0;
}Output:
a + b * c = 22
(a + b) * c = 42
a - b - c = 3
a / b * c = 6
x y z = 7 7 7
(a > b && b > c) || c > a = 1
n = -3, m = 6
(a & 1) == 0 is 1Evaluating step by step
x = 8 + 12 / 4 % 3 * 2 - 1;/,%and*share a level and associate left to right:12 / 4= 3.3 % 3= 0.0 * 2= 0.- Then
+and-, left to right:8 + 0= 8, then8 - 1= 7. =is applied last.
x = 7y = 2 + 3.5 * 2 / 4; where y is an int3.5 * 2: theint2 becomesdouble, giving 7.0.7.0 / 4: 4 becomes 4.0, giving 1.75.2 + 1.75: 2 becomes 2.0, giving 3.75 (adouble).- Assigning to
inttruncates the fraction.
y = 3Key points to remember
- Implicit conversion:
char/short/boolpromote toint, then the narrower operand widens towardslong double. - Assigning to a narrower type truncates or wraps:
int i = 7.9gives 7. int / intis integer division even if the result is stored in adouble: cast first.- Unary, assignment and
?:associate right to left; almost everything else left to right. - When in doubt, use brackets: they make the order explicit.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is implicit type conversion? Explain with the water-fall model. (5 Marks)
- Write: integral promotion, the conversion order, the diagram, the expression table and the program.
- Q2. Explain operator precedence and associativity in C++. Evaluate an expression step by step. (5 Marks)
- Write: the definitions, the precedence table (at least the main 10 levels), and one solved example.
- Q3. Why does
double avg = 17 / 4;give 4? How can it be fixed? (2.5 Marks) - Q4. Differentiate between implicit and explicit type conversion. (2.5 Marks)