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

Discussion

In C, which statement about static inside a function is correct ?

  • A.Lifetime is entire program
  • B.It is initialized every time function is called
  • C.It is stored in stack
  • D.It cannot be modified

Answer: A

The correct statement is that its lifetime is the entire program.

A static variable declared inside a function has static storage duration: it is created once, before the program's main work begins, and lasts until the program ends. Its scope is still limited to the function, but it keeps its value between calls, which makes it useful for counters.

It is initialised only once, not on every call, so option B is wrong. It lives in the data (or BSS) segment, not on the stack, which is where ordinary local variables go. And it can be modified; a variable that cannot be changed is const, not static.

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

Post your comment

Please log in or register to post a comment.