C++ evaluation order with pre/post operators and defaults: what does this call print?\n\n#include<iostream.h>\nint CuriousTabFunction(int a, int b = 3, int c = 3)\n{\n cout << ++a * ++b * --c;\n return 0;\n}\nint main()\n{\n CuriousTabFunction(5, 0, 0);\n return 0;\n}

Difficulty: Easy

Correct Answer: -6

Explanation:


Introduction / Context:
This problem focuses on pre-increment and pre-decrement semantics inside a single multiplicative expression, with explicit arguments overriding the function defaults. You must evaluate each operand carefully before the multiplication.


Given Data / Assumptions:

  • Call is CuriousTabFunction(5, 0, 0); defaults are not used.
  • Expression printed: ++a * ++b * --c.


Concept / Approach:
Pre-increment (++x) changes x and yields the incremented value; pre-decrement (--x) changes x and yields the decremented value. There is no sequence point issue within this expression because each variable is modified once and used once. Just compute each operand, then multiply.


Step-by-Step Solution:
1) Initial values: a = 5, b = 0, c = 0. 2) ++a → a becomes 6, yields 6. 3) ++b → b becomes 1, yields 1. 4) --c → c becomes -1, yields -1. 5) Multiply: 6 * 1 * (-1) = -6. 6) cout prints -6.


Verification / Alternative check:
Try the default call CuriousTabFunction(5) and you get (6) * (4) * (2) = 48 because the defaults start at 3 (but the given call overrides them).


Why Other Options Are Wrong:
8 or 6 ignore the negative operand; -8 mis-multiplies the factors; 0 would require one factor to be 0, which is not the case after pre-increment/decrement.


Common Pitfalls:
Misapplying post-increment rules or assuming defaults still apply when explicit arguments are provided.


Final Answer:
-6

More Questions from Functions

Discussion & Comments

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