Storage Class in C | Basic IT Topic

Storage Class in C

storage class in c

Storage Class in C:-

Every c program has a storage class and a scope. The storage class is a data structure which decides the scope, storage default initial value, and life of variable function.

Different types of scope and rules present in c:-

  1. Program scope
  2. File scope
  3. Function scope
  4. Block scope

Program scope:-

If a variable and function are defined in one if they will be accessed in other files of a program is called program scoping.

File scoping:-

Writing static before a variable and function it means that variable unction it means that variable and function only can be access in same but can’t be accessed in other files of a program which is called file scoping.

Function scope:-

If a function declaration appears outside of any function, the identifier introduces file scope and external linkage, unless static is used or an earlier static declaration is visible.

Block scope:-

A Block is a set of statements that enclosed within the left and right braces ({ and } respectively).

There are 4 types of storage class in C:-

  1. Extern
  2. Static
  3. Auto
  4. Register

1.Extern

Extern command tells to the compiler either the variable is defined in same or other files of a program.

Extern variable can’t be initialized because it is called a variable declaration. The variable declaration does not allocate memory but definition allocate memory.

Example:-

main()
{
      int x=90;
      extern int x;
      printf(“%d”,x);
}

Output:-  90

2.Static

Static variable death at the end of the program. Auto variable is death when a function is puff from the stack.

Example:-
main()
{
        int i=1;
        while(i<=5)
{
       static int i=100;
       printf(“%d”,i);
       i++;
}
}
Output:-
100 101 102 103 104…………………..

3.Auto

Auto is the default storage class of a local variable.

All auto variables automatically allocate and deallocate memory when a function is push in pop and stack.

Example:-
main()
{
     int i;
     for(i=1;i<=5;i++)
       {
          Bbsr();
       }
}
int Bbsr()
{
       auto int x=1;
       printf(“%d”,x);
       x++;
}

Output:-   1  1  1  1  1

4.Register:-

A register generally use in a loop counter on  heavy duty.

Example:-
main()
{
      register int i;
      for(i=1;i<=8000;i++)
      {
           printf(“hi”);
      }
}
basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community