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;
    }

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


1 comment:

  1. Dell Laptop Service center are giving repair service at the door. We should high quality Dell out of warranty Laptop Repair, removal of virus, screen removal, wireless network set up, battery removal, motherboard replacement to several other are offered at budget friendly price and it’s Negotiable. We can fix them all in time by our well experience and certified technicians. If you want to repair your laptop in front of your eyesight, than you may call us: 7217871051

    ReplyDelete