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>

int main()
{
    int *a = NULL;
    int b = 5;
    a = &b;
    printf("%d ", (*a)++);
    return 0;
}
Choose one option.
Show answer & explanation
Answer: D. 5

The post-increment operator (*a)++ returns the value of the dereferenced pointer before incrementing it. When printf executes, *a contains 5, so 5 is printed. The value of b is then incremented to 6, but this happens after the printf statement completes. The key distinction is that post-increment has lower precedence than dereference, so (*a)++ dereferences first, prints the current value (5), and increments afterwards.

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

  1. int *a = NULL; — Initialize pointer a to NULL
  2. int b = 5; — Initialize variable b with value 5
  3. a = &b; — Make pointer a point to address of b
  4. printf("%d ", (*a)++); — Post-increment on dereferenced pointer:
    • *a evaluates to 5 (current value at address stored in a)
    • Post-increment operator ++ returns the old value before incrementing
    • printf receives 5 and prints it
    • After printf returns, b is incremented to 6
  5. Output: 5

Note: Post-increment has higher precedence than the dereference in this context due to operator binding. The expression evaluates as (*(a))++ which returns the pre-increment value.