When a variable within a function is declared with the static specifier, the variable has a permanent duration. In other words, the memory storage allocated for the variable is not destroyed when the scope. If excecution ever returns to the scope of the variable, the last value stored in the variable is still there. Here is an example:
Code:
//using the static specifier
#include<stdio.h>
int add_two(int x, int y){
static int counter=1;
printf("This is the function call of %d,\n",counter++);
return (x+y);}
main(){
int i,j;
for(i=0,j=5;i<5;j--)
printf("the addition of %d and %d is %d.\n",i,j,add_two(i,j));
return 0;}
the output you get should be something like
----output----
this is the function call of 1,
the addition of 0 and 5 is 5
this is the function call of 2,
the addition of 1 and 4 is 5
this is the function call of 3,
the addition of 2 and 3 is 5
this is the function call of 4,
the addition of 3 and 2 is 5
this is the function call of 5,
the addition of 4 and 1 is 5
----output----
The vaules stored by counter are retained because the duration of the variable is permanent. In other words, although the scope of counter is within the block of the add_two() function, the memory loction of counter and the value saved in the location are not changed after the calling of add_two(). I hope this help with what the static specifier does.