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);
}- A.
prints 012
- B.
prints 123
- C.
prints 3 consecutive, but unpredictable numbers
- 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.