Home » Articles/Editorial

C Language Storage Classes

By: IncludeHelp, on 07 AUG 2016

auto, static, register and extern

Storage classes define the following properties to an object (usually variable, constants).

  • Default value
  • Storage
  • Scope or Visibility
  • Lifetime

Default value

Storage classes determine the default values to the uninitialized variable. If you declare a variable which is uninitialized. Storage class determines that what value will variable contain?

Storage

Storage classes also determine the storage of the variable, where memory will allocate to the variable? (there are only two memories locations for the variable: A) register or B) Primary Memory).

Scope or Visibility

Storage classes determine the scope or visibility of the variable, i.e. in which part of program you can access that variable. You can not access the variable beyond the scope.

Lifetime

Storage classes determine the Lifetime of variable i.e. how long variable will exist into the memory.

Types of Storage Classes

There are four storage classes in C language:

  • auto or aromatic
  • static
  • register
  • extern

auto/automatic

An automatic variable declared at the starting of any block. Memory allocation and free will be done on entry and exit of the block, automatic variable stores in the main memory.
Declaration: auto int a; or int a;
Default Value: Garbage
Storage: Primary Memory
Scope or Visibility: Local to Block, An automatic variable can access within the block in which variable is declared.
Lifetime: Local to Block, An automatic variable freed on exit of the block.

static

A static variable declared at the starting of any block. Memory allocates at the first calling of the function and freed when program ends. Static variable also stores in the main memory.
Declaration: static int a;
Default Value: 0
Storage: Primary Memory
Scope or Visibility: Local to Block, A static variable can access within the block in which variable is declared.
Lifetime: Entire Program – A static variable's reside in the memory until program's end, but you can access the variable with in the block only in which variable is declared.

register

A resister variable stores in the CPU registers and Allocate, Free on Block entry, exit. Registers are the memory parts of the CPU which are allocated in the CPU itself to fast access.
Declaration: register int a;
Default Value: Garbage
Storage: Primary Memory
Scope or Visibility: Local to Block, A static variable can access within the block in which variable is declared.
Lifetime: Local to Block, An automatic variable freed on exit of the block.

extern

All storage classes, we have been learned so far have the limited access; extern is only a storage class that has access to other files too. We can access one file's variable into another declaring variable as extern.
Declaration: extern int a; [it's a declaration, variable must be defined somewhere else]
Default Value: 0
Storage: Primary Memory
Scope or Visibility: Global – extern variable can be accessed anywhere in any file.
Lifetime: Entire Program – extern variables reside in the memory until program's end.

Comments




Copyright © 2024 www.includehelp.com. All rights reserved.