Learn how to write the C program step-by-step | Lesson - 6

 Storage Classes

 C Program

 

TOPICS:

  • Meaning
  • Automatic Variables 

Learn how to write the C program
C Program

A variable that is recognized  only within a single  function is known as a "local" variable, while a variable that are recognized throughout the entire program are known as "global" variables. A local variable normally does not retain its value once control has been transferred out of its defining function. But, it may be required that to have certain local variables retain their values, so that the function can be re-entered later and the computation resumed.

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

  1. automatic     auto        auto int x, y, z;
  2. external        extern     extern float sq1, sq2;  
  3. static            static       static int counter = 0;
  4. 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

#include<stdio.h>
/* Calculate the factorial */
void main ()
      
{      auto int number;
        long int factorial(int number);
        printf("\nNumber = ");
        scanf("%d",&numb);
        printf("\nFactorial of %d = %1d\n",number, factorial(number));
        }

long int factorial(int number)      
{     auto int i;
       auto long int ans = 1;
       if(number > 1)
            for(i = 2; i <=number;++i)   
                     ans *= i;
                     return(ans);       
       }


  • 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

#include<stdio.h>
/* Calculate the average length of a line */
Void main( )
{       int n,count=0,total=0;
         float average;  
         int linecount( );    
         printf("\nEnter few lines below (Blank line to terminate)\n");
         while((n = linecount()) > 0)
        {        total += n;
                  ++counter;
                

    average = (float)total/counter;
    printf("Average number of characters per line : %5.2f\n", average);
    }   


int linecount( )
{         char line [80];
            int counter = 0;

            while(( line[counter] = getchar()) !='\n') 
                         ++counter;
            return(counter);
            }

 

  • 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.


 

If you find this article informative, please share the post to your friends. 
 
 
 

 
Exercise regularly. Eat a nutritious diet. Don't smoke - WHO (Covid-19).

 

 

Click here for other important blogs..

 
 

Thanks a lot for reading.
Neel Kamal
Learn how to write the C program step-by-step | Lesson - 6 Learn how to write the C program step-by-step | Lesson - 6 Reviewed by Neel Kamal on December 04, 2020 Rating: 5

No comments:

For More Details Subscribe & Comment..!

Powered by Blogger.