What is a static variable in C

1 Answer

0 votes
// A static variable is a local variable that keep its value between the function call,
// till the program end. The default value of static variable is 0. 

#include <stdio.h>

void f(void);

int main(void)
{
    f();
    f();
    f();
    
    return 0;
}

void f(void)
{ 
   static int n; 
   
   n++; 
   
   printf("%d\n", n); 
}

  
/*
run:
    
1
2
3

*/

 



answered Jun 20, 2017 by avibootz

Related questions

1 answer 192 views
2 answers 171 views
1 answer 154 views
154 views asked Dec 27, 2020 by avibootz
1 answer 236 views
1 answer 219 views
1 answer 104 views
...