what is static variable in c

what is static variable in c

1 year ago 71
Nature

A static variable in C is a variable that has the property of preserving its value even after it is out of its scope. It is declared with the keyword "static" and can be used in both global and local scopes. The following are some key features of static variables in C:

  • A static variable preserves its previous value in its previous scope and is not initialized again in the new scope.
  • A static int variable remains in memory while the program is running, unlike a normal or auto variable which is destroyed when a function call where the variable was declared is over.
  • Static variables are initialized as 0 if not initialized explicitly.
  • In C, static variables can only be initialized using constant literals.

The syntax for declaring a static variable in C is as follows: static data_type var_name = var_value;.

Read Entire Article