stu nicholls dot com | menu - Professional dropdown #3

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

    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
Untitled Document
Untitled Document
Footer1