There are two different ways to characterize variables : by data type and by storage class. Data type refers to the to the type of information represented by a variable. Storage class refers to the permanence of a variable and its scope within the program, that is, the portion of the program over which the variable is recognized.
A variable name identifies some physical location within the computer, where the string of bits representing the variable's value is stored. There are basically two kinds of locations in a computer where such a value may be kept - memory or one of the CPU registers. It is a variable's storage class which determines whether it is to be stored in memory or in a register.
The storage class also specifies when the variable is active . The is, whether a variable is available as long as a program is running or only when a function is invoked. It also specifies the part of the program over which the variable is available.
There are four different storage-class in C. They are:
Keyword Example
- automatic auto auto int x, y, z;
- external extern extern float sq1, sq2;
- static static static int counter = 0;
- register register extern char text;
Automatic Variables
- This Variables are always declared within a function and local to the function in which they are declared; that is, their scope is confined to that function. Automatic variables defined in different functions will therefore be independent of one another, even though they may have the same name.
- Variables are not declared to be auto since it is the default storage class. Whenever a variable is declared inside a block of code and no explicit storage class is given, the variable is assumed to be automatic.
- Any variable declared within a function is interpreted as an automatic variable unless a different storage-class specification is included within the declaration. All the variables in the programming examples used in the earlier chapters have been automatic variables.
- The keyword auto is not required at the beginning of each variable declaration there is no harm in including an auto specification within a declaration if the programmer wishes, through this is normally not done.
Program: Calculate the factorial
Program-1
- Automatic variables can be assigned initial values by including appropriate expressions within the variable declarations, as in the above program, or by explicit assignment expression elsewhere in the function. Such values will be reassigned each time the function is re-entered. If an automatic variable is not initialized in some manner, however, its initial value will be unpredictable, and probably unintelligible.
- An automatic variable does not retain its value once control is transferred out of its defining function. So, any value assigned to an automatic variable within a function will be lost once the function is exited.
- If the program requires that an automatic variable be assigned a particular value each time the function is executed, that value will have to be reset whenever the function is accessed.
Program: Calculate the average length of a line
Program-1
average = (float)total/counter;
printf("Average number of characters per line : %5.2f\n", average);
}
int linecount( )
{ char line [80];
int counter = 0;
- Any variable declared within a function is private or local to that function alone. It is know only to that function. No other function is aware of the existence of the local variable of another function and existence of the local variable of another function and hence cannot have direct access to it. This is the reason why arguments have to be explicitly passed between functions, if they if they belong to the automatic storage class.
- The scope of auto variable is limited to the function in which they are declared, so it is possible to have variables of the same name in different functions. Because these variable are local to a function in which they are declared, their value cannot be changed accidentally by what happens in some other function.
- Thus, automatic variables do not retain their values from one function invocation to another. If the function block is reentered, the storage is reallocated to the variable. Hence the value of automatic variables must be explicitly set upon each entry. If they are not set, they will contain garbage values.
- Learn how to write the Turbo C program step-by-step | Lesson -1
- Learn how to write the Turbo C program step-by-step | Lesson -2
- Learn how to write the Turbo C program step-by-step | Lesson -3
- Learn how to write the Turbo C program step-by-step | Lesson -4
- Learn how to write the Turbo C program step-by-step | Lesson -5
Click here for other important blogs..
No comments:
For More Details Subscribe & Comment..!