Machine-translated by Google. Questions are authored in English.
Top

Discussion

 

What is the output of this C code?

void main()
    {
        m();
        m();
    }
    void m()
    {
        static int x = 5;
        x++;
        printf("%d", x);
    }

 

  • A.6   7
  • B.6   6
  • C.5   5
  • D.5   6

Answer: A

Correct answer: 6 7

A static local is initialised once, before the program starts, and keeps its value between calls - its storage lasts as long as the program even though its name is local. So the first call prints 6 and the second 7. An ordinary local would print 6 both times.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.