Storage Class In C / C++ And How To Use Automatic, Register, Static And External
Table of Contents
Storage class:
When a storage class in c/c++ is not present in the declaration the compiler assumes default storage class based on the place of declaration. The storage class besides the following various aspect of variables:
Lifetime: the time between the creation and destruction of a variable.
Scope: location where the variable is available for use
Initial value: default value taken by an initial variables
Place of storage: place in memory where the storage is allocated for variables
The general syntax of storage class is :
Class data type variablename;
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Need of storage class in C/C++:
By the purpose use of storage class in C/C++, the class makes our program efficient and first.
Types of storage class in C/C++:
- Automatic (auto)
- External (Extern)
- Static (static)
- Register (register)
Automatic Storage Class in C/C++:
In computer programming, an automatic variable is a local variable that is allocated and deallocated automatically when program flow enters and leaves the variable’s scope. The scope is the lexical context, particularly the function or block in which a variable is defined. Local data is typically (in most languages) invisible outside the function or lexical context where it is defined. Local data is also invisible and inaccessible to a called function but is not deallocated, coming back in scope as the execution thread returns to the caller.
All the variables inside block or function without any storage class specifier is called automatic variables and the keyword ‘auto’ is used for automatic storage class.
For example:
function()
{
int a,b;
}
function()
{
auto int a,b;
}
This is called automatic because the storage is reserve automatically each time when the control enter the function or block and the released automatically when the function or block is terminated.
Example write a program which illustrates the default initial value of an automatic Storage Class in C/C++:
1 2 3 4 5 6 7 8 |
#include<stdio.h> main() { auto int sum; printf(“%d”, sum); } Output: Garbage value |
In this program, the variable sum is an automatic storage class. Here the variable sum is not initialized and still, we want to print the value of the sum so compiler it will print and unpredictable value. the value is known as garbage value.
Register Storage Class in C/C++:
The feature of the register storage class is given below
Storage area: CPU register
Default initial value: Garbage value
Scope: Local to the block in which the variable is declared
Write a program which illustrates register storage class in C/C++:
1 2 3 4 5 6 7 8 9 10 11 12 |
#include<stdio.h> #include<conio.h> main() { register int x; printf("the no is :"); for (x=1; x<=4; x++) printf("%d", x); } Output: the no is :1234 |
static storage class in C/C++:
In the static storage class, the keyword static is used to declare a static variable. The static variable is not reinitialized but the auto variable is reinitialized. The small difference of auto variable is that the initial value of the auto is garbage value but the static variable initial value is zero.
Static global variable
A variable declared as static at the top level of a source file (outside any function definitions) is only visible throughout that file (“file scope”, also known as “internal linkage”). In this usage, the keyword static is known as an “access specifier”.
Static function
Similarly, a static function – a function declared as static at the top level of a source file (outside any class definitions) – is only visible throughout that file (“file scope”, also known as “internal linkage”).
Write a program which illustrates the work of static storage class in C/C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> #include<conio.h> main() { xyz(); xyz(); xyz(); } xyz() { static int x=1; x++; printf("%d ",x); } Output: 2 3 4 |
Example how to Write a counter program which illustrates the work of static storage class in C/C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> /* declaration of th function */ void func(void); static int counter = 5; /* initialize global variable */ main() { while(counter--) { func(); } return 0; } /* definition of the function */ void func( void ) { static int i = 5; /* local static variable */ i++; printf("i is %d and count is %d\n", i, counter); } Output: i is 6 and count is 4 i is 7 and count is 3 i is 8 and count is 2 i is 9 and count is 1 i is 10 and count is 0 |
External storage class in C/C++:
To understand how external variables relate to the extern keyword, it is necessary to know the difference between defining and declaring a variable. When a variable is defined, the compiler allocates memory for that variable and possibly also initializes its contents to some value. When a variable is declared, the compiler requires that the variable be defined elsewhere. The declaration informs the compiler that a variable by that name and type exists, but the compiler does not need to allocate memory for it since it is allocated elsewhere. The extern keyword means “declare without defining”. In other words, it is a way to explicitly declare a variable or to force a declaration without a definition. It is also possible to explicitly define a variable, i.e. to force a definition. It is done by assigning an initialization value to a variable. If neither the extern keyword nor an initialization value is present, the statement can be either a declaration or a definition. It is up to the compiler to analyze the modules of the program and decide.
The variables that are active and alive throughout the entire program. The external variable is also known as global variables. Here the keyword extern is used to declare an external variable. The external variable is used to outside in function
Write a program which illustrates the work of External storage class in C/C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> #include<conio.h> extern x= 5; main() { xyz(); xyz(); xyz(); } xyz() { x++; printf("%d ",x); } Output: 6 7 8 |
Electronic Reniveson