0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- What does
-7 / 2give in C? from Lesson 1.1.2 - What range does an 8-bit unsigned char hold? from Lesson 1.1.1
- How many bits are needed to label 300 different values? from Lesson 0.2.1
1 · The Hook
🎣
A teacher averages two marks, 7 and 8, and the program prints 7 instead of 7.5. A car odometer with five digits reads 99999 and, one kilometre later, 00000. Both happen in C: a value is quietly converted to another type, or it no longer fits its box and wraps around.
2 · Predict First
🤔
What does this print?
printf("%d\n", -1 < 1u);
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
💡
Predict First — the answer
Output
0
Surprise: −1 < 1 is reported false. 1u is unsigned, so C converts −1 to unsigned before comparing. As an unsigned 32-bit number, −1 becomes 4,294,967,295, which is not less than 1.
Integer promotion
In any arithmetic, values of type char and short are first converted to int. So two chars holding 100 add up to the int 200, even though a signed char cannot hold 200.
char c = 100, d = 100;
int s = c + d;
printf("%d\n", s);
Output
200
The usual arithmetic conversions
When an operator gets two different types, the “smaller” one is converted to the “larger” before the operation:
char, short --> int --> unsigned int --> long --> unsigned long --> long long --> float --> double
(promoted first) (int meets unsigned int of the same size: both become unsigned)
So 7 / 2.0 is 3.5 (7 becomes a double), and -1 < 1u compares two unsigned values.
Conversion on assignment
Storing a value into a variable converts it to the variable’s type: a double stored in an int drops the fraction (towards zero): int k = 8.9; gives 8. A larger integer stored in a char keeps only the low 8 bits.
Casting
A cast (type)expression converts on purpose. Watch where it applies:
int a = 7, b = 2;
printf("%d %f %f\n", a / b, (double)a / b, (double)(a / b));
printf("%d %d\n", (int)3.99, (int)-3.99);
Output
3 3.500000 3.000000
3 -3
(double)a / b converts a first, so the division is done with fractions. (double)(a / b) divides integers first (3) and then converts.
Overflow
Unsigned arithmetic never overflows in C: it wraps around modulo 2^n^ like an odometer (0 − 1 gives the largest value; 255 + 1 in an unsigned char gives 0). Signed overflow (going past the int range, e.g. 2,147,483,647 + 1) is undefined behavior.
unsigned int u = 0;
u = u - 1;
unsigned char uc = 255;
uc = uc + 1;
printf("%u %d\n", u, uc);
Output
4294967295 0
char is a small integer
A char stores a character code, so you can do arithmetic on it. 'A' + 2 is 67, which prints as C with %c. '7' − '0' is the digit value 7. Lowercase letters are 32 above uppercase: 'a' − 32 == 'A'.
char c = 'A' + 2;
printf("%d %c %c %d\n", c, c, 66, '7' - '0');
Output
67 C B 7
Storing 200 in a plain (signed) char cannot fit; on our machine the bits read back as 200 − 256 = −56. C calls this implementation-defined: GATE questions usually assume a signed 8-bit char and expect −56.
printf returns a count
printf hands back the number of characters it printed (the new line counts). Nested calls print the inner text first:
int n = printf("GATE");
printf(" %d\n", n);
printf("%d\n", printf("ab\n"));
Output
GATE 4
ab
3
Two more traps
- Fractions such as 0.1 cannot be stored exactly.
float f = 0.1;holds a slightly different value from the double 0.1, sof == 0.1is 0, and even0.1 + 0.2 == 0.3is 0. sizeofdoes not evaluate its operand: aftersizeof(i++), i is unchanged.
int i = 5;
int s = sizeof(i++);
printf("%d %d\n", i, s);
Output
5 4
4 · Visual
Odometer view of 8-bit unsigned char (wrap-around):
253 254 255 | 0 1 2
^ ^
|-- +1 --> 254 -- +1 --> 255 -- +1 --> 0 (keeps only the low 8 bits)
Storing 200 into a signed char (8 bits):
200 = 11001000 -> read with top bit weighing -128: -128 + 64 + 8 = -56
Comparing -1 with 1u:
-1 (int) --convert to unsigned--> 4294967295 ; 4294967295 < 1 ? -> 0
Conversions keep the bits and change how they are read; unsigned values wrap like an odometer.
5 · Worked Examples
Example 1 Easy
What does this print?
int a = 7, b = 2;
printf("%d %f %f\n", a / b, (double)a / b, (double)(a / b));
printf("%d %d\n", (int)3.99, (int)-3.99);
Solution
a/b with ints is 3. (double)a makes the division real: 3.5. (double)(a/b) converts the already-truncated 3. Casting a double to int drops the fraction towards zero: 3 and −3.
Output
3 3.500000 3.000000
3 -3
Example 2 Medium
What does this print?
int i = -3;
unsigned int u = 2;
printf("%d %u\n", i + u > 0, i + u);
Solution
i + u mixes int and unsigned int, so −3 is converted to unsigned: 4,294,967,293 + 2 = 4,294,967,295. That is certainly > 0, so the comparison gives 1.
Output
1 4294967295
Example 3 GATE
What does this print?
printf("%d\n", printf("%d", 1234));
Solution
The inner printf runs first and prints 1234; it returns 4. The outer printf then prints 4 and a new line. Nothing separates them on screen.
Output
12344
6 · GATE Traps
⚠️
(double)(a/b)is not(double)a/b: the first divides integers before converting.- Mixing signed and unsigned converts the signed value to unsigned:
-1 > 0uis true. - Signed overflow is undefined; unsigned overflow wraps (mod 2ⁿ).
- Storing out-of-range values in a signed char gives implementation-defined results; GATE assumes 8-bit two’s complement (200 → −56).
- Never compare fractions with ==:
0.1f == 0.1is false. printfreturns the number of characters printed, counting \n.sizeof(i++)does not change i.
8 · Memory Hook
🧠
“Small things grow up before they fight.” chars and shorts become ints, ints become unsigned or double to match the bigger partner. Unsigned numbers live on an odometer; signed ones fall off a cliff (undefined).
9 · Connect the Dots
Lessons 1.1.1–1.1.3 covered values, operators and conversions: a program can now compute anything, but always in a straight line from top to bottom. Next lesson answers: how can a program choose between different statements depending on a value?
10 · Self-Test
GATE format. Try every question before opening the vault.
Q1MCQ1 markOutput
What does this print?
printf("%d %d\n", 9 / 2, (int)(9 / 2.0 + 0.5));
- (A)
4 5 - (B)
5 5 - (C)
4.5 5 - (D)
4 4
Q2MCQ2 marksConversion
What does this print?
double d = 9 / 2 + 9 / 2.0;
int k = d;
printf("%f %d\n", d, k);
- (A)
8.500000 9 - (B)
8.000000 8 - (C)
8.500000 8 - (D)
9.000000 9
Q3NAT1 markWrap-around
An unsigned char holds 250. After adding 10 to it (and storing back), what value does it hold?
Numerical answer: ________
Q4MCQ1 markprintf
What does this print?
printf("%d\n", printf("Hi\n"));
- (A)
Hi3 - (B)
3thenHi - (C)
Hithen3 - (D)
Hithen2
Q5MSQ2 marksConversions
Which of these expressions have the value 1?
- (A)
(int)1.999 - (B)
-1 > 0u - (C)
'b' - 'a' - (D)
0.1 + 0.2 == 0.3
11 · Rule Card
- char/short → int in arithmetic (integer promotion).
- Mixed types: convert towards double; same-size signed + unsigned → unsigned.
- double → int drops the fraction (towards 0); big int → char keeps low 8 bits.
- Cast (type)e converts e only: (double)a/b ≠ (double)(a/b).
- Unsigned wraps mod 2ⁿ; signed overflow is undefined.
- char arithmetic: 'A' = 65, 'a' = 97, '0' = 48; 'd' − '0' = digit value.
- printf returns characters printed; sizeof does not evaluate its operand; never compare fractions with ==.
12 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
integer promotionusual arithmetic conversionsassignment conversioncast (type)exprunsigned wrap-aroundsigned overflow (undefined)char arithmeticprintf return valuefloating-point inexactness
13 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Recall Warm-up
- −3 (integer division truncates towards zero).
- 0 to 255.
-
Self-Test
Q1 Answer: (A)1 mark
9/2 = 4 (integer division). 9/2.0 = 4.5; + 0.5 = 5.0; cast gives 5.
Output
4 5
- (B) is wrong: 9/2 truncates to 4.
- (C) is wrong: %d prints whole numbers.
- (D) is wrong: The second value is rounded up by + 0.5.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q2 Answer: (C)2 marks
9/2 = 4 (ints); 9/2.0 = 4.5; sum 8.5. Storing 8.5 in an int drops the fraction: 8.
Output
8.500000 8
- (A) is wrong: Conversion to int truncates, not rounds.
- (B) is wrong: 9/2.0 is 4.5.
- (D) is wrong: 9/2 is 4, not 4.5.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q3 Answer: 41 mark
260 does not fit; keep it modulo 256: 260 − 256 = 4.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q4 Answer: (C)1 mark
The inner call prints "Hi\n" (3 characters) and returns 3.
Output
Hi
3
- (A) is wrong: The inner text ends with a new line.
- (B) is wrong: The inner call runs first.
- (D) is wrong: The new line counts as a character.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q5 Answer: (A) (B) (C)2 marks
−1 becomes a huge unsigned value; letters are consecutive codes; the cast truncates to 1; the fraction sum is not exact.
- (D) is wrong: Floating-point sums are not exact: this is 0.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns