What is the value of 'b' after the execution of the following code statements:…

2009

What is the value of 'b' after the execution of the following code statements:

c = 10;
b = ++c + ++c;

Answer: D. NoneIn C, the pre-increment operator ++x first modifies the variable and then yields the updated value for use in the surrounding expression. The C standard also…

  1. A.

    20

  2. B.

    22

  3. C.

    23

  4. D.

    None

Attempted by 42 students.

Show answer & explanation

Correct answer: D

In C, the pre-increment operator ++x first modifies the variable and then yields the updated value for use in the surrounding expression. The C standard also requires that between two sequence points, an object must not be modified more than once by the evaluation of an expression — when an expression violates this rule, the standard leaves the outcome undefined, so no single value is guaranteed by the language for that expression.

  1. The statement c = 10; sets c to 10, and b = ++c + ++c; applies the pre-increment operator to the same variable c twice within one expression, with the two ++c occurrences joined only by +, which is not a sequence point in C.

  2. Because there is no sequence point between the two increments, the standard leaves undefined how many times c is read for the addition and which intermediate value each read observes — this is the same class of undefined behavior as the classic example i = i++ + i++;.

  3. Two evaluation strategies are commonly observed in real compilers: one completes each ++c immediately and adds the two yielded values in turn (c: 10 -> 11 giving 11, then 11 -> 12 giving 12), producing 11 + 12 = 23; another performs both increments on c first and then reads its final value (12) for both operands of the addition, producing 12 + 12 = 24 — and because the behavior is undefined, the standard does not require either of these two outcomes, or any other particular outcome, from a conforming implementation.

  4. Since the standard does not fix the value of b to any single guaranteed number, none of the specific numeric options (20, 22, or 23) can be relied upon as the value produced by a conforming C implementation.

This matches the well-documented C sequence-point restriction: between two sequence points, an object's stored value must not be modified more than once by the evaluation of an expression, and — as a separate, additional condition that applies whenever an expression both modifies an object and also needs its prior value for some other purpose in that same evaluation — that prior value may only be read to determine the new value being stored, never for any other use. Expressions of the form x++ + x++ or ++x + ++x are consistently described in C references as undefined behavior because they violate the first of these two requirements outright, which means the standard imposes no requirement at all on the resulting value, not merely that different compilers may pick between a couple of plausible answers — which is exactly why this expression is set as an answer-key example testing recognition of undefined behavior rather than arithmetic alone.

So the value of b cannot be reliably pinned to 20, 22, or 23 for a standard-conforming compiler — the value of b is None (of the given numeric options).

Explore the full course: Coding For Placement

Loading lesson…