Mask-group

Storage Classes in C



This basically includes the scope, visibility and lifetime of a variable which helps to trace the existence of a particular variable during the runtime of a program.

  • Auto

  1. The auto keyword is applied to all local variables automatically.

  2. It is the default storage class that is why it is known as an automatic variable.

​​​​​​​

  • Register

  1. The register variable allocates the memory in the register rather than RAM.Its size is same as register size.

  2. It has faster access than other variables.

  3. It is recommended to use register variables only for quick access such as in the counter.

  4. Note: We can’t get the address of the register variable.

  • Static

  1. The static variable is initialized only once and exists till the end of the program.

  2. It retains its value between multiple function calls.

  3. The static variable has the default value 0 which is provided by the compiler.

​​​​​​​​​​​​​​​​​​​​​

  • Extern

  1. The extern variable is visible to all the programs.

  2. It is used if two or more files are sharing the same variable or function

Important points:

  • A static int variable remains in memory while the program is running.

  • A normal or auto variable is destroyed when a function call where a variable was declared is over.

  • Static variables are allocated memory in the data segment,not stack segment