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.

16 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..

Priyanka kumari 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....

Unknown said...

no it has to give an error

Unknown 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

Unknown said...

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

Unknown said...

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

Unknown said...

because register variable doesn't have memory address

Unknown said...

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

Unknown said...

show me one register variable example with real time

Unknown said...

show me one register variable example with real time

Unknown said...

Sir please give perfect example with no error

chintan said...

it working because if registers are not available they are stored as "auto" variables... correct me if i am wrong

Unknown said...

U r correct chintan