Storage Classes :
- The storage class in c decide the scope, lifetime and visibility of a variable.
- They precede the type that they modify. The specifiers which determine the type of storage classes in c are known as storage class specifiers.
There are four type of storage class specifiers in C :
- auto
- register
- static
- extern
Let's learn more about storage classes in c with the help of a diagram :
Storage specifier | How it has declared | Storage | Initial value | Scope | Life |
---|---|---|---|---|---|
auto | globally,locally | stack | garbage | within block | end of block |
extern | globally,locally | data segment | zero | global multiple files | till end of program |
static | globally,locally | data segment | zero | within block | til end of program |
register | globally,locally | CPU register | garbage | within block | end of block |
Now let's discuss each storage class separately.
➣Automatic storage class :
- The auto storage class is the default storage class for all the local variables that means for all the variables which are defined inside a block or a function.
- These variables are also called as local variables because they get destroyed after the execution of the block.
- As the variables are created automatically, garbage values (could be anything) are assigned to them by the compiler.
- The keyword auto is rarely used while writing programs in C.
Example of auto storage class :
➣Register storage class :
- The values stored in register storage class is same as the value stored in automatic storage class.
- The values of register class get stored in CPU register instead of RAM for faster retrieval of values in program. Forex- counter lie values.
- The default value of register variable is the garbage value.
- It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.
Example of register storage class :
➣Static storage class :
- The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope.
- The scope of the variable will not be effected i.e. variable remains local to the block in which it lies.
Therefore, making local variables static allows them to maintain their values between function calls.
- The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.
➣External storage class :
- The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.
- The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.
- Default value of any external variable is ‘0’ (zero).
Interview questions on storage classes
'or'
Each storage class can be asked by the interviewer separately.
Q2. Output of following program?
#include
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
}
Answer : 0000
Q3. #include
int main()
{
static int i=5;
if (--i){
printf("%d ",i);
main();
}
}
Answer : 4321
Q4. Output?
#include
int fun()
{
static int num = 16;
return num--;
}
int main()
{
for(fun(); fun(); fun())
printf("%d ", fun());
return 0;
}
Answer : 14 11 8 5 2
For any query related to the content mail to devkumar082001@gmail.com
Which is still underway, has also gone extremely smoothly. website design and development companies ability to consistently follow up with the client truly differentiates them.
ReplyDeleteThey proved to be a flexible partner premium niche edit who could accommodate feedback and quickly adapt to changing requirements.
ReplyDeleteThey go above and beyond, putting their heart and soul into their work.
ReplyDeletedesign firms San Francisco
The quality of your articles and contents is great.
ReplyDeletebest app designer
This blog Info is fabulous; I must wanna see best more from your writers.
ReplyDeleteUI consultant
The blog is good enough I again n again read this.
ReplyDeleteUI design services
I’m impressed with the special and informative contents that you just offer in such short timing.
ReplyDeleteBrand Booster