Storage Classes
Storage Classes in 'C' :
2) register :
- Storage classes tells information about storage location for variables, who can access variables, what will be default for it etc.
- There are four storage classes in 'C' as follow :
1) auto :
- By default storage class for members is auto.
- auto keyword is used to declare auto storage class for example auto int age;
- An auto variables are also called as automatic variables.
- They are stored in a computers memory.
- Default value for auto variables is unpredictable.
- They can be accesses inside the function or block.
- Variables having register storage class will get stored into a registers in CPU instead of memory.
- register keyword is used to define register storage class. For example register int age;
- Data from register can be fetched fast as compared to data from memory this is benefit of register storage class.
- Default value for register varable is any garbage value.
- They get stored in memory of computer.
- They can be accessed inside a function or block.
3) extern :
- It is used to create global data members so that they can be shared among multiple files.
- extern keyword is used to declare them. For example extern int age=45;
- They can be intialize only once in a original file.
- Default value of extern variable is zero.
- They get stored in a memory of computer.
- They can be accessed in entire file and also in other files where it is declared as extern.
4) Static :
- Static keyword is used to declare static storage class for data members in a program.
- Static members can be both local or global dependinb on way how they are declared.
- Default value for static variables is zero.
- Local static members can be access using local scope where as global static members can be access using global scope.
- They get stored in memory of computer.
- Important point to note about static variables is they are initialized only once in their lifitime.
- They persist or retain their values between function call.
- Example : Consider following code
#include<stdio.h>
int main()
{
int sum()
{
//local static var
Static int sum = 0;
sum++;
printf("sum = %d", sum);
}
sum();
sum();
return 0;
}
Output :
sum = 1
sum = 2
Note : Since static variables persist their value between function calls we get output 1 first function call and 2 for next function call.
Comments
Post a Comment
Please do not enter any spam link in message.