ACompressed recap
🧾
Every value has a type with a size (here char 1, short 2, int 4, long 4, long long 8, float 4, double 8 bytes) and a range (n bits: unsigned 0 … 2ⁿ − 1, signed −2ⁿ⁻¹ … 2ⁿ⁻¹ − 1). Constants have types too; a leading 0 means octal.
Operators: integer / truncates towards 0 and % follows the left sign; comparisons and logic give 1/0; && and || short-circuit; bitwise operators work on bits; x++ gives the old value, ++x the new. Precedence and associativity fix the grouping; changing a variable twice in one expression is undefined behavior.
Conversions: small integers promote to int; mixed types move towards the wider type, and signed meets unsigned as unsigned; assignment truncates fractions; casts convert on purpose; unsigned values wrap; printf returns the number of characters it printed.
BMerged rule card
1.1.1 Data types and their sizes; signed vs unsigned; constants; variables; declaration vs definition
- Sizes here: char 1, short 2, int 4, long 4, long long 8, float 4, double 8; only char = 1 is guaranteed.
- n bits: unsigned 0 … 2ⁿ − 1; signed −2ⁿ⁻¹ … 2ⁿ⁻¹ − 1.
sizeof= size in bytes, an operator; print with %u (or %zu).- Literals: 42, 42u, 42L, 3.75 (double), 3.75f, 'A' = 65, 0x.. hex, 0.. octal.
constvariables cannot be assigned again.- Declaration = name + type; definition = declaration + memory. Defined once, declared many times.
1.1.2 Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
- int / int truncates towards 0; a % b has the sign of a; (a/b)*b + a%b == a.
- Relational and logical results are int 1 or 0; non-zero = true.
- && and || short-circuit: right side may never run (side effects skipped).
- Bitwise: & | ^ ~ << >>; ~x = −x − 1; x<<k = x·2ᵏ; x>>k = ⌊x/2ᵏ⌋ for x ≥ 0.
- x++ old value then add; ++x add then new value.
- Precedence: unary > * / % > + − > shifts > relational > == != > & > ^ > | > && > || > ?: > = > ,
- Unary, ?: and assignment associate right to left; others left to right.
- Changing a variable twice (or change + read) in one expression without an ordering point = undefined behavior.
1.1.3 Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
- 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 ==.
CInterleaved GATE-style questions
Q1MCQ1 markOutput
What does this print?
int a, b, c;
a = b = c = 7;
a += 3;
b *= a - 5;
c %= 4;
printf("%d %d %d\n", a, b, c);
- (A)
10 35 3 - (B)
10 35 7 - (C)
10 21 3 - (D)
3 35 3
Q2NAT1 markRanges
What is the smallest value of a signed 8-bit char?
Numerical answer: ________
Q3MCQ2 marksOutput
What does this print?
int a = 4, b = 9;
int big = a > b ? a : b;
int k = (a = 1, a + 10);
printf("%d %d %d\n", big, k, a);
- (A)
9 14 4 - (B)
4 11 1 - (C)
9 2 1 - (D)
9 11 1
Q4MCQ1 markConversion
What is the value of (double)(5 / 2)?
- (A) 2.5
- (B) 3.0
- (C) 2
- (D) 2.0
Q5NAT2 marksBitwise
What is (5 << 2) ^ 3?
Numerical answer: ________
Q6MSQ2 marksUndefined behavior
With int i = 2;, which statements have undefined behavior?
- (A)
i = i++ + 1; - (B)
int j = i++ + i++; - (C)
i = (i++, i + 1); - (D)
i += 2;
Q7MCQ2 marksOutput
What does this print?
char c = 200;
printf("%d\n", c);
- (A)
-200 - (B)
-56 - (C)
56 - (D)
200
Q8NAT1 markCharacters
What is the value of '9' - '0' + 'C' - 'A'?
Numerical answer: ________
Q9MCQ1 markShort-circuit
After int a = 1, b = 1; int r = a || ++b;, what are r and b?
- (A) r = 1, b = 1
- (B) r = 1, b = 2
- (C) r = 0, b = 1
- (D) r = 2, b = 2
Q10MCQ2 marksprintf
What does this print?
int n = printf("%d", 100);
printf(" %d\n", n);
- (A)
100 100 - (B)
3 100 - (C)
100 4 - (D)
100 3
DAnswer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (A)1 mark
All three become 7. a = 10. b = 7 × (10 − 5) = 35. c = 7 % 4 = 3.
Output
10 35 3
- (B) is wrong: c %= 4 gives 3.
- (C) is wrong: b *= a − 5 uses the new a = 10.
- (D) is wrong: a += 3 gives 10.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
Q2 Answer: -1281 mark
−2⁷ = −128.
Re-read: Lesson 1.1.1 — Data types and their sizes; signed vs unsigned; constants; variables; declaration vs definition
Q3 Answer: (D)2 marks
a > b is false, so big = b = 9. The comma operator sets a = 1, then gives a + 10 = 11.
Output
9 11 1
- (A) is wrong: a was changed to 1 before a + 10.
- (B) is wrong: The condition is false, so b is chosen.
- (C) is wrong: The comma operator yields its right side, a + 10.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
Q4 Answer: (D)1 mark
5/2 = 2 first (ints), then converted: 2.0.
- (A) is wrong: The division happened before the cast.
- (B) is wrong: Integer division truncates.
- (C) is wrong: The result is a double, 2.0.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q5 Answer: 232 marks
5 << 2 = 20 = 10100; XOR 00011 = 10111 = 23.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
Q6 Answer: (A) (B)2 marks
A changes i twice (++ and =); B changes i twice. C is defined: the comma operator orders i++ before i + 1, then the assignment stores 4.
- (C) is wrong: The comma operator is an ordering point.
- (D) is wrong: Ordinary compound assignment.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
Q7 Answer: (B)2 marks
Signed 8-bit: 200 − 256 = −56 (the usual GATE assumption).
Output
-56
- (A) is wrong: The value wraps by 256.
- (C) is wrong: The top bit makes it negative.
- (D) is wrong: 200 does not fit in a signed char.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q8 Answer: 111 mark
9 + 2 = 11.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns
Q9 Answer: (A)1 mark
a is true, so ++b is skipped. r = 1.
Output
1 1
- (B) is wrong: ++b never runs.
- (C) is wrong: a is true, so r is 1.
- (D) is wrong: Logical results are 0 or 1.
Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior
Q10 Answer: (D)2 marks
The first call prints 100 (3 characters) and returns 3.
Output
100 3
- (A) is wrong: printf returns the count, not the value printed.
- (B) is wrong: The first call prints first.
- (C) is wrong: There is no new line in the first call.
Re-read: Lesson 1.1.3 — Type conversion: implicit promotion, casting, overflow, char as a small integer; what printf returns