Lesson 7 +25 XP

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):

texttt{int} → texttt{unsigned int} → texttt{long} → texttt{unsigned long} → texttt{float} → texttt{double} → texttt{long double}

Water-fall model of implicit type conversionWater-fall model of implicit type conversion

ExpressionOperand typesConverted toResult type
'A' + 1char, intchar promoted to intint (66)
5 / 2int, intno conversionint (2)
5 / 2.0int, double5 becomes 5.0double (2.5)
2.5f * 2float, int2 becomes 2.0ffloat (5)
10L + 3.0flong, float10L becomes 10.0ffloat

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:

AssignmentWhat 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
C++
#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:

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 cast
⚠️ Warning

double 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).

LevelOperatorsAssociativity
1 (highest)::left to right
2() call, [], ., ->, postfix ++ - , typeid, casts like static_castleft to right
3prefix ++ - , unary + -, !, ~, * (indirection), & (address), sizeof, new, delete, C-style castright 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
12bitwise OR (one vertical bar)left to right
13&&left to right
14logical OR (two vertical bars)left to right
15?:right to left
16= += -= *= /= and the other assignmentsright to left
17 (lowest),left to right

A short way to remember it: unary > multiplicative > additive > shift > relational > equality > bitwise > logical > conditional > assignment > comma.

C++
#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:

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 1

Evaluating step by step

SolvedExample 1 · Evaluate x = 8 + 12 / 4 % 3 * 2 - 1;
  1. /, % and * share a level and associate left to right: 12 / 4 = 3.
  2. 3 % 3 = 0.
  3. 0 * 2 = 0.
  4. Then + and -, left to right: 8 + 0 = 8, then 8 - 1 = 7.
  5. = is applied last.
Answerx = 7
SolvedExample 2 · Evaluate y = 2 + 3.5 * 2 / 4; where y is an int
  1. 3.5 * 2: the int 2 becomes double, giving 7.0.
  2. 7.0 / 4: 4 becomes 4.0, giving 1.75.
  3. 2 + 1.75: 2 becomes 2.0, giving 3.75 (a double).
  4. Assigning to int truncates the fraction.
Answery = 3
💡 Tip

Key points to remember

  • Implicit conversion: char/short/bool promote to int, then the narrower operand widens towards long double.
  • Assigning to a narrower type truncates or wraps: int i = 7.9 gives 7.
  • int / int is integer division even if the result is stored in a double: 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.
🎯 END-TERM EXAM PREP

📝 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)