Computer Concept & Programming In C - Unit II - 8

Q.9 Describe the storage classes in detail.
Related Questions -
Q. Write the storage classes supported in C with example.              (AKTU. 2008 - 09)      
Q. Discuss various storage classes in C with suitable example. Also give their significance.                                                                                   (AKTU. 2010 - 11)





Q. Write a short note on the various storage classes in C language.
                                                       (AKTU. 2012 - 13, 13 - 14)
Ans. Storage Classes: -
Because variables can have different attributes you would expect that they are not stored in the same way. Actually, the compiler keeps variables in different sections of memory according to their storage classes:
auto, static, register and extern.
The words static and auto indicate the duration attribute and the storage class at the same time.
Automatic and Static Variable: -
auto when a specific module of (a function) is being executed, the local variables defined inside this module are active in memory. When the module is done the variables are removed automatically from memory. That is why a local variable is not visible to the other modules. The variables that exercise this temporary duration are classified as automatic variables. All local variables are automatic by default, but you can also use the word auto to declare a variable such as:
auto int score; is same as int score;
static the duration of a local variable is changed if it is declared as static. In this case it will retain its value between the function calls. This is an example of the declaration:
static int result:
Difference Between Auto and Static: -
The following program contains two variables a and b in the function kount ( ). The first is an int (auto by default) while the second is static. Both variables are incremented by 1 with every call of the function. Take a look at the execution of the program and notice the value of the static variable b which builds up with every call. In contrast, the auto variable a never exceeds the initial value1. this attribute is useful if you want to count the number of times a certain function is called.
# include <stdio.h>
# include <conio.h>
void kount (void);
void main ( )
{
  int i = 1;
while (i< = 4){
kount ( );
i++;
}
  getch ( );
}
void kount (void)
{
  int a = 1;
static int b = 1; /*variable initialization*/
printf (“\n automatic \ “a \” = %d while static \ “b\” = %d”, a, b);
a++;
b++;
}
The output is:
automatic “a” = 1 while static “b” = 1
automatic “a” = 1 while static “b” = 2
automatic “a” = 1 while static “b” = 3
automatic “a” = 1 while static “b” = 4
notice that initializing a static variable is not like assignment. Any assignment to the variable b will override the retained value from the previous call.
Register variable: -
These variables are accessed faster than any other class of variables because they are stored in the registers of the microprocessor, but not in the memory. You may need to declare a register variable if it is accessed repeatedly, as loop counters are.
The declaration looks like this example:
register int counter;
The number of register variables, however is limited by the number of registers. If you exceeds this limit, the variables will usually go to the default auto class.
Also, because a register is only two bytes long. You can only use this class for char and int types.
External variables: -
The C program modules may be contained in different files, with every file containing one or more functions. The scope of a global variable extends to all the files of the program, but you have to re-declare it in the other files as an external variable. This will tell the compiler that the variable has been declared before in another file.
The declaration is made by using the keyword extern in the form:
extern int ssn;
In a multiple file program you can also declare a global variable as static. This declaration will limit the scope of the variable to its own file.