Storage Modifiers

Storage modifiers are keywords in C that are used to modify the storage class of a variable. The storage class of a variable determines its scope, lifetime, and initial value.

There are four storage classes in C:

  • auto
  • extern
  • static
  • register

The auto storage class is the default storage class for all local variables declared in a function. Local variables declared with the auto storage class are only visible within the function in which they are declared. They are also initialized to 0 when they are declared.

The extern storage class is used to declare a global variable. Global variables declared with the extern storage class are visible to all functions in the program. They are also initialized to 0 when they are declared, unless they are explicitly initialized to a different value.

The static storage class can be used to declare local variables or global variables. Local variables declared with the static storage class are visible only within the function in which they are declared. They are also initialized to the value that they were last initialized to, when the function is called again. Global variables declared with the static storage class are also visible only within the file in which they are declared. They are also initialized to the value that they were last initialized to, when the program starts.

The register storage class is used to declare variables that should be stored in a CPU register. Variables declared with the register storage class are not guaranteed to be stored in a register, but they will be stored in a register if the compiler can do so.

Here is a table that summarizes the effects of storage modifiers on variables in C:

Storage modifierScope
LifetimeInitial value
autoLocalWithin function0
externGlobal
Program scope0
staticLocalWithin functionLast initialized value
registerLocal
Within functionNot guaranteed

Leave a Reply

Your email address will not be published. Required fields are marked *