register storage class in c


Register storage class specifiers in c with example


A register storage class is very similar to auto storage class except one most important property. All register variable in c stores in CPU not in the memory.

Important points about register storage class

(1)In following declaration:

    register int a;

We are only requesting not forcing to compiler to store variable a in CPU. Compiler will decide where to store in the variable a.

(2)A register variable execute faster than other variables because it is stored in CPU so during the execution compiler has no extra burden to bring the variable from memory to CPU.

(3)Since a CPU have limited number of register so it is programmer responsibility which variable should declared as register variable i.e. variable which are using many times should declared as a register variable.

(4) We cannot dereference register variable since it has not any memory address. For example:
     
(a)
#include<stdio.h>
int main(){
    register int a=10;
    int *p;
    p=&a;
    printf("%u",p);
}

Output: Compilation error

(b)
#include<stdio.h>
int main(){
    register int a,b;
    scanf("%d%d",&a,&b);
    printf("%d  %d",a,b);
}

Output: Compilation error

(5) Default initial value of register variable is garbage.

(6) Scope and visibility of register variable is block.

15 comments:

ranjith said...

(b)
#include
int main(){
register int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
}

why we get compiler error.....plz explain..

Ritesh kumar said...

Hi Rajith,
Register variables are stored in CPU not in memory. So register variable cannot have memory address. &a gives memory address of variable a while variable a is register variable due to which we get compiler error.

Anonymous said...

gcc shows no error to ranjith's program....

Suresh Betahala said...

no it has to give an error

Sandeep Uniyal said...

its not giving error when it is derefrenced using & i.e int*p=&a
what could be the reason......??

Anonymous said...

both programs are compiling successfully...i tried both program and register variable has address also.after compilation you'l get the output with register variable 'a' address also...

kishoreaits said...

because we cannot dereference the register variable since it has not any memory address

Anudeep K said...

hey in turbo c iam getting the output and it is reading the variables.

Avnish Gupta said...

because your file is .cpp change it to .c and you will get an error and modern compiler doesn't give error

Diwakar Singh said...

because register variable doesn't have memory address

Abhishek kumar said...

No both the progam are showing error
Saying-must take address pf memory location

ummadipolu balaji said...

show me one register variable example with real time

ummadipolu balaji said...

show me one register variable example with real time

Virender singh said...
This comment has been removed by the author.
Unknown said...

Sir please give perfect example with no error