Static Storage Class in C

o	Keyword : static
o	Storage Location : Main memory
o	Initial Value : Zero and can be initialize once only.
o	Life : depends on function calls and the whole 
        application or program.
o	Scope : Local to the block.

Syntax :

	static [data_type] [variable_name];

Example :

	static int a;

There are two types of static variables as :

a) Local Static Variable
b) Global Static Variable

Static storage class can be used only if we want the value of a variable to persist between different function calls.

Program :

/*  Program to demonstrate static storage class.
Creation Date : 10 Nov 2010 00:14:22 AM
Author : www.technoexam.com [Technowell, Sangli] */

#include <stdio.h>
#include <conio.h>

int main(void)
{
	int i;
	void incre(void);
	clrscr();
	for (i=0; i<3; i++)
	incre();
	getch();
	return 0;
}

void incre(void)
{
	int avar=1;
	static int svar=1;
	avar++;
	svar++;
	printf("\n\n Automatic variable value : %d",avar);
	printf("\t Static variable value : %d",svar);
}

Output :

Automatic variable value : 2	Static variable value : 2

Automatic variable value : 2	Static variable value : 3

Automatic variable value : 2	Static variable value : 4_

Leave A Comment

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