What is static variable in C Programing?

(Last Updated On: April 9, 2017)

Answer: Static variable in C programing is the variable which value persist between different function calls throughout the life cycle of the program.

For example, in below function staticVarCount (), s is a static variable and when we make a call again and again, its value will be increased e.g.1,2,3…and so on. Value of s will persist, even though its scope is within the function itself.



Below is the same function implementation with non static variable s. when we make multiple calls, its value will be always 1 as variable s will die as soon as control goes out of the function.



Static Variable in C Program Example



Static Variable:1
Static Variable:2
Non Static Varable:1
Non Static Varable:1



  • Static variables in c and  c++ automatically get initialize by zero, if we don’t initialize it by zero or some number.
  • There can be a Global and local Static variable in c. Local static variable will be always taken on priority within the scope. Do read example below.
Global and Local Static variable C Code Example


Value of s:10