1. Home
  2. Computer Science MCQs
  3. C Programming
  4. void tick(void) { static int c = 0; c++; printf("%d ", c)…

Added 30 Nov -0001

void tick(void) { static int c = 0; c++; printf("%d ", c); } is called three times from main. What is printed?

  1. 1 2 3 Correct answer
  2. 0 1 2
  3. 1 1 1
  4. An error, because a static local may not have an initialiser

Correct answer

A. 1 2 3

13 views

MCQSEXAM

Explanation

Static storage duration gives the local object a single instance for the whole program, initialised once before main starts, so each call increments the remembered value. An automatic local would restart at zero and print 1 1 1. Static locals are zero-initialised by default when no initialiser appears, unlike automatic variables whose value is indeterminate.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs