← 🔢 Types, Operators & Expressions
Subtopic 5 of 5

1.5Type conversion, casts and integer division

The idea

When two different types meet in an expression, C quietly converts them to a common type. Integer division throws away the fraction.

Think of it like this: Pouring water between cups of different sizes: everything is poured into the bigger cup first, then you compare or add.

Why it exists

The conversion rules explain why −1 < 1u is false, why 5/2 is 2, and why a char sum can exceed 127.

How it works

  1. char and short become int first.
  2. If one operand is floating point, the other becomes floating point.
  3. If one is unsigned and the sizes match, the signed one becomes unsigned.
  4. Integer / integer truncates toward zero; the sign of % follows the dividend.

Worked example: -1 &lt; 1u

The signed −1 is converted to unsigned: it becomes 4294967295. Then 4294967295 < 1 is false (0).

Now play with it

  • →In the lab, try -1 < u: the result is 0.
  • →Compare 5 / 2 and 5 / 2.0 and read the types.

Type each expression and see its type and value. Remember the conversion rules in the table below.

type of the expressionint
value-3
Memory (one box per element, address below)
-7
i
@100
1
u
@101
2.5
d
@102
100
c
@103

Gold = the object the expression names. Blue = the element a pointer result points to. Each box stands for one element; real addresses grow by sizeof(element).

SituationRuleExample
char or short in an expressionpromoted to int firstc + c with c = 100 gives 200 (an int)
int meets longint becomes long1 + 2L is a long
signed meets unsigned (same size)signed becomes unsigned−1 < 1u is false (−1 becomes 4294967295)
integer meets floating pointinteger becomes floating5 / 2.0 = 2.5
both integersinteger division, truncates toward 05 / 2 = 2, −7 / 2 = −3, −7 % 2 = −1
assign double to intfraction droppedint n = 3.99 gives 3
assign int to charlow 8 bits keptchar c = 300 gives 44

When in doubt, convert both operands to the larger type first and then do the operation.

Check yourself

What does -7 / 2 evaluate to in C?

Remember: Mixed types: promote, then compute. Integer division truncates toward 0; signed + unsigned means unsigned.
Exam notes (the dense version)

In a mixed expression both operands are converted to the "bigger" type (the usual arithmetic conversions); char and short first become int. If one operand is unsigned and the other signed of the same size, the signed one becomes unsigned, so -1 < 1u is false. Integer division truncates toward zero (−7/2 = −3, −7%2 = −1). Assigning a double to an int drops the fraction.

Previous year questions: Types, Operators & Expressions (2)

PYQs are grouped by topic, so this is the full set for Types, Operators & Expressions; try the ones that match this subtopic. Open all 2 on the topic page.