What is a register storage class in C language?

CServer Side ProgrammingProgramming

There are four storage classes in C programming language, which are as follows −

  • auto
  • extern
  • static
  • register

Register variables

  • The keyword is register.

  • Register variable values are stored in CPU registers, rather than in memory where, normal variables are stored.

  • Registers are temporary storage units in CPU.

  • They allow faster access time for register variables than normal variables.

Example 1

Following is the C program for register storage class

 Live Demo

#include<stdio.h>
main ( ){
   register int i;
   for (i=1; i<=5; i++)
      printf ("%d ",i);
}

Output

The output is stated below −

1 2 3 4 5

Example 2

Consider another C program for register storage class −

 Live Demo

#include<stdio.h>
int main(){
   register int a;
   printf("%d",a); //prints default value of a =0
}

Output

The output is stated below −

0

Example 3

Following is the third C program for static storage class −

#include<stdio.h>
int main(){
   register int i = 10;
   int *p;
   //int *p = &i; //error occurred ,here we are trying to request address of register    variable
   printf("Value of i: %d", *p);
   printf("Address of i: %u", p);
}

Output

The output is stated below −

Error:add of reg var?
raja
Published on 11-Mar-2021 12:26:15
Advertisements