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

What will be the output of the program given below?

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

What will be the output of the program given below?

#include <stdio.h>

#define k !0 * 5

int main()
{
    #define h 6
    if (h > k)
    {
        printf("%d", k && (2, 4, 6));
    }
    else if (!h)
    {
        printf("%d", h * k);
    }
    else
        printf("%d", printf("%d", (8, 9, 7)));
    
    return 0;
}
Choose one option.
Show answer & explanation
Answer: A. 1

The macro k evaluates to !0 * 5 = 1 * 5 = 5 (due to operator precedence: ! binds tighter than *). With h = 6, the condition h > k (6 > 5) is true, so the first if-block executes. The expression k && (2, 4, 6) uses the logical AND operator; k is 5 (truthy), and the comma operator evaluates to 6 (the rightmost value). Since both operands are truthy, the && operator returns 1.

Step-by-step Derivation:
Step 1: Evaluate macro k

  • k is defined as !0 * 5
  • Operator precedence: ! (logical NOT) binds tighter than * (multiplication)
  • !0 = 1 (NOT false is true)
  • 1 * 5 = 5
  • Therefore, k = 5

Step 2: Evaluate h

  • h is defined as 6
  • Therefore, h = 6

Step 3: Evaluate conditions

  • First condition: h > k → 6 > 5 → true
  • Since the first condition is true, the if-block executes

Step 4: Execute if-block

  • printf("%d", k && (2, 4, 6))
  • Evaluate k && (2, 4, 6)
    • k = 5 (truthy, non-zero)
    • (2, 4, 6) uses comma operator, evaluates left-to-right, returns rightmost value = 6 (truthy)
    • In C, && (logical AND) returns 1 if both operands are true, 0 if either is false
    • 5 && 6 = 1 (both are non-zero/truthy)
  • printf("%d", 1) outputs: 1

Final Output: 1