Filter by Tags

What are static local variables in C

Votes

0

0 0

Explain with an example the concept of static local variables in C.

Ravinder Singh
Posted on : Mon, Jun 12, 2017
Views : 162
Comments

Ask Question

If you want to ask question you must LogIn or SignUp
Login SignUp

Answers (2)


1

Static local variables are those variables which can be used within a scope where it is declared and the memory allocated for the variable is not destroyed untill and unless the program terminates. Unlike other variables the memory space stays alloacated to the static variables even if the function call ends.

e.g.

void go()

{

  static int count=0;

  printf("%d",++count);

}

int main()

{

  printf("%d",go());

  printf("%d",go());

return 0;

}

 

Apurva Thakker
Mon, Jun 12, 2017
Loading...
Comments
Apurva Thakker :- int go() { static int count=0; return ++count; }
2017-06-12 7:02:01
Apurva Thakker :- Output: 12
2017-06-12 6:52:36

Edit Answer

Submit

1

Static Local Variable - In C Language Static local variables are definded inside a function and their value is not destoryed even after running the same function againe.

example: Below is the a fucntion containg local static variable and the main function.

int one(){

        static int v=0;

        v++;

        return v;

}

int main(){

       cout<<one();

       cout<<one();

}

 

output: 1 2

 

in above example value of "v" in first function call is 1 and in second fuction call the value on "v" is 2 as the value of "v" was not destroyed after the first call. 

Sahil Gaba
Mon, Jun 12, 2017
Loading...
Comments

Edit Answer

Submit
This answer will permanently deleted. Do you really want to delete this Answer ?
To Add your Answer Please
LogIn | SignUp

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp