In this tutorial, we will learn about c storage class. We will learn how a variable, functions etc. are stored in c program.
C Storage class defines the scope, visibility and life-time of of any variable/function. In other words, storage class in C decides where will any variable be stored in memory, the scope where any variable can be accessed, the life-time of any variable i.e. how long a variable will be existing etc. Every variable is stored somewhere. Depending on the type of storage, it is either stored in stack, heap etc. Similarly, variable can be accessed globally or inside a function only. A variable can be available throughout program execution or it may be deleted just after completion of a function execution.
C Storage class also decides what will be the initial value of the variable if not initialized. All of the above scenario is decided by storage class we use with every variable/function. Let’s see it point wise –
C Storage class decides –
A. Storage area of variable.
B. Scope of the variable, where a variable can be accessed.
C. Life-time of the variable, how long a variable will exists.
D. Default value of the variable if it is not initialised.
There are 4 types of C storage classes. Below picture depicts those classes clearly.
Now, we will go through each type one by one.
1. Auto
2. Static
3. Extern
4. Register
1. Auto Storage class in C
This is the default c storage class for the variable defined inside a function or block. Use auto keyword to declare auto variable. While declaring a variable inside function or block, if you do not specify the storage class it will automatically be assumed of auto storage class. That’s why auto keyword is rarely used while writing program in c. Some of the features of variable of auto storage class are as below –
A. Auto variables can be accessed within the block or function they are defined and not outside it.
B. If auto variables are not initialised, they are automatically initialised by some garbage value.
C. If there are nested blocks. Then, the variables defined in parent block can be accessed inside the child blocks.
D. The auto variables can be accessed outside their scope using pointer.
Some examples are –
int i=0; { // First block…. auto int i=10; printf(“First time = %d”, i); { // Second block…. auto int i=60; printf(“Second time = %d”, i); } } printf(“Third time = %d”, i);
Here, all the variables (all i) have auto storage class. Notice that some variables have auto specifier explicitly written while some do not have.
But, all the variables are of auto storage class. It does not matter whether auto is mentioned or not while defining the variable.
Now, what will be the output of the code snippet? Can you guess it? Before reading the answer, please try to give answer yourself. Write the answer in a copy. Then, read the answer here.
The output is
First time = 10 Second time = 60 Third time = 0
Output explanation: We declared and defined a variable i and initialised it with 0. Then, a block is started. This is first block. Here, another variable i is declared and defined. The scope of this variable is within first block only. In c, If you have 2 variables with same name, then, local variable would be given more preference over other variables. So, printf(“First time = %d”, i); statement will print i with value 10. After this statement, Another block starts. This is second block. Again the same thing, A variable with name i is declared and defined. Then, A printf() statement follows this statement. Again, variable i with value 60 would be given more preference over others. Scope of variable i with value 60 is within second block only. it can not be accessed outside second block. Similarly, Scope of variable i with value 10 is within first block. So, it can not be accessed outside first block. So, printf statement outside first block will print value 0.
2. Static Storage class in C
Static c storage class is used to define the static variable. We use static keyword for this type of variable. Static variables are declared only once. They preserve the value even after they are out of scope. Hence, static variables preserve the last value. Since static variables are declared only once, memory is allocated only one time. However, static variables have scope local to the function in which they are defined. Global static variable can be declared anywhere in the program. For example,
int main() { printf(“%d”, newValue()); printf(“%d”, newValue()); printf(“%d”, newValue()); printf(“%d”, newValue()); return 0; } int newValue() { static int i; return i++; }
Output: Note that we have defined static variable inside newValue function. However, we are printing the value inside main function. Also, we have not initialised variable i. Since it is static variable, it will automatically be initialised with value 0; So, When first time newValue is called, newValue method will return value 0, then, 1, then, 2, then, 3..and so on. Note that the statement static int i is executed only once in whole program execution.
3. Extern storage class in C
We use extern keyword for this type of variable. Extern storage class is used when we need to define a variable at different block from where it is to be used. Basically, the variable is defined in a block A. But, it can be accessed and changed from block B, C…. etc.
We use extern keyword to define extern storage class variable. The main purpose of using extern variables is that they can be accessed from different files in a large program (that has a lot of files). A global variable can be made extern by adding extern keyword before it. This means we are using the same global variable in other files as well instead of defining a new variable. For example,
We have 3 files(file1.c, file2.c and file3.c). In file1.c, we will define global variable. In file 2, we will use this global variable using extern keyword. In file3.c, we are trying to use global variable without using extern keyword in other file.
file1.c
#include<stdio.h> int a=7; // Defining global variable int main() { printf(“%d”, a); return 0; }
file2.c
#include "file1.c" int main() { extern int a ; //By using extern keyword, we informed compiler that variable a is in other file. printf(“%d”, a); return 0; }
file3.c
#include "file1.c" int main() { printf(“%d”, a); // It will throw error since we have neither defined variable a nor mentioned that variable a is in other file. return 0; }
You can also use extern keyword to use variable, defined at some other places, in same file.
int main() { extern int x; // Informing the compiler that variable x is defined somewhere else in this file. x = 10; printf("%d",x); return 0; } int x; // Defining Global variable x
4. Register storage class in C
We use register keyword to define variable of this type. If any variable is being used frequently, you would like to store the variable in a place from where it can be accesses frequently and fastly. In such scenario, we use register storage class. Such variables are stored in register instead of memory. So, register variables have faster access than normal variables. Only few variables can be stored in register. Since we are storing the variable in register, we can not get the address of this variable using pointer. We use register keyword to define a register class variable. Since we have limited register, so, if free register is not available, the variable will behave as auto storage class variable. Means, if there are no free register while running the program, the register variable will be stored in memory instead of register. So, it’s behaviour will be same as auto variable in this case. Example,
#include<stdio.h> int main() { register int a=7; printf("%d", a); return 0; }
Conclusion
Depending on the requirements, we use different variables of different c storage class. If you do not define any storage class explicitly, it would be considered as auto storage class variable in C.
You must be logged in to post a comment.