Showing posts with label c language. Show all posts

Storage class in c

Storage class tells about the storage location ,life time and initial value of a variable/function.

There are four types of storage class in c

1.auto storage class
2.register storage class 
3.static storage class 
4.extern storage class 

let's understand with the help of example

1. auto storage class 
 example declaration-   auto int a ;
       
or simply -    int a ;


A)Storage location -> Storage location of this variable is main memory i.e. if we declare the variable in auto storage mode then that variable will store into RAM 

 B)Default value -> Default value of this type of variable is GARBAGE , i.e.until we don't assign any value to this variable it will contain GARBAGE value.
    
 C)Scope ->      local to the block in which it's defined. i.e. we can use this variable in only the block in which it's defined.

 D)LifeTime -> Till the control remains in block .

 understand with example !!

main() 

 { 

int a;

printf("%d",a);// garbage value(initial value)

a=10;

printf("%d",a); // 10 

{ 

int  b=10; 

printf("%d",b);

}

printf("%d",b); // error b is not in its scope


}

if we ignore the third printf
 //printf("%d",b);     then code will work smoothly and first printf the GARBAGE value because initial value of auto class is GARBAGE  ,
in next time 10 will be printed, but in next printf there will be an error 

because the variable b is declared in another block and we are printing the value of b in another block.
that's why we say the scope of auto variable is block in which that is defined
 .

2.Resister storage class 

  A)Storage location ->Storage location of this variable is CPU register,they are stored in CPU register.but sometimes if the CPU register are not free they will get stored in main memory.

  B)Default value -> default value of this type of variable is GARBAGE .initial value to this variable is GARBAGE.

 C)Scope  ->  Scope of this variable is local to the block in which it is defined .
 

 D)LifeTime ->Till control remains within the block 

ex- register int a ; 


3.Static storage class 

  A) Storage location -> Storage location of this type of variable is main memory .

  B) Default value -> Default value of this variable is ZERO , initially the variable will contain ZERO.

  C) scope -> Local to the block in which the variable is defined.

  D)Life Time -> Till the value of variable persists between different function calls.


Declaration method -
 static int a ;
program to understand the term 
int func(); 

#include<stdio.h>
void main() 
 {
    int i ;

 int a=10;

   for(i=0;i<10;i++)
       { 
          func();
       }
 }
int func()
  {
static int c=10;
    printf("%d\n",c);c++;
return (0);
  }
            
Output:









10
11
12
13
14
15
16
17
18
19
  

Here you can see the value of c is persisting in function calling 

 4. External storage class

  A)Storage -> Storage class of this type of variable is Main memory ,variable of this type is stored in main memory. 

  B) Default value -> Default value of this type variable is ZERO 

  C)Scope  -> Global ( we can access this variable from any where any block . it's accessible for all block of program.

D)Life Time -> As long as the program execution doesn't comes too an end . 

method of declaration

extern int a ;

remember one thing about extern variable 
extern int a ; 
// it's a declaration so we  have to define it later to use in program

like :

1
2
3
4
5
6
7
8
9
10
extern int a ;
#include<stdio.h>
int main() 
 {
     int a=10;
  printf("%d",a);

   
return 0;
 }



Output:
1
10





if we use it wihout definition like below
1
2
3
4
5
6
7
8
9
10
extern int a ;
#include<stdio.h>
int main() 
 {
     //int a=10;
  printf("%d",a);

   
return 0;
 }


it will be error ! 
Output:
1
2
In function `main':
undefined reference to `a'










C program for quick sort

/*Program of sorting using quick sort through recursion*/



#include<stdio.h>

#define MAX 30

enum bool { FALSE,TRUE };



void display(int arr[],int,int);



void quick(int arr[],int low,int up);





main()

{



       int n,i;  int array[MAX];

    printf("Enter the number of elements : ");

         scanf("%d",&n);

  for(i=0;i<n;i++)

     {

     printf("Enter element %d : ",i+1);

           scanf("%d",&array[i]);

      }

   printf("Unsorted list is :\n");



display(array,0,n-1);



printf("\n");



quick(array,0,n-1);



printf("Sorted list is :\n");



display(array,0,n-1);



printf("\n");



return 0;



}       /*End of main() */





void quick(int arr[],int low,int up)

  {

       int piv,temp,left,right;

enum bool pivot_placed=FALSE;

left=low;

right=up;



piv=low; /*Take the first element of sublist as piv */



if(low>=up)

  return ;



printf("Sublist : ");



display(arr,low,up);



/*Loop till pivot is placed at proper place in the sublist*/



 while(pivot_placed==FALSE)

    {



   /*Compare from right to left  */



    while( arr[piv]<=arr[right] && piv!=right )

    

      right=right-1;

   



      if( piv==right )



pivot_placed=TRUE;



if( arr[piv] > arr[right] )

 {

       temp=arr[piv];

       arr[piv]=arr[right];

       arr[right]=temp;

       piv=right;

}



/*Compare from left to right */



while( arr[piv]>=arr[left] && left!=piv )

      left=left+1;



if(piv==left)

    pivot_placed=TRUE;



if( arr[piv] < arr[left] )

 {

     temp=arr[piv];

     arr[piv]=arr[left];

     arr[left]=temp;

     piv=left;

 }



}   /*End of while */



    printf("-> Pivot Placed is %d -> ",arr[piv]);



   display(arr,low,up);



    printf("\n");

 

   quick(arr,low,piv-1);

 

   quick(arr,piv+1,up);



 }/*End of quick()*/





void display(int arr[],int low,int up)

  {



    int i;

      for(i=low;i<=up;i++)

        printf("%d ",arr[i]);

}                                                         

You may like these .

 1. Bubble sort in c
 
 2. Heap sort in c
 
 3. Insertion sort in c 

C program for matrix multilplication , addition and subtraction


/*Matrix Add,Sub,Multi */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int n,**p,**q,**r,i,j,k,ch;
printf("enter the order of matrix\n");
scanf("%d",&n);
p=(int**)malloc(n*sizeof(int));
q=(int**)malloc(n*sizeof(int));
r=(int**)malloc(n*sizeof(int));
for(i=0;i<n;i++)
p[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
q[i]=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++)
r[i]=(int*)malloc(n*sizeof(int));

printf("enter the element of first matrix\n");  //inputing
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&p[i][j]);
printf("enter the element of second matrix");   //inputing
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&q[i][j]);
clrscr();
printf("your entered matrix is\n");     //printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
 printf("  %d",p[i][j]);
}
printf("\n");
}
printf("\n\n");                            //printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
 printf("  %d",q[i][j]);
}
printf("\n");
}
while(1)
{
clrscr();
printf("\n\tEnter 1. for Addtion :");
printf("\n\tEnter 2. for Mult :");
printf("\n\tEnter 3. for Subtract :");
printf("\n\tEnter 4. for Exit :");
printf("\n\tEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
for(i=0;i<n;i++)   //Add
{
for(j=0;j<n;j++)
{
r[i][j]=p[i][j]+q[i][j];
}
}
printf("\n Addtion matrix is\n");      //output printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",r[i][j]);
}
printf("\n");
} getch();
break;
}
case 2:
{
for(i=0;i<n;i++)   //multipicating
{
for(j=0;j<n;j++)
{
r[i][j]=0;
for(k=0;k<n;k++)
{
r[i][j]=r[i][j]+p[i][k]*q[k][j];
}
}
}
printf("\n multiplyed matrix is\n");      //output printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",r[i][j]);
}
printf("\n");
}getch();
break;
}
case 3:
{
for(i=0;i<n;i++)   //Add
for(j=0;j<n;j++)
{
r[i][j]=p[i][j]-q[i][j];
}
printf("\n Subtract matrix is\n");      //output printing
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d",r[i][j]);
}
printf("\n");
}
}  getch();
break;
case 4 : return;
default : printf("\n\tWrong choice");
}
}
getch();
}

C program for heap sorting

/* Program of sorting through heapsort*/

# include <stdio.h>

void display();

void del_root(int last);

void create_heap();

void heap_sort();

void insert(int num,int loc);

int arr[20],n;

void main()

{
    int i;

    printf("Enter number of elements : ");

      scanf("%d",&n);

    for(i=0;i<n;i++)

       {
           printf("Enter element %d : ",i+1);

              scanf("%d",&arr[i]);

        }
     printf("Entered list is :\n");

       display();


       create_heap();


     printf("Heap is :\n");

       display();


     heap_sort();


     printf("Sorted list is :\n");

         display();

}

void display()

   {     
       int i;


   for(i=0;i<n;i++)

         printf("%d  ",arr[i]);


 printf("\n");


  }

void create_heap()

    {
        int i;

     for(i=0;i<n;i++)


         insert(arr[i],i) ;
    }

 void insert(int num,int loc)

      {
          int par ;

   while(loc>0)

       {

              par=(loc-1)/2;

           if(num<=arr[par])

             {

               arr[loc]=num;

                return;
             }

             arr[loc]=arr[par];

            loc=par;
      }


         arr[0]=num;

  }

void heap_sort()

     {
          int last;

  for(last=n-1; last>0; last--)

         del_root(last);

     }

  void del_root(int last)

        {
            int left,right,i,temp;

            i=0; /*Since every time we have to replace root with last*/
 /*Exchange last element with the root */

         temp=arr[i];

         arr[i]=arr[last];

         arr[last]=temp;

        left=2*i+1;   /*left child of root*/

        right=2*i+2;  /*right child of root*/

        while( right < last)

           {
               
   if( arr[i]>=arr[left] && arr[i]>=arr[right] )

                  return;

               if( arr[right]<=arr[left] )

                 {
                    temp=arr[i];

                    arr[i]=arr[left];

                    arr[left]=temp;

                    i=left;
                 }
             else

                {
                   temp=arr[i];

                   arr[i]=arr[right];

                   arr[right]=temp;

                    i=right;
                }

         left=2*i+1;

         right=2*i+2;

      }

                
 if( left==last-1 && arr[i]<arr[left] )/*right==last*/

              {
                     temp=arr[i];

                     arr[i]=arr[left];

                     arr[left]=temp;
               }


         }       /  *End of del_root*/














You may like these .

 1. Bubble sort in c
2. Insertion sort in c 3. Quick sort in c

program of Insertion sort in c


/* Program of sorting using insertion sort */
#include <stdio.h>
#define MAX 20

main()
{
int arr[MAX],i,j,k,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/*Insertion sort*/
for(j=1;j<n;j++)
{
k=arr[j]; /*k is to be inserted at proper place*/
for(i=j-1;i>=0 && k<arr[i];i--)
arr[i+1]=arr[i];
arr[i+1]=k;
printf("Pass %d, Element inserted in proper place: %d\n",j,k);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}/*End of main()*/


Bubble sort in c

/* Program of sorting using bubble sort */

#include <stdio.h>

#define MAX 20

main()
 {

          int arr[MAX], i, j, k, temp ,n , xchanges;

          printf("Enter the number of elements : ");

                 scanf("%d",&n);

       for (i = 0; i < n; i++)

           {
              printf("Enter element %d : ",i+1);

                 scanf("%d",&arr[i]);
           }

           printf("Unsorted list is :\n");
 
      for (i = 0; i < n; i++)

          printf("%d ", arr[i]);
              
                 printf("\n");

 // Bubble sort works start from here 

 for (i = 0; i < n-1 ; i++)

       {
            xchanges=0;

     for (j = 0; j <n-1-i; j++)

          {

     if (arr[j] > arr[j+1])
           {
                 temp = arr[j];

                 arr[j] = arr[j+1];

                 arr[j+1] = temp;

                 xchanges++;
   
            }   //End of if


          } //End of inner for loop

        if(xchanges==0)    //  If list is sorted

            break;
 
printf("After pass%d,arrangement of elements",i+1);

             for (k = 0; k < n; k++)

                 printf("%d ", arr[k]);

            printf("\n");

      }  //End of outer for loop

           printf("Sorted list is :\n");

                 for (i = 0; i < n; i++)

                     printf("%d ", arr[i]);

               printf("\n");


    } //End of main()