Wednesday, 29 August 2018

Storage Classes 2 (Static)

In this post we will discuss about static, extern and register storage classes.

But first we need understand what is the difference between life and scope :-

Life :-

The lifetime of variable in C language is the time, for which the variable have memory location in memory section. Its may be during the run time execution of the program or while creation or destruction of the function stack.

Scope :- 

The scope of variable in C language is that part of code till which we can directly access the variable. 


Static Storage Classes :-



Default Value :-
  • Default value of static variable is zero(0) or NULL if pointer.
  • We can declare a static variable, global as well as local.
Memory :-

Data section are divided into two things 
  • Initialize data section ( Data )
  • Uninitialized  data section ( BSS-Block started by symbol )
Scope :-

  • If we declared a static variable locally then the scope is within function or block.
  • If we declared a static variable globally then the scope is within that whole file.
Life :-
  • Life of static variable starts when program execution starts till the execution is completed.
Important Note :-
                              Re-initialization is not possible for static variable because life of the variable is up to end of compilation.
                              Global Static variable are having internal linkage. We can access that variable with in the file.

Tuesday, 28 August 2018

Storage Classes 1


Storage Classes are used to describe about the features of a variable. 


Basically there are four things that will decide the storage classes-

  • Default Value
  • Memory (Where it Stored)
  • Scope (Visibility)
  • Life
There are four storage classes in C Language are given bellow -
  1. Auto
  2. Static
  3. Extern
  4. Register 

Auto Storage Classes :- 

Default Value :- 
  • For local variable if we don't specify any storage type, then the compiler will treat that as auto .
  • All auto variable are local variable but we can not say all local variable auto variable.
Memory :-
  • Auto variable are present in function stack frame.
Scope :-
  • Scope of an auto is within a function or within block where it is declared.
Life :-
  • Life of an auto variable start when function execution start and ends when function execution ends.


Important Note :     
                              Re-Initialization is allowed for auto storage class. 
                              We should not catch Auto variable address while returning from the function.




Remaining storage classes will discuss in next post.