Sunday, 27 March 2011

User Defined Data Types


Enumerations (enum)

Enumerations create new data types to contain something different that is not limited to the values fundamental
data types may take. Its form is the following: 

Syntax:
enum identifier{data1,data2,data3,..};

enum is ” Enumerated Data Type “.
enum is user defined data type
In the above example “identifier” is nothing but the user defined data type.

data1,data2,data3….. etc creates one set of enum values.

Using “identifier” we are creating our variables.


enum enumeration_name {
  value1,
  value2,
  value3,
 . 
 . 
} object_names;


enum month {JAN,FEB,MAR,APR};
enum month m;

First Line Creates “User Defined Data Type” called month.
It has 12 values as given in the pair of braces.
In the second line “m” variable is declared of type “month” which can be initialized with any “data value amongst 4 values”.
m = FEB;
Default Numeric value assigned to first enum value is “0”.
Numerically JAN is given value “0”.
FEB is given value “1”.
MAR is given value “2”.
APR is given value “3”.

printf("%d",m);
It will Print “1” on the screen because “Numerical Equivalent” of  “FEB” is 1 and “rmonth” is initialized by “FEB”.


Generally Printing Value of enum variable is as good as printing “Integer“.


You may like the following posts:

No comments:

Post a Comment