Chapter 2.4 - Discuss How Variables Are Used In A Program | Part - 3
DECLARATION OF STORAGE CLASS
Variables in C can have not only data type but also storage class that provides information about their location and visibility. The storage class decides the portion of the program within which the variables are recognized. Consider the following example:
/* Example of storage classes */
int m;
main()
{
int i;
float balance;
.....
.....
functions1();
}
function1()
{
int i;
float sum;
.....
.....
}
The variable m which has been declared before the main is called a global variable. It can be used in all the functions in the program. It need not be declared in the functions. A global variable is also known as an external variable.
C Numbers Pattern Programs With Easy Solutions
C Star Pattern Programs With Easy Solutions
C Numbers Pattern Programs With Easy Solutions
C Star Pattern Programs With Easy Solutions
The variables i, balance and sum are called local variables because they are declared insides a function. Local variables are visible and meaningful only inside the functions in which they are declared.
They are not known to other functions. Note that the variable i has been declared in both the functions. Any change in the value of i in one function does not affect its value in the other.
C provides a variety of storage class specifiers that can be used to declare explicitly the scope and lifetime of variables. The concepts of scope and lifetime are important only in multifunction and multiple file programs and therefore the storage classes are considered in detail later when functions are discussed. For now, remember that there are storage class specifiers (auto, register, static and extern) whose meanings are given in Table 2.10.
The storage class is another qualifier (like long or unsigned) that can be added to a variable declaration as shown below:
auto int count;
register char ch;
static int x;
extern long total ;
Static and external (extern) variables are automatically initialized to zero. Automatic (auto) variables contain undefined values (known as 'garbage') unless they are initialized explicitly.










