659 views
1 1 vote
Life of static variables span across function calls but their scope is respective to their function body only.

What is the meaning of this statement?

1 Answer

0 0 votes
Suppose ,there are two functions.

Sum0() and sum1(). An static variable is declared in function sum0; (i.e)

int  sum0(

{

static int a=0;// This statement executes during first function call.and for rest of four calls this statement don't executes.

a++;

-------------

-------------

return 0;

}

int Sum1()

{
-----------

-----------

 a++;// Here static variable a will not be visible in this function.i.e (scope of a static  variable  is limited to the function in which it is defined );

}

int main( )

{

  for(int i=0;i<5;i++)

     sum0();/*when this function is called  five times local static variable a is defined once i.ememory

                  is allocated once and value increments five times. After function returns last time,memory is

                   deallocated . (i.e) static variables span across function calls.

return 0;

}
Position:
Show:

Related questions

1 1 vote
1 1 answer
152
152 views
khanshahima4 asked 6 days ago
152 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
13 13 votes
1 1 answer
1.3k
1.3k views
GO Classes asked Jul 2
1,252 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
16 16 votes
2 2 answers
673
673 views
GO Classes asked Jul 2
673 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...
9 9 votes
1 1 answer
495
495 views
GO Classes asked Jul 2
495 views
Consider the following code idea from the source:typedef struct { double x, y; } Point; void reset(Point p) { p.x = p.y = 0; } int main() { Point a = {12.0, 42.0}; Point ...