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;
}
Show answer & explanation
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:
int *a = NULL;— Initialize pointer a to NULLint b = 5;— Initialize variable b with value 5a = &b;— Make pointer a point to address of bprintf("%d ", (*a)++);— Post-increment on dereferenced pointer:*aevaluates 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
- 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.