Home  >  Article  >  Backend Development  >  In C language, the "register" keyword

In C language, the "register" keyword

王林
王林forward
2023-08-25 22:21:181263browse

In C language, the register keyword

Register variables tell the compiler to store the variable in a CPU register instead of in memory. Frequently used variables are kept in registers, where they have faster accessibility. We can never get the addresses of these variables. Register variables are declared using the "register" keyword.

Scope - They are local.

Default value - The default initialization value is garbage.

Lifetime - Before the execution of the block in which it is defined ends.

The following is an example of a register variable in C language:

Example

Demonstration

#include <stdio.h>
int main() {
   register char x = &#39;S&#39;;
   register int a = 10;
   auto int b = 8;
   printf("The value of register variable b : %c</p><p>",x);
   printf("The sum of auto and register variable : %d",(a+b));
   return 0;
}

Output

The value of register variable b : S
The sum of auto and register variable : 18

Register keyword can also be used Used with pointers. It can hold the address of a memory location. It doesn't produce any errors.

The following is an example of the register keyword in C language

Example

Real-time demonstration

#include<stdio.h>
int main() {
   int i = 10;
   register int *a = &i;
   printf("The value of pointer : %d", *a);
   getchar();
   return 0;
}

Output

The value of pointer : 10

The above is the detailed content of In C language, the "register" keyword. 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