Pages

Saturday 23 February 2019

Storage Classes in C

             Storage Classes are used to describe about the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.

C language uses 4 storage classes, namely:
1. auto
2. static
3. register
4. extern

1.Program to illustrate auto variable:
#include<stdio.h>
void autoStorageClass(void); // function declaration
int main()
{
    printf("\nA program to demonstrate auto Storage Classess in C");
    autoStorageClass(); //calling function
    return 0;
}

void autoStorageClass() //called function
{
  auto int a = 1;
   {
    auto int a = 2;
     {
      auto int a = 3;
      printf("\n auto int Value of the variable 'a' is : %d",a);
      }
     printf("\n auto int Value of the variable 'a' is : %d",a);
    }
  printf("\n auto int Value of the variable 'a' is : %d",a);
}

output:


2.Program to illustrate static variable: 
#include<stdio.h>
void staticStorageClass(void); // function declaration
int main()
{
  printf("\nA program to demonstrate static Storage Classess in C");
  staticStorageClass(); //calling function
  return 0;
}

void staticStorageClass() //called function
{
    int i = 0;
    printf("\nLoop started:\n");
    for (i = 1; i < 5; i++)   
    {
      static int y = 5;
      int p = 10;
      y++;
      printf("\nAt iteration %d The static int y value is %d\n",i, y);
      p++;
      printf("\nAt iteration %d The non static int p value is %d\n",i, p)
   }
    printf("\nLoop ended:\n");
}

output:

 3.
Program to illustrate register variable: 
#include<stdio.h>
void registerStorageClass(void); // function declaration
int main()
{
 
printf("\nA program to demonstrate register Storage Classess in C");
  registerStorageClass(); //calling function
  return 0;
}

void registerStorageClass() //called function
{
 
printf("\nDemonstrating register class\n\n");
  register char b = 'A' ;
  printf("\n Value of the register variable 'b' : %d", b); // prints ASCII value of A 
  register char x = 'a' ;
  printf("\n Va
lue of the register variable 'x': %d\n",x); // prints  ASCII value of a
}

output:


4. Program to illustrate extern variable:
consider two programs in two separate files  named by externfile1.c and externfile2.c

Program in externfile1.c
// A C program to demonstrate extern storage class
#include<stdio.h>
#include"externfile2.c"
int x=20; // global declaration
void externStorageClass(void); // function declaration
int main()
{
 
int x=10;
  printf("\n The value assigned with out extern to variable  x is :%d",x);
  externStorageClass(); //calling function
  printf("\n The value assigned with extern to variable 
                  x after sub program  is :%d",x); 
 return 0;

}

Program in externfile2.c
#include<stdio.h>
extern int x;
void externStorageClass(void) //called function
{
  printf("\n\n The value assigned with extern to variable  x is :%d",x);
}
 
Now compile the program one i.e externfile1.c the the output of extern storage class is  


No comments:

Post a Comment

Programs in turboc3 : Files

File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...