Home  >  Article  >  Backend Development  >  In C language, what is the register storage class?

In C language, what is the register storage class?

WBOY
WBOYforward
2023-08-30 09:45:151430browse

In C language, what is the register storage class?

There are four storage classes in the C programming language, which are:

  • auto
  • extern
  • static
  • register

Register variable

  • The keyword is register.

  • The value of a register variable is stored in the CPU's register, not in memory, as ordinary variables are stored in memory.

  • Register is a temporary storage unit in the CPU.

  • They allow register variables to have faster access times than ordinary variables.

Example 1

The following is the register storage class of the C program:

Demonstration

#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 that uses the register storage class−

Online demonstration

#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

The 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?

The above is the detailed content of In C language, what is the register storage class?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete