“register” keyword in C

CProgrammingServer Side Programming

Register variables tell the compiler to store the variable in CPU register instead of memory. Frequently used variables are kept in registers and they have faster accessibility. We can never get the addresses of these variables. “register” keyword is used to declare the register variables.

Scope − They are local to the function.

Default value − Default initialized value is the garbage value.

Lifetime − Till the end of the execution of the block in which it is defined.

Here is an example of register variable in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   register char x = 'S';
   register int a = 10;
   auto int b = 8;
   printf("The value of register variable b : %c\n",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 be used with pointer also. It can have address of memory location. It will not create any error.

Here is an example of register keyword in C language

Example

 Live Demo

#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
raja
Published on 08-Oct-2018 09:50:02
Advertisements