OA. free
Free
Qualcomm Embedded Systems & Hardware Embedded Systems & Hardware Medium

What is the output of this program?

Qualcomm technical mcq question, verified with a worked answer. Free to practise - no sign-up.

int main()
{
    #define h 6
    if (h > k)
    {
        printf("%d", k && (2, 4, 6));
    }
    else if (!n)
    {
        printf("%d", h * k);
    }
    else
    {
        printf("%d", printf("%d", (8, 9, 7)));
    }
    return 0;
}

What is the output of this program?

Choose one option.
Show answer & explanation
Answer: B. 0

The program has undefined behavior because variables k and n are declared but not initialized. However, in most environments, uninitialized global/static variables default to 0, and uninitialized local variables have indeterminate values. Assuming k = 0 and n = 0, the condition h > k (6 > 0) is true, so the first branch executes: printf("%d", k && (2, 4, 6)). The comma operator evaluates left-to-right and returns the rightmost value (6), so k && 6 becomes 0 && 6, which is 0 due to short-circuit AND evaluation.

Step-by-step Derivation:
Step-by-step execution:

  1. h is defined as 6 via macro.
  2. Variables k and n are undeclared (undefined behavior), typically assumed to be 0 in most environments.
  3. First condition: if (h > k)if (6 > 0) → true.
  4. Execute first branch: printf("%d", k && (2, 4, 6)).
  5. Comma operator in (2, 4, 6) evaluates to 6 (rightmost value).
  6. Expression k && 60 && 6.
  7. AND operator short-circuits: 0 && anything is always 0.
  8. printf("%d", 0) outputs: 0