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 202 views
2 answers 176 views
1 answer 160 views
160 views asked Dec 27, 2020 by avibootz
1 answer 242 views
1 answer 225 views
1 answer 111 views
...