Storage Classes in C

Ad Code

Storage Classes in C

 


Storage classes in C are used to describe the features such as determine the lifetime, visibility, memory location, and initial value of a variable during the runtime of a program.


types of storage classes in C:
There are four types of storage class:

  • Automatic
  • External
  • Static
  • Register
Storage
Classes
Storage
Place
Default
Value
ScopeLifetime
autoRAMGarbage
Value
LocalWithin function
externRAMZeroGlobalTill the end of the main program Maybe declared anywhere in the program
staticRAMZeroLocalTill the end of the main program, Retains value between multiple functions call
registerregisterGarbage
Value
LocalWithin the function

1. auto: This is the default storage class for all the variables declared inside a function or a block. So, the keyword auto is rarely used while writing programs in C. The visibility and scope of the automatic variables is limited to the block in which they are defined. They are assigned a garbage value by default whenever they are declared.

Example:

#include <stdio.h>
int main()
{
int a; //auto
char b;
float c; 
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and c. 
return 0;
}
OUTPUT

garbage garbage garbage

2. Static: Static local variables are visible only to the function or the block in which they are defined. The variables defined as static specifier can hold their value between the multiple function calls. A same static variable can be declared many times but can be assigned at only one time. Hence, static variables preserve the value of their last use in their scope. By default, they are assigned the value 0 by the compiler.

Example

#include<stdio.h>
void disp()
{
static int a = 10;
static int b = 24; 
printf("%d %d \n",a,b);
a++; 
b++;
}
void main()
{
int i;
for(i = 0; i< 3; i++)
{
disp(); // The static variables holds their value between multiple function calls.  
}
}

Output:

10 24 
11 25 
12 26 

3. Register: This storage class declares register variables which have the same functionality as that of the auto variables. The only difference is that the compiler tries to store these variables in the register of the CPU if a free register is available. The access time of the register variables is faster than the automatic variables.

Example

#include<stdio.h> 

int main() 
int i = 12; 
register int* a = &i; 
printf("%d", *a); 
getchar(); 
return 0; 

Output:

12


4. extern: The external storage class is used to tell the compiler that the variable defined as extern is declared with an external linkage elsewhere in the program. The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that the variable is declared elsewhere in the program. We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any block or method and can be declared many times but can be initialized at only once.
What is Global variable?
A global variable is a variable which is declared outside of all the functions. It can be accessed throughout the program and we can change its value anytime within any function as follows.

Example

#include <stdio.h>
int a;
void print(){
  a = 12;
  printf("a = %d\n", a);
}
int main(){
  a = 9;
  printf("a = %d\n", a);
  print();
  return 0;
}

Output:

a = 9 a = 12


Here, a is the global variable defined outside of all the functions. In the main function, its value was assigned as 9 and in the print function as 12. Declaring a global variable, some space in memory gets allocated to it like all other variables. We can assign a value to it in the same program file in which it is declared as in the above example.
So, if we want to use or assign it a value in any other program file, we can do by using extern keyword.
Step 1: Create first file like first.c and declared a global variable a.
first.c

int a=0;

Step 2: Now, we will declare variable 'a' as extern in a header file first.h and then include it in the second file in which we want to use this variable.
first.h

extern int a;

Stetp 3: Now in the second program file externexam.c, here use the global variable 'a', with including the header file in it by writing #include "first.h". Here we assigned a value 9 to the variable 'a' and thus the value of 'a' in this program becomes 9.
externexam.c
#include "first.h"
main(){
  a = 9;
  printf("%d", a);
}



Post a Comment

0 Comments

Ad Code