Hello guys today in this post we will discus about storage class.
Storage class:
Every identifiers not only has data type but also has a storage class the storage class the storage class of an identifiers determine the following character-
- scope of an identifiers
- Storage of an identifiers
- Storage location of an identifiers
- Lifetime of an identifiers
- Linkage of an identifiers
- Initial value of an identifiers
The storage class of an identifiers can be specified will the help of storage class specified
following are storage class specifiers-
- auto
- static
- typedef
- register
- extern
It most one storage class specifier can be specified in a declaration statement.
Auto:
- Keyword used in auto.
- By default an identifiers declared with in a block has auto storage class.
- The auto identifiers is stored in main memory.
- The lifetime of automatic storage class is local i.e. the identifiers will come into existence from the point if its declaration and remaining into existence till the control goes out of the block.
- Auto identifiers are not initialized by default i.e. they have garbage value.
- auto identifiers can not be declared globally.
- They have no linkage.
Ex.
#include<stdio.h>
main()
{
auto int a=10;
printf("The value of a is %d \n",a);
{
int a=20;
printf("The value of a inside block is %d ",a);
}
printf("The value of a outside block %d \n",a);
}
Output:
The value of a is 10
The value of a inside block is 20
The value of a outside block is 10
register:
- Keyword used is register.
- Register storage class specified that the declared object will have local scoe
- It cannot be used in the declaration mode in the global scope.
- It is not initialized and needs to be initialized in the program.
- The address of register identifiers cannot be accessed hence applying will give compile time error.
- The register identifiers is stored in CPU register instead of main memory hence can be accessed faster. If CPU register are not available then It is stored in main memory and treated as auto storage class.
- Register storage class is commonly used for loop counters to improve the performance of program.
Static:
- keyword used is static.
- The lifestyle of static storage class can be declared with global and local scope.
- The variable of static storage class are implicitly initialized and not implicitly initialized value is 0 if int type 0.0 if float type and ' \0 ' if class type.
- It is stored in main memory.
- Static storage class can be declared with global and local scope.
- Static storage class variable when declared globally have external linkage and if declared locally have internal linkage.
- static variable can not be used as parameter.
Ex.
#include<stdio.h>
main()
{
int a=0;
for (i=1;i<=5;i++)
{
for (a);a=a+1;
}
}
void fun (int b)
{
static int t=20;
printf("The value of b=%d &t=%d ",b,t);
printf("The value of t=%d after inc."++i);
}
KEEP SUPPORTING..............
Comments
Post a Comment