how to write code for Storage Classes in C: Static (static)
3)Static:
There are two types of static variables:
(i) Local Static Variables
(ii)Global Static Variables
(i)Local Static Variables:
The scope of local static variable is same as that of an automatic variable.
The lifetime of a local static variable is more than that of the automatic variable. It is created at compile time and its life is till the end of program.
It is not created and destroyed each time the control enters a function or block.
It is created only once. It is not initialized every time the function is called.
If we do not initialize a static variable. The value taken by it is zero.
We can initialize them only by constants or expressions representing constants.
/* Program to understand the usage of local static variables*/
#include<stdio.h>
int main()
{
//Call update function 3 times to see its effect on static integers
update();
update();
update();
return 0;
}
int update()
{
static int z;
printf("\nThe value of z = %d",z);
z=z+10;
return 0;
}
Here we can see that we haven’t initialized the variable z but it is initialized automatically with value 0.
Also , the previous value of z is retained.
(ii)Global Static Variables:
When global variables are used, the static specifier doesn’t
extend the lifetime since they already have a lifetime which is equal to the lifetime of the program.
In this case, the static specifier is used to hide information.
If an external variable is static it can’t be used by other files of the program.
In above fig. the variable y is declared as static external variable, so we cannot use y in flies other than f1.c by putting the extern declaration in those files.
Storage Classes in C: Auto (Automatic)
Storage Classes in C: Auto (Automatic) | output
A variable in addition to data type has an attribute which is its storage class.
If we will use storage class properly the program will become more efficient and faster.
The general syntax of a storage class is:
Storage_class data_type variable_name;
There are four types of storage classes:
1)Automatic
2)External
3)Static
4)Register
A storage class is responsible for the four aspects of a variable:
1)Lifetime:
Time between the creation and destruction of a variable.
2)Scope:
Scope means the locations where the variable is available for use
3)Initial Value:
Default value taken by an uninitialized variable
4)Place of Storage:
The place in memory which is allocated to store the variable in memory.
1)Automatic:
All the variables declared without any storage class specifier are called as automatic variables.
This can be also done using the keyword auto.
The uninitialized automatic variables have garbage values.
There scope is limited inside the block or function inside the block where they are declared and we can’t use them outside these blocks and functions.
Now let us understand them using following programs:
/*Program to understand the working of automatic variables*/
#include
void fun()
{
int n=30;
printf("\nInside Function........");
printf(" \n n= %d",n);
}
int main()
{
int n=5;
printf("Before calling Function........");
printf("\n n= %d",n);
fun();
printf("\nAfter calling Function........");
printf("\n n= %d",n);
}
This storage class can be applied only to local variables . The scope, lifetime and initial value of register variable are same as that of the automatic variables.
The difference between the automatic and register variable is their place of storage.
The automatic variables are stored in the memory while the register variables are stored in CPU Registers.
The main advantage of register variables is that they can be accessed much faster as that of the variables stored in the memory.
So the variables which are used frequently can be assigned register storage class. We can declare loop counters as register variables as they are frequently used.
Let us see their use in the following program:
/*Program to illustrate the use of register variables*/
#include
int main()
{
register int i;
int j;
printf("\nUsing register storage class.......:\n");
for(i=0;i<200;i++)
printf("%d\t",i);
}
0 comments: