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:
(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..
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.
gcc shows no error to ranjith's program....
no it has to give an error
its not giving error when it is derefrenced using & i.e int*p=&a
what could be the reason......??
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...
because we cannot dereference the register variable since it has not any memory address
hey in turbo c iam getting the output and it is reading the variables.
because your file is .cpp change it to .c and you will get an error and modern compiler doesn't give error
because register variable doesn't have memory address
No both the progam are showing error
Saying-must take address pf memory location
show me one register variable example with real time
show me one register variable example with real time
Sir please give perfect example with no error
it working because if registers are not available they are stored as "auto" variables... correct me if i am wrong
U r correct chintan
Post a Comment