Storage Classes in C

There are four main storage classes available in c programming:

1.Auto

2. Static

3.Extern

4.Register

  • Used for variables and also used in the function.
  • According to the storage class, compiler behavior will be changed on that variable.
  • Storage classes include some information like …
  • The default value of the variable, memory where it is stored, Scope(visibility) and life of the variable.
  • We can use it for function also but only static and extern.
  • For the variable, we can use all four storage class.

Auto:

  • If we don’t mention any storage class, for a local variable the compiler will treat Default as Auto.
  • Auto variable default value is unpredictable.
  • Auto variable stored in stack
  • A small portion of the stack for every function is called stack frame.
  • In that stack frame, the Auto variable is stored.
  • If we declare an auto variable in the main function and we call it in another function directly, it generates an error.
  • For all function, the separate stack frame is allocated & after completion of program stack frame will quit.
  • Scope: The scope of an Auto variable is within a block where we can declare it.
  • Life of Auto variable: It starts when the function execution is started and ends when the function execution is completed.
  • Stack follow LIFO (Last in First out) mechanism.
  • Dangling pointer: After freeing memory of pointer, if still it holds an address of the previous variable then it’s called the dangling pointer.
  • Until and unless we can’t call function, memory is not allocated.
  • When we call the function then only memory will be allocated.
  • All the auto variables are local variables but all the local variables may not be auto variables.

The advantage of Auto:

  • Security purpose
  • Temporary purpose

Static:

  • The default value of the static variable is zero.
  • We can declare static variable as a Global and Local Variable.
  • The default value will be stored in a data section.
  • Life of static variable: It starts when the function execution is started and ends when the function execution is completed.
  • If we declare a static variable as Global than it is visible only within this file & if we declare a static variable as the local variable than it is visible only for this function.
  • For the static variable, Re-initialization is not done.
  • A static function can be called only the function which is present in the same file.
  • Static variables and function are only linked to saving file only (linkage)

Linkage: visibility of variable within a  i)External , ii)No linkages  & iii)Internal linkage

  • external linkage means in another file that we can access the specific variable.
  • No linkage means the static variable is within braces { … }.
  • Internal linkage means we can access that particular variable only in present file.

Extern:

  • The default value of Extern is zero and it is stored in the data section.
  • If we declare the global variable with storage class, default it is treated as Extern.
  • The scope of extern is, if we declare it globally all the function can access it, we declare locally it will access into current function only.
  • Life of extern: It starts when the function execution is started and ends when the function execution is completed.

Register:

  • Default value of register is unpredictable.
  • Not possible to declare Globally.
  • All the auto variable stored in the stack, but register storage class may store into CPU, If CPU is busy then it will be stored in a stack.
  • The processor can access faster because of CPU internal Register, So Register will requesting into CPU into Internal Register.
  • Disadvantage: We can’t get access the address of Register.
  • We don’t have authority to access the register Storage class.
  • Advantage: Processing speed will increase
  • Program execution increase
  • Scope: Within block
  • Life of Register: It starts when the function execution is started and ends when the function execution is completed.

 

 

 

 

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: