Static Variable

Static Variable:

Static variables retain the value of the variable between different function calls.

Example 1

#include<stdio.h>

void increment1(void);

int main()

{

increment1();

increment1();

increment1();

increment1();

return 0;

}

void increment1(void)

{

static int l = 0 ;

printf ( “%d “, l ) ;

l++;

}

Output

0 1 2 3

Example 2

#include<stdio.h>

void display1();

int main()

{

display1();

display1();

return 0;

}

void display1()

{

static int c = 0;

printf(“%d “,c);

c += 5;

}

Output

0 5

Leave a Reply

Your email address will not be published. Required fields are marked *