This blog is under construction

Sunday, 13 May 2012

register storage class in C

Register variables:
Registers are special storage area in CPU.  Variables declared using register storage Class will be stored in CPU registers which helps to improve the execution time of a program. Basically, frequently used variables will be declared as registers. Arithmetic, logical operations can be carried out on register variables.  But, accessing address of registers variable is not allowed.

Usage of scanf for register variable results in below error:
main() {
     register in a;
     scanf("%d", &a);
register.c:5: error: address of register variable ‘a’ requested

Basically, address operator is not allowed on register variables.

Register variable example:
int main() {
       register int a = 100, b = 200;
       return 0;

Example C Program using register variables:
    #include <stdio.h>
    int main() {
          register int a, b;
          a = 100;
          b = 10;
          a = a + b;
          printf("Sum: %d\n", a);
          return 0;

  jp@jp-VirtualBox:~/$ ./a.out
  Sum: 110

No comments:

Post a Comment