Consider the following C program: #include <stdio.h> int main() { int i = -3, j = 2, k = 0, m; m = ++j && ++i || ++k; printf("%d %d %d %d", i, j, k, m); return 0; } What values are printed for i, j, k and m?

Difficulty: Medium

Correct Answer: -2 3 0 1

Explanation:


Introduction / Context:
This question tests your understanding of operator precedence, pre increment operators and short circuit evaluation for logical AND (&&) and logical OR (||) in C. It is a classic exam style problem where you must carefully track the order in which expressions are evaluated and how side effects change the variables.


Given Data / Assumptions:

    Initial values are i = -3, j = 2, k = 0.
    The expression is m = ++j && ++i || ++k;.
    In C, the ++j and ++i are pre increment operations; they change the variable first and then yield the new value.
    The operator precedence rules state that ++ has higher precedence than &&, which has higher precedence than ||, so the expression groups as m = ((++j && ++i) || ++k).


Concept / Approach:
Logical operators in C use short circuit evaluation. For the logical AND operator, if the left operand is zero (false), the right operand is not evaluated. For the logical OR operator, if the left operand is nonzero (true), the right operand is not evaluated. The result of a logical expression is 1 for true and 0 for false. We must apply these rules to determine the final values of i, j, k and m.


Step-by-Step Solution:
Evaluate ++j: j becomes 3 and the value of ++j is 3, which is nonzero (true). Since the left side of the && is true, we evaluate ++i: i becomes -2 and the value of ++i is -2, which is nonzero (true). The expression ++j && ++i therefore evaluates to true, which is represented as 1. Now evaluate the outer expression: (true) || ++k. Because the left operand of || is already true (1), short circuit rules mean that ++k is not evaluated, so k remains 0. The overall result of (true) || ++k is true, so m is assigned 1.


Verification / Alternative check:
After all evaluations, the variable values are: i = -2, j = 3, k = 0 and m = 1. Plugging these into the printf call printf("%d %d %d %d", i, j, k, m) produces -2 3 0 1 as text on the screen. Running a small test program in a C compiler will confirm this behavior on any conforming implementation.


Why Other Options Are Wrong:
Option b assumes that k is incremented, but short circuit evaluation of the logical OR operator prevents ++k from executing because the left side of || is already true.
Option c has incorrect values for i and m, which do not match the logic of pre increment and logical evaluation.
Option d incorrectly leaves j unchanged and m as 0, ignoring the effect of ++j and the logical operations.


Common Pitfalls:
Many students forget that logical operators return 1 or 0 rather than the original operand values, and they often misapply short circuit rules. Another common mistake is to mis group expressions, forgetting that && binds more tightly than ||. When in doubt, rewrite the expression with explicit parentheses to clarify the evaluation order.


Final Answer:
The program prints -2 3 0 1, corresponding to i = -2, j = 3, k = 0 and m = 1.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion