storage class in c

storage class in c

When we create a variable in c, then two things are always attached with variable data type and storage class. storage class decides the extent (life-time) and scope (visibility) of the variable in the program.

Every variable gets some physical location from memory where variable’s value is stored in form of bits.Storage class defines the storage location of the variable like CPU register or memory beside it also define the life-time of the variable in the program.

A storage class helps us to trace the existence of a specific variable during the run-time of the program.

We have four different storage class.

auto:

An auto storage class is default class. if we have declared a variable within a function without defining any storage class then variable automatically promoted to auto storage class.

In simple word, we can say local variable (non-static) is an auto variable and scope of the auto variable is inside the function in which it declares and lives till control within  the function.

some properties related to an auto variable
  • scope: Within the block or function in which it declares.
  • life: Live till the control remain in function.
  • storage: auto variable creates in the stack of the process.
  • Default value: Garbage.
static:

When we will write static (prefix) with variable then it alive across the entire run of the program. means the extent of a static variable over the entire run of the program and the scope of a static variable within the module in which it declares.

If we will use the static keyword with global variable then the scope of global variable limited to the file in which it declares. in another word we can say that static makes a variable or function private for the file in which it declares.

When we have initialized a static variable then it will be created in Data Segment (.ds) either it will be created in .bss (block started symbol) of the process.

Note: static variable preserves its previous value and it is initialized at compilation time when memory is allocated. If we do not initialize the static variable, then it’s a responsibility of compiler to initialize it with Zero value.

some properties related to static variable
  • scope:
    the scope within the function in which declare.But if the global variable is static then the scope of that variable limited to the file in which it declares.
  • life:
    life of static variable whole execution of the program.
  • storage:
    if the static variable is initialized then stored in .ds otherwise stored in .bss.
  • Default value:
    the default value of the static variable is Zero.
extern:

The extern storage class only declare a variable and it used to give the reference of the global variable that is accessed by all files of the program.Simply extern says to the compiler that this variable is defined in elsewhere in the program, it only point to the already defined variable in the program.

A variable did not get any memory until you defined it.When you have declared a variable,then you just give the information of the variable to the compiler,but there is no memory reserve it .You can declare a variable multiple time in your program but define only once.

Suppose a project contains two file Display.c and Calculation.c . All calculation related function defined in calculation file and display related function defined in the display file.

If you compile the above project then you will get linker error “undefine reference to gData” because, at the linking time, linker did not resolve the address of gData.

So how we can solve this problem?
Yes, you are thinking in the right way using the extern keyword, we can solve this problem. If we declare gData using the extern keyword in Display file then the linker will resolve the address of gData.

some properties related to the global variable

scope:
It accesses by all function.
life:
life of the global variable is the throughout of the execution of the program.
storage:
If the global variable is initialized then stored in .ds otherwise stored in .bss .
Default value:
The defaults value of the global variable is Zero.

Note: we can declare a variable multiple time in a program but define only a single time.  

register:

Register variable stored in CPU register instead of memory and its properties are generally similar to the local variable.Accessing of the register is faster than the memory. So generally, register variable is used in looping.
A register keyword only gives the indication to the compiler to store this variable in the register instead of RAM ,But it totally depends on the compiler.The compiler decides where to put the variable in register or RAM .

Note: We can not use & and * operator with register variable because access the address of the register variable is invalid.

error: address of register variable ‘Data’ requested.

 

 

 

2 comments

Leave a Reply