Storage Classes And User Defined Data types
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.
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.
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.
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.
Note: Global variable is visible only from the point of declaration to the end of the program.
Example
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.
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
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.
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.
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:
The declared enumerated variables can have any one of the values defined inside the flower brackets.
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
#include
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( );
}
C Preprocessor<< Previous
Next >> C Decision Making & Branching
Our aim is to provide information to the knowledge seekers.