CS Atlas GATE 2027
PDS course
0 / 103 complete

PDS/Phase 1 · Programming in C

Lesson 1.1.2 · Item 11 of 103

Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior

Read at your own pace. Try the questions before opening the answers.

On this page

0 · Recall Warm-up

Answer from memory first. Answers are in the Answer Vault at the bottom.

1 · The Hook

🎣

Three friends split a ₹1000 bill. Each pays ₹333 and ₹1 is left over. That is exactly what C does with whole numbers: division gives the whole part, and a separate operator gives the remainder.

Programs combine values constantly: add, compare, check two conditions at once, count up by one. Each operator has a precise meaning, and when several appear together, fixed rules decide which goes first.

2 · Predict First

🤔

What does this print?

printf("%d %d %d\n", 7 / 2, -7 / 2, 7 % -3);

Make a guess in your head. The answer opens Section 3.

3 · Core Concept

💡

Predict First — the answer

Output

3 -3 1

Dividing two ints throws away the fraction, cutting towards zero: 7/2 = 3 and −7/2 = −3. The remainder % takes the sign of the left number: 7 % −3 = 1.

Arithmetic: + − * / %

  • If both operands are integers, / is integer division: the fraction is dropped, rounding towards zero.
  • a % b is the remainder, defined so that (a/b)*b + a%b == a. Its sign follows a. It works only on integers.
  • Dividing by zero is undefined behavior (see below).

Relational: < > <= >= == !=

A comparison produces the int 1 if true and 0 if false. == compares; = stores.

Logical: && || ! and short-circuiting

In C any non-zero value counts as true and 0 as false. a && b is 1 if both are true; a || b is 1 if at least one is true; !a flips it. The result is always 0 or 1.

Short-circuit: && and || work left to right and stop as soon as the answer is known. If the left side of && is false, or the left side of || is true, the right side is not evaluated at all, so any assignment or ++ inside it never happens.

Bitwise: & | ^ ~ << >>

These work on the individual bits. & AND, | OR, ^ XOR (1 where the bits differ), ~ flips every bit (for ints, ~x = −x − 1), x << k shifts left (multiplies by 2^k^ while it fits), x >> k shifts right (divides a non-negative x by 2^k^, rounding down).

Assignment and compound assignment

a = b stores and also has a value (the stored value), so a = b = c = 7 works right to left. a += 3 means a = a + 3; likewise -= *= /= %= &= |= ^= <<= >>=.

++ and --

++i (pre-increment) adds 1 and gives the new value. i++ (post-increment) gives the old value, then adds 1. Same for --.

Conditional and comma operators

c ? x : y gives x if c is non-zero, else y. The comma operator (e1, e2) evaluates e1, then e2, and its value is e2.

Precedence and associativity

Precedence decides which operator binds first; associativity decides the order among operators of the same level.

Level (high → low)OperatorsAssociativity
1x++ x-- (post), ( ) function callleft to right
2++x --x +x -x ! ~ sizeofright to left
3* / %left to right
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``
11&&left to right
12`
13?:right to left
14= += -= …right to left
15,left to right

The full table also has a few operators you will meet later; they slot into levels 1 and 2. Two classic surprises: shifts bind tighter than & ^ |, and & ^ | bind looser than ==.

Undefined behavior

Some expressions have undefined behavior: the C standard places no requirement on the result, so different compilers (or the same one on different days) may print different things or crash. The main example here: changing a variable twice in one expression, or changing it and also reading it elsewhere in that expression, with no fixed order between them:

i = i++;            /* undefined */
i = i++ + ++i;      /* undefined */
printf("%d %d", i, i++);   /* undefined */

There are fixed ordering points: the end of each statement, and after the left side of &&, ||, ?: and the comma operator. Never give such code a single “correct” output; GATE expects you to recognise it as undefined.

4 · Visual

Bitwise on 8-bit patterns (12 = 00001100, 10 = 00001010)

   00001100        00001100        00001100
 & 00001010      | 00001010      ^ 00001010
 ----------      ----------      ----------
   00001000 = 8    00001110 = 14   00000110 = 6

 12 << 2 = 00110000 = 48        12 >> 2 = 00000011 = 3        ~12 = -13

Short-circuit:   a && (b = 10)  with a = 0
                 a is false --> stop; (b = 10) is never evaluated, b keeps 5

Bitwise operators act column by column on the bits; && and || may skip their right side entirely.

Output

8 14 6 -13 48 3

5 · Worked Examples

Example 1 Easy

What does this print?

printf("%d %d %d %d\n", 17 / 5, 17 % 5, -17 / 5, -17 % 5);

Solution

17 = 3 × 5 + 2. For −17: the quotient cuts towards zero to −3, and the remainder keeps the sign of −17: −17 − (−3 × 5) = −2.

Output

3 2 -3 -2

Example 2 Medium

What does this print?

int a = 0, b = 5, r;
r = a && (b = 10);
printf("%d %d\n", r, b);
r = a || (b = 10);
printf("%d %d\n", r, b);

Solution

First line: a is 0, so a && … is false at once; (b = 10) never runs. r = 0, b = 5. Second line: a is 0, so || must check its right side: b = 10, which is non-zero, so r = 1.

Output

0 5
1 10

Example 3 GATE

What do these print?

int a = 5, b = 6, c;
c = a++ + ++b;
printf("%d %d %d\n", a, b, c);
int x = 5 ^ 3 << 1;
int y = 2 + 3 * 4 > 10 && 1;
int z = 10 - 4 - 3;
printf("%d %d %d\n", x, y, z);

Solution

Program 1: a++ gives 5 (then a = 6); ++b makes b 7 and gives 7; c = 12. This is well defined: a and b are each changed once.

Output

6 7 12

Program 2: << binds tighter than ^: 3 << 1 = 6, and 5 ^ 6 = 3. Next, 3 * 4 = 12, 2 + 12 = 14, 14 > 10 is 1, 1 && 1 is 1. Finally, − is left-to-right: (10 − 4) − 3 = 3.

Output

3 1 3

6 · GATE Traps

⚠️

  • Integer division truncates towards zero: −7/2 is −3, not −4.
  • The sign of % follows the left operand: −7 % 2 = −1.
  • Short-circuit skips side effects: in 0 && x++ x is not incremented.
  • & and | are bitwise, not logical, and bind below ==: x & 1 == 0 means x & (1 == 0).
  • i = i++ and printf("%d %d", i, i++) are undefined behavior: no single correct output.
  • Assignment is right-associative: a = b = 3 sets b first.

8 · Memory Hook

🧠

“Old-then-add vs add-then-new.” x++ hands over the old value and then grows; ++x grows first. And && / || are lazy guards: they stop reading the moment they know the answer.

9 · Connect the Dots

Lesson 1.1.1 gave each type its range; this lesson combined values with operators. When an expression mixes types (an int with a double, a char with an int, a signed with an unsigned), C quietly converts some of them first. Next lesson answers: what exactly gets converted, and when does a value no longer fit?

10 · Self-Test

GATE format. Try every question before opening the vault.

Q1MCQ1 markOutput

What does this print?

int i = 3, j;
j = i++ * 2;
printf("%d %d\n", i, j);
j = ++i * 2;
printf("%d %d\n", i, j);
  • (A) 4 6 then 5 10
  • (B) 4 6 then 5 8
  • (C) 3 6 then 4 8
  • (D) 4 8 then 5 10

Q2NAT1 markRemainder

What is the value of -17 % 5 in C?

Numerical answer: ________

Q3MCQ2 marksShort-circuit

What does this print?

int x = 1, y = 0, z;
z = y && (x = 7);
z = z || x++;
printf("%d %d %d\n", x, y, z);
  • (A) 1 0 0
  • (B) 2 0 1
  • (C) 8 0 1
  • (D) 7 0 1

Q4MSQ2 marksUndefined behavior

Assume int i = 1; before each line. Which lines have undefined behavior?

  • (A) i = i++;
  • (B) i = i + 1;
  • (C) i = ++i + i++;
  • (D) i++ && i++;

Q5NAT1 markBitwise

What is the value of 7 & 12 | 1?

Numerical answer: ________

11 · Rule Card

  • 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.

12 · Concept Ledger

Unlocked in this lesson. From here on, these terms may be used.

integer division% remainderrelational operators&& || ! and short-circuitbitwise & | ^ ~ << >>compound assignmentpre/post increment and decrementconditional operator ?:comma operatorprecedenceassociativityundefined behavior

13 · Answer Vault

Answer Vault

Try the questions first. Open when you are ready to check.

Recall Warm-up

  • 8 (octal).
  • −3.

Self-Test

Q1 Answer: (A)1 mark

i++ gives 3 (i becomes 4): j = 6. ++i makes i 5 first: j = 10.

Output

4 6
5 10
  • (B) is wrong: ++i uses the new value 5.
  • (C) is wrong: i does change.
  • (D) is wrong: i++ uses the old value 3.

Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior

Q2 Answer: -21 mark

−17 = (−3) × 5 + (−2): −2.

Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior

Q3 Answer: (B)2 marks

y is 0, so (x = 7) is skipped: z = 0. Then z || x++: z is false, so x++ runs, giving 1 (true) and making x 2. z = 1.

Output

2 0 1
  • (A) is wrong: x++ does run in the second line.
  • (C) is wrong: x was never 7.
  • (D) is wrong: x = 7 is never executed.

Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior

Q4 Answer: (A) (C)2 marks

A and C change i twice (or change and read it) with no ordering point in between. D is defined: && is an ordering point between its sides.

  • (B) is wrong: i is read, then changed once: fine.
  • (D) is wrong: && orders its left side before its right side.

Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior

Q5 Answer: 51 mark

& binds before |: 7 & 12 = 0111 & 1100 = 0100 = 4; 4 | 1 = 5.

Re-read: Lesson 1.1.2 — Operators: arithmetic, relational, logical with short-circuiting, bitwise, assignment, ++ and --; precedence and associativity; undefined behavior

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.