• Static variable have a property of preserving their value even after they are out of their scope.
  • Static variable preserve their previous values in their previous scope and are not initialized again the new scope.

Syntax

static data_type var_name = var_value;
  • Here data_type –> Data types like int, char, float etc.
  • Var_name -> Name of the variable given by user.
  • Value -> Any value to initialize. By default, it is zero.

  • A static variable remains in the memory while the program is running.
  • Static variables are allocated memory in data segment, not in stack segment.
  • Default values of static variables is assigned as 0 if not initialized explicitly.
  • In C, static variables can be initialized using constant literals.
  • Static global variables and functions are possible in C. The purpose of these is to limit scope of a variable or function to file.
  • Static variables cannot be declared inside the structure. The reason is compiler requires the entire structure elements to be placed together.
  • Memory allocation for structure members should be contiguous.

Categorized in: