Static Variables and Functions in C With Examples

Static Variables in C

Basically, when static variables are declared, they create only a single copy of them and are also termed as Class Variables.

They retain the value assigned in the corresponding scope and do not initialize the variables again in the new scope of it.

  • Static variables remain in the memory space during the execution of the code.
  • The default value of the initialization of the Static variable is zero (0).
  • In C programming, it is mandatory for the static variables to be initialized using the constant literal else it returns an error.



In the above code snippet, the value of the local variable gets initialized again after every function call.

Unlike the local variable (x), the static variable (counter) was initialized only once and it did retain the same copy of variable throughout the execution of the program.


Static Functions in C

Static functions in C basically restrict the scope of the method to the corresponding file. These functions can also be called without having the object initialized.

  • Static functions usually enhance the usability of the code i.e. makes the code ready to be reused again.
  • It limits the visibility of the scope of the function within that particular program.





Thus, in this article, we have understood the importance and behavior of static variables and static functions in the C programming language.

By admin

Leave a Reply

%d bloggers like this: