register storage class specifier
This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the microprocessor if a free register memory is available.
This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register memory is not available, these are then stored in the normal memory only.
#include <stdio.h> int main() { register int r=10; ++r; printf("r=%d",r); return 0; }
[quote]
Output:
r=11
[/quote]
#include <stdio.h> int main() { register int r=10; ++r; printf("r=%d",r); printf("Enter a value: "); scanf("%d",&r); ++r; printf("r=%d",r); return 0; }
[quote]Output: ErrorĀ [/quote]
Analysis
A register is a special kind of variable which doesn’t allow to access address i.e. pointer or address related functionality doesn’t allow on it.
auto storage class specifier
This is the default storage class for all the variables declared inside a function or a block. ‘auto’ variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope). the auto variable assigned garbage value by default whenever they are declared.
#include <stdio.h> void abc() { auto int a=5; ++a; printf("a=%d\n",a); } int main() { abc(); abc(); abc(); return 0; }
[quote]
Output:
a=6
a=6
a=6
[/quote]
Analysis
‘a’ is an auto variable which is created within abc() and it will create always when control passed to abc() and automatically destroyed when control will pass outside of abc()
#include <stdio.h> void xyz() { int a=10; ++a; printf("a=%d\n",a); } int main() { int a=20; ++a; xyz(); xyz(); ++a; printf("a=%d\n",a); return 0; }
[quote]
Output:
a=11
a=11
a=22
[/quote]
Analysis
By default, any type of variable is ‘auto’ within the function or block that’s why main() function related data can be access in abc() and vice versa.
#include <stdio.h> int main() { int a=10; auto int a=20; ++a; printf("a=%d",a); return 0; }
[quote]Output: Error Multiple declarations for ‘a’[/quote]
Analysis
In any kind of scope, only one variable can be created with any unique name i.e. multiple variables are not allowed in the same scope with the same name.
#include <stdio.h> int main() { int a=10; ++a; printf("a=%d\n",a); { auto int a=20; ++a; printf("a=%d\n",a); } ++a; printf("a=%d\n",a); return 0; }
[quote]
Output:
a=11
a=21
a=12
[/quote]
#include <stdio.h> int main() { int a=10; ++a; printf("a=%d\n",a); { auto int a=20; ++a; printf("a=%d\n",a); } ++a; printf("a=%d\n",a); }
[quote]
Output:
a=11
a=21
a=12
[/quote]
#include <stdio.h> int main() { int a=10; --a; printf("a=%d\n",a); { auto int a=15; int b=20; ++a; ++b; printf("a=%d b=%d\n",a,b); } ++a; ++b; printf("a=%d b=%d\n",a,b); return 0; }
[quote]Output: Error undefined symbol ‘b’[/quote]
Analysis
By default, any type of variable scope is ‘auto’ and scope is restricted within the block or body. ‘b’ is a local variable in nested block it’s not allowed to access in the main block.