by Dinesh Thakur Category: Type And Variables

Static variable is a special variable that is stored in the data segment unlike the default automatic variable that is stored in stack. A static variable can be initialized by using keyword static before variable name.

This is useful if we want to isolate pieces of a program to prevent accidental changes in global variables. Static local variables are variables whose value is held in a function call to another.


static int a = 5;

A static variable behaves in a different manner depending upon whether it is a global variable or a local variable. A static global variable is same as an ordinary global variable except that it cannot be accessed by other files in the same program / project even with the use of keyword extern. A static local variable is different from local variable. It is initialized only once no matter how many times that function in which it resides is called. It may be used as a count variable.

The function count () returns the number of times it has been called. See the local int variable is initialized. This boot is only valid for the first time the function is called as a must maintain its value from a call to the other. What the function does is increase in every call and return its value. The best way to understand this is by implementing static local variable.


#include <stdio.h>
#include <conio.h>
void count(void)
   static int count1 = 0;
   int count2 = 0;
   printf("\nValue of count1 is %d, Value of count2 is %d", count1, count2);
int main()
   return 0;

Static Variable

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content