Home > Backend Development > C++ > body text

In C language, the 'register' keyword

王林
Release: 2023-08-25 22:21:18
forward
1329 people have browsed it

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;
}
Copy after login

Output

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

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;
}
Copy after login

Output

The value of pointer : 10
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template