Operators & Precedence Questions with Answers

27 practice questions on Operators & Precedence (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 27 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

#include <stdio.h>

int main(void) {
    int a, b;
    a = (b = 4) + 1;
    printf("%d %d\n", a, b);
    return 0;
}
  1. 4 4
  2. 5 4
  3. 1 4
  4. 5 5

Answer: B. 5 4

Explanation

An assignment expression has the value assigned, so (b = 4) yields 4 and a becomes 5.

Step-by-step solution

  1. (b = 4) assigns 4 and evaluates to 4.
  2. a = 4 + 1 = 5.

Why the other options are wrong

  • A. a is 5.
  • C. Not 1.
  • D. b is 4.

Tip: Assignment returns value.

Question 2Easy

What is the output?

#include <stdio.h>

int main(void) {
    int n = printf("Hello");
    printf(" %d\n", n);
    return 0;
}
  1. Hello 1
  2. Hello 0
  3. Hello 5
  4. Hello 6

Answer: C. Hello 5

Explanation

printf returns the number of characters it printed; "Hello" has 5 characters.

Step-by-step solution

  1. Printf with Hello prints 5 characters and returns 5.
  2. So n is 5.
  3. The second printf prints a space, then 5 and a newline.
  4. The output is Hello 5, which is option C.

Why the other options are wrong

  • A. Printf returns 5, the number of characters printed, not 1.
  • B. N is the count 5, not 0; printf printed 5 characters.
  • D. Hello has 5 characters; the space is printed by the second printf and is not counted in n.

Tip: Printf returns the number of characters it printed.

Question 3Easy

What is the output?

#include <stdio.h>

int main(void) {
    printf("%d %d\n", !!7, !0 + !5);
    return 0;
}
  1. 1 6
  2. 7 1
  3. 1 1
  4. 7 0

Answer: C. 1 1

Explanation

! yields 0 or 1: !7 is 0, so !!7 is 1. !0 is 1 and !5 is 0, so the sum is 1.

Step-by-step solution

  1. !!7 = !(0) = 1.
  2. !0 = 1 and !5 = 0, so the sum is 1.
  3. Output: 1 1.

Why the other options are wrong

  • A. !5 is 0.
  • B. ! gives 0 or 1 only.
  • D. !!7 is 1.

Tip: !! = convert to 0/1.

Question 4Easy

What is the output?

#include <stdio.h>

int main(void) {
    int x = 2 + 3 * 4 % 5;
    printf("%d\n", x);
    return 0;
}
  1. 5
  2. 14
  3. 4
  4. 0

Answer: C. 4

Explanation

*, / and % share precedence and group left to right:

3 * 4 = 12

12 % 5 = 2

then 2 + 2 = 4.

Step-by-step solution

  1. 3 * 4 = 12.
  2. 12 % 5 = 2.
  3. 2 + 2 = 4.
  4. X is assigned 4.
  5. Printf prints 4.

Why the other options are wrong

  • A. Five would need the sum to be 5, but 3 * 4 % 5 is 2, so 2 + 2 gives 4.
  • B. Fourteen ignores the % 5 part, which applies after 3 * 4 and leaves 2, so the result is 4.
  • D. Zero is never produced; the remainder of 12 % 5 is 2, and adding 2 gives 4.

Tip: Remember operator precedence for arithmetic expressions.

Question 5Medium

What is the output?

#include <stdio.h>

int main(void) {
    int i = 1, j = 1;
    int k = i++ || j++;
    printf("%d %d %d\n", i, j, k);
    return 0;
}
  1. 2 2 2
  2. 2 1 1
  3. 1 1 1
  4. 2 2 1

Answer: B. 2 1 1

Explanation

i++ yields 1 (true), so || skips j++.

i becomes 2, j stays 1, and the result of || is 1.

Step-by-step solution

  1. I++ gives the old value 1, which is true, and i becomes 2.
  2. Because the left side of || is true, j++ is skipped, so j stays 1.
  3. The result of || is 1, so k is 1.
  4. The output is 2 1 1, which is option B.

Why the other options are wrong

  • A. J stays 1 because || skips j++, and k is 1, not 2.
  • C. I is 2 after i++, not 1.
  • D. J stays 1, not 2, because the right side of || is skipped.

Tip: With || the right side runs only if the left side is false; with && only if the left side is true.

Question 6Medium

What is the output?

#include <stdio.h>

int main(void) {
    unsigned int n = 29;
    int count = 0;
    while (n) {
        n &= n - 1;
        count++;
    }
    printf("%d\n", count);
    return 0;
}
  1. 4
  2. 29
  3. 3
  4. 5

Answer: A. 4

Explanation

n &= n - 1 clears the lowest set bit each time, so the loop counts set bits. 29 is 11101 in binary, which has four 1s.

Step-by-step solution

  1. n = 29 is binary 11101.
  2. n &= n - 1 clears the lowest set bit each time.
  3. 11101 has four set bits, so the loop runs four times.

Why the other options are wrong

  • B. 29 is the starting value, not a count.
  • C. The binary form has four 1s, not three.
  • D. Five would need five set bits.

Tip: Loop count equals number of 1 bits.

Question 7Medium

The intent of the code below is to print "zero" only when x is 0, but it never prints. Which change fixes it?

int x = 0;
if (x = 0)
    printf("zero\n");
  1. Add a semicolon after if (x = 0)
  2. Initialise x with 1 instead of 0
  3. Change x = 0 in the condition to x == 0
  4. Change x = 0 in the condition to x === 0

Answer: C. Change x = 0 in the condition to x == 0

Explanation

x = 0 is an assignment whose value is 0, so the condition is always false. The equality operator == compares instead. (C has no === operator, and a semicolon after if would make the body unconditional.)

Step-by-step solution

  1. x = 0 assigns 0 to x, and the value of that assignment is 0.
  2. So the if test is 0, which is false, and the printf never runs.
  3. x == 0 compares x with 0 and gives true when x is 0.
  4. With == the test is true and zero prints, so the fix is option C.

Why the other options are wrong

  • A. A semicolon makes the if body empty, so printf would run every time, which is not the intent.
  • B. X is reassigned to 0 inside the test, so its starting value does not matter.
  • D. C has no === operator, so this does not compile.

Tip: In C, = assigns and == compares; a single = inside an if condition is a common bug.

Question 8Medium

What is the output?

#include <stdio.h>

int main(void) {
    int a = 5, b = 10, c = 7;
    int m = a > b ? a : b > c ? b : c;
    printf("%d\n", m);
    return 0;
}
  1. 5
  2. 10
  3. 7
  4. 1

Answer: B. 10

Explanation

?: groups right to left: a > b? a: (b > c? b: c). Since 5 > 10 is false, it evaluates b > c? b: c, which is 10.

Step-by-step solution

  1. a = 5, b = 10, c = 7.
  2. a > b is false, so evaluate b > c? b: c.
  3. b > c is true, so m = b = 10.
  4. Printf prints 10.
  5. Final answer: 10.

Why the other options are wrong

  • A. Five is a, but a > b is false, so the ternary moves on to test b > c instead of picking a.
  • C. Seven is c, but b > c is true, so the inner ternary picks b, which is 10.
  • D. One would need a true or false result, but the ternary returns the value b, not a comparison.

Tip: Remember that ?: groups right to left for nested conditions.

Question 9Hard

In C17, what is the status of the statement i = i++ + 1; where i is an int?

  1. Well defined: i ends up as i + 2
  2. Well defined: i ends up as i + 1
  3. Undefined behaviour, because i is modified twice without sequencing
  4. Implementation-defined but always one of i + 1 or i + 2

Answer: C. Undefined behaviour, because i is modified twice without sequencing

Explanation

The side effect of i++ and the assignment to i are unsequenced relative to each other, which the standard makes undefined behaviour; any result (or none) is permitted.

Step-by-step solution

  1. I++ increments i but returns the original value, so i = i++ + 1 uses the old value of i.
  2. The assignment and increment are unsequenced, leading to undefined behaviour.
  3. C17 standard says modifying a variable twice without sequencing is undefined.
  4. This makes the result unpredictable and not well defined.
  5. Thus, the statement has undefined behaviour as per C17 rules.
  6. Undefined behaviour, because i is modified twice without sequencing.

Why the other options are wrong

  • A. Well defined is wrong: modifying i twice without sequencing is undefined in C17, not a defined result.
  • B. Well defined with i + 1 is wrong; the same object is modified twice unsequenced, which is undefined behaviour.
  • D. Implementation-defined is wrong; the standard makes this undefined behaviour, so no compiler choice applies.

Tip: Avoid modifying a variable more than once in an expression without explicit sequencing.

Question 10Hard

What is the output?

#include <stdio.h>

int main(void) {
    int x = 5;
    int y = x++ > 5 ? x : -x;
    printf("%d %d\n", x, y);
    return 0;
}
  1. 6 -6
  2. 5 -5
  3. 6 -5
  4. 6 6

Answer: A. 6 -6

Explanation

x++ > 5 compares the old value 5, which is false. ?: has a sequence point after the condition, so x is already 6 when -x is evaluated, giving y = -6.

Step-by-step solution

  1. X++ > 5 uses the old value 5, so the test 5 > 5 is false.
  2. X++ still increments x, so x is now 6.
  3. The false branch -x is evaluated after the test, using the new x, so -x is -6.
  4. Y is -6 and x is 6, which is option A.

Why the other options are wrong

  • B. X is incremented to 6 by x++ even though the test is false.
  • C. Y uses the new x = 6, so -x is -6, not -5.
  • D. The false branch is taken, so y is -6, not 6.

Tip: A postfix increment still updates the variable; the ?: operator finishes the test before it picks a branch.

Practise all 27 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics