The following program main() { inc(); inc(); inc(); } inc() { static int x;…

2015

The following program

main()
{
inc(); inc(); inc();
}
inc()
{
static int x;
printf("%d", ++x);
}
  1. A.

    prints 012

  2. B.

    prints 123

  3. C.

    prints 3 consecutive, but unpredictable numbers

  4. D.

    prints 111

Attempted by 513 students.

Show answer & explanation

Correct answer: B

The function inc() uses a static variable x, which is initialized to 0 once and retains its value across calls. The main() function invokes inc() three times.

Each call increments x using pre-increment (++x) before printing: 1, then 2, then 3. Thus, the output is 123.

Explore the full course: Isro