Untitled Document

Storage Classes And User Defined Data types


Scope and life time of variable in functions

Storage class provides the information about location and visibility of a variable. Thus a variable in C along with the data type also has the storage class. The visibility of a variable means the portion of the program within which the variables are available.

Example:

int m;
main ( )
{
int i;
m = 60;
disp ( );
}
disp( )
{
int n;
:
:
m = 50;
:
:
}

In the above example the variable m, this has been declared before the main and it is called as Global variable.

It can be used in all the functions in the program. It need not be declared in other functions. A global variable is also known as an external variable. In the above example the variables i and n declared within main() and disp() respectively and are known as local variable because they are declared within a function. Local variables are visible and meaningful only inside the functions in which they are declared. They are not known to other functions.

C provides a variety of storage class specifiers that can be used to declare explicitly the scope and life time of variables. The concepts of scope and life time are important in multifunction and multiple file programs. Following are the different storage classes that exist in C.

  1. Automatic variables
  2. Static variables
  3. Register variables
  4. External variables

  • Automatic Variables: These variables are declared inside a function in which they are to be utilized. They are created when the function is entered. Hence the name automatic. Thus automatic variables are local to the function in which they are declared. Thus, they are referred to as local or internal variables. An automatic variable does not retain its value once control is transferred out of its defining function. Therefore any value assigned to automatic variable within a function will be lost once the function is exited. These variables are destroyed automatically when the function is exited.

  • Example: auto int n;

  • Static variables: The value of the static variable is retained until the end of the program. A variable can be declared static using the keyword static.
  • Example: static int m;

    The static variables which are declared inside a function is called as internal static variable. The static variables can be external also. The scope of internal static variables extends up to the end of the function in which they are defined. Therefore internal static variables are similar to auto variables, except that they remain in existence throughout the remainder of the program. Thus even between the function calls the internal static variables retain their values. For example it can be used to count the number of calls made to a function as shown in the following examples.
    A static variable is initialized only once when the program is compiled. It is never initialized again.

    #include <stdio.h>
    #include <conio.h>
    main( )
    {
    int i;
    void disp();
    clrscr();
    for (i=0; i<5; i++)
    {
    disp();
    }
    getch();
    }
    void disp()
    {
    static int counter = 0;
    printf("This is BCA Class \n");
    counter = counter + 1;
    printf ("Counter is %d \n", counter);
    }

    Output:



    From the output of the example you can observe that value of the variable counter is retained and it is incremented each time the function is visited.

  • Register variables: When we declare a variable as a register variable the CPU's registers are assigned instead of memory location. Since register access is much faster than the memory access, keeping the frequently used variables in the register will lead to the faster execution of program. Since only few variables can be placed in the register it is important to carefully select the variables for this purpose. However C will automatically convert register variables to non-register variables once the limit is reached.
  • {
    register int i;
    :
    :
    }
  • External variable: External variable are not confined to a single function. Their scope extends from the point of definition to the remainder of the program. That means they often span an entire program and they are also known as global variables. The global variables are initialized to 0 by default. Once a variable is declared as global, any function can use it and change its value. Then subsequent function can reference only that new value. Because of this property we can use global variables for variables shared between functions when it is inconvenient to pass them as parameters.

  • Note: Global variable is visible only from the point of declaration to the end of the program.

    Example

    main( )
    {
    extern int m;
    void disp();
    :
    }
    void disp( )
    {
    extern int m;
    :
    }
    int m;

    As shown in the example although the variable m has been defined after both the functions. The external declaration of m inside the function informs the compiler that it is an integer type defined some where else in the program. The extern declaration does not allocate memory space for variables. An extern within function provides the type of information to just that function. In case of arrays the definition should include their size as well.

    User defined data types:

    typedef: This feature allows user to define new data type that are equivalent to existing data types. Once a user-defined data type has been established then new variables, arrays, structures and so on can be declared in terms of this new data type.

    Syntax:
    typedef data type newtype;

    Where datatype refers to an existing data type either a standard data type or a previous user-defined data type and newtype refers to the new user-defined data type. However the new data type will be new in name only. In reality this new data type will not be fundamentally different from one of the standard data type.

    Example:

    typedef int empno;
    empno a;
    typedef int age;
    age male, female;

    In the examples empno and age are the user-defined data type equivalent to type int.
    So, the variable declaration empno a; is equivalent to writing int a; and age male, female; is equivalent to writing

    int male, female;
    typedef float height [100];
    height men, women;

    In the examples, new data type height is defined as 100-element floating-point array. Hence men and women are 100-element floating-point array.
    typedef feature is particularly convenient when defining structures, since it eliminates the need to repeatedly write the struct tag.

    typedef struct
    {
    member 1;
    :
    :
    member n;
    }
    new type;

    Example:

    typedef struct
    {
    int empno;
    char emphame [25];
    } employee;
    employee a, b;

    New type is the user-defined structure type. The structure variables can be defined in terms of new datatype. Hence employee is the new user defined data type. So, a and b are the structure variables of type employee.

    Enumerate Data Type:

    This is a user-defined data type, which can be used to declare variables that can have any one of the values enclosed within the flower brackets. These values are known as enumeration constants. Once this definition is made, variables of this enumerate type can be declared.

    Syntax:

    enum identifier {value1, value2,....valuen};
    enum identifier variable1, variable2....variablen;

    The declared enumerated variables can have any one of the values defined inside the flower brackets.

    Example:

    enum day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
    enum day weekbegin, weekend;

    In the above example weekbegin and weekend can have any one of the values i.e.it can have Monday or Tuesday and so on.
    Beginning with integer value zero all the enumeration constants are assigned values by the compiler automatically.
    So in the above example Monday is assigned with the value zero, Tuesday gets the value 1 and so on. Thus Sunday gets the value as 6. But automatic assignment can be overridden by assigning explicit values to the enumeration constants i.e. Monday can have value 5. So Tuesday will take value 6, Wednesday will take value 7 etc.

    Example:

    #include <stdio.h>
    #include <conio.h>
    void main( )
    {
    int weekday1, weekday2;
    enum day {Monday,Tuesday,Wednesday,Thursday,Friday, Saturday,Sunday};
    enum day weekbegin, weekend, weekmiddle;
    printf("Enter 0 for Monday\n");
    scanf("%d",&weekday1);
    printf("Enter 3 for Thursday\n");
    scanf("%d",&weekday2);
    weekbegin=weekday1;
    weekmiddle=weekday2;
    weekend=Sunday;
    if(weekbegin==Monday)
    printf("Week has begun\n");
    if(weekmiddle==Thursday)
    printf("Middle of the week\n");
    if(weekend==Sunday)
    printf("Enjoy the Sunday the week end\n");
    getch( );
    }

    Output:



    C Preprocessor<< Previous
    Next >> C Decision Making & Branching

    Our aim is to provide information to the knowledge seekers. 


    Support us generously

    comments powered by Disqus








    Footer1