Storage Class.
A storage class represents the visibility and a location of a variable. It tells from what part of code we can access a variable. A storage class is used to describe the following things:
The variable scope.
The location where the variable will be stored.
The initialized value of a variable.
A lifetime of a variable.
Thus a storage class is used to represent the information about a variable.
Extern storage class.
Extern stands for external storage class. Extern storage class is used when we have global functions or variables which are shared between two or more files.
Keyword extern is used to declaring a global variable or function in another file to provide the reference of variable or function which have been already defined in the original file.
The variables defined using an extern keyword are called as global variables. These variables are accessible throughout the program. Notice that the extern variable cannot be initialized it has already been defined in the original file
Example, extern void display();
First File: main.c
#include <stdio.h>
extern i;
main() {
printf("value of the external integer is = %d\n", i);
return 0;}
Second File: original.c
#include <stdio.h>
i=48;
Result:
value of the external integer is = 48
Register storage class.
You can use the register storage class when you want to store local variables within functions or blocks in CPU registers instead of RAM to have quick access to these variables. For example, "counters" are a good candidate to be stored in the register.
Example: register int age;
The keyword register is used to declare a register storage class. The variables declared using register storage class has lifespan throughout the program.
It is similar to the auto storage class. The variable is limited to the particular block. The only difference is that the variables declared using register storage class are stored inside CPU registers instead of a memory. Register has faster access than that of the main memory.
The variables declared using register storage class has no default value. These variables are often declared at the beginning of a program.
#include <stdio.h> /* function declaration */
main() {
{register int weight;
int *ptr=&weight ;/*it produces an error when the compilation occurs ,we cannot get a memory location when dealing with CPU register*/}
}
OUTPUT:
error: address of register variable 'weight' requested
Comments
Leave a comment