OA. free
Free
IBM Core Computer Science Core Computer Science Medium

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

Choose 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:

  1. int i = 10 creates an integer with value 10.
  2. int *p = &i makes p point to i.
  3. 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).
  4. Inside foo(), *p dereferences the received address to access the value 10.
  5. printf("%d\n", *p) outputs: 10