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 303 views
2 answers 310 views
1 answer 255 views
255 views asked Dec 27, 2020 by avibootz
1 answer 346 views
1 answer 319 views
1 answer 236 views
...