What is the output of the following C program snippet?
IBM technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What is the output of the following C program snippet?
#include<stdio.h>
void foo(int*);
int main()
{
int i = 10, *p = &i;
foo(p++);
}
void foo(int *p)
{
printf("%d\n", *p);
}
Pick ONE option
Show answer & explanation
Answer: A. 10
The post-increment operator p++ returns the current value of p (the address of i) before incrementing the pointer itself. This address is passed to foo(), which dereferences it to print the value at that address, which is 10. The increment of p happens after the function call completes, so it has no effect on the output.
Step-by-step Derivation:
int i = 10creates an integer with value 10.int *p = &imakes p point to i.foo(p++)uses post-increment: the current value of p (address of i) is passed to foo, then p is incremented (but this happens after the call).- Inside
foo(),*pdereferences the received address to access the value 10. printf("%d\n", *p)outputs: 10