Arpana Arpana Shree.A Shree.A By 1 Enumerated Data Types
May 13, 2015
Arpana Shree.AArpana Shree.A
By1Enumerated Data Types
What is Enumerated Data What is Enumerated Data Type???Type???
Enumerated Data type gives you an opportunity to
Invent your own data type
Define what values of the variables of this data type can take
The main purpose of he Enumerated data types is to allow numbers to be replaced by words
2Enumerated Data Types
Enumerated typeEnumerated type is a data type whose list of is a data type whose list of values is specified by the programmer.values is specified by the programmer.
enum enum_name
{identifier_list}
enum_type;
enum BOOLEAN{TRUE,FALSE}b1,b2;
SYNTAX EXAMPLE
3Enumerated Data Types
Another Way of having Syntax
enum enum_name
{identifier_list}
enum_type;
SYNTAX
enum BOOLEAN{TRUE=0, FALSE=1}b1,b2;
EXAMPLE
4Enumerated Data Types
Note:
If no integer values are specified then the left most word has integer value 0 and each one after that is incremented by one from that point. (0, 1, 2, 3, etc…) This also means that the left-most word is generally the smallest and the right-most word is generally the largest.
5Enumerated Data Types
enum BOOLEAN{TRUE,FALSE}b1,b2;
EXAMPLEIn the example the User defined data type BOOLEAN has been defined
The new data type has two values. TRUE and FALSE
The word TRUE has an Integer value 0
The word FALSE has an Integer value 1
6Enumerated Data Types
Another Example:
enum Weekdays{ Monday = 1, Tuesday, Wednesday, Thursday,Friday };
EXAMPLE
enum Weekdays{ Monday = 1, Tuesday, Wednesday=6, Thursday,Friday };
EXAMPLE
7Enumerated Data Types
Guess the output:
Void main()
{
enum WeekDays{Mon,tue,wed thurs,fri,sat,sun}days;
int i;
for(i=Mon;i<=fri;i++)
{
printf(“\n %d”,i);
}
getch();
}
01234
Output
8Enumerated Data Types
Now what happens!!!!
Void main()
{
enum WeekDays{Mon=32767,tue,wed thurs,fri,sat,sun}days;
int i;
for(i=Mon;i<=sun;i++)
{
printf(“\n %d”,i);
}
getch();
}
What will be the
OUTPUT????
Complier throws an
errorError: NUMERIC
CONSTANTS TOO LARGE
9Enumerated Data Types
Guess What Happens!!!!
Void main()
{
enum WeekDays{Mon=32762,tue,wed thurs,fri,sat,sun}days;
int i;
for(i=Mon;i<=sun;i++)
{
printf(“\n %d”,i);
}
getch();
}
What will be the
OUTPUT????
Does Complier throw an
error?Error: NUMERIC
CONSTANTS TOO LARGE
10Enumerated Data Types
Void main()
{
enum WeekDays{Mon=32760,tue,wed thurs,fri,sat,sun}days;
int i;
for(i=Mon;i<=sun;i++)
{
printf(“\n %d”,i);
}
getch();
}
What will be the
OUTPUT????3276032760327613276132572325723276332763327643276432765327653276632766
11Enumerated Data Types
Void main()
{
enum WeekDays{Mon=-1,tue,wed thurs,fri,sat,sun}days;
int i;
for(i=Mon;i<=sun;i++)
{
printf(“\n %d”,i);
}
getch();
}
What will be the
OUTPUT????-1-1001122334455
12Enumerated Data Types
Type-definition
The type definition statement is used to allow user defined data types to be definedusing other already available data types.
Typedef existing_data_type new_user_define_data_type;
SYNTAX
Typedef
int
Integer
EXAMPLE
13Enumerated Data Types
The example above simply creates an alternative data
type name
• Integer for the built in data type called “int”.
Typedef
int
Integer
EXAMPLE
14Enumerated Data Types
How Type def is used in structure
typedef struct
{
char names[10];
int age;
int marks;
} WEIGHT; WEIGHT a1,a2,a3;
struct stud
{
char names[10];
int age;
int marks;
}; Typedef struct stud STD
STD s1,s2,s3 15Enumerated Data Types
• In the above example STD is a new name for structure, Now every time it is not required to use Struct Stud for creating structure variable
16Enumerated Data Types
Bit Fields
While declaring a integer data type members within a structure, its size will be 16 bits. There are occasions where data requires much less than 16 bits space. We can overcome the wastage of the space by specifying the bit length
The Bit length specifies the number of bits that should be allotted to a member
17Enumerated Data Types
-Continued Bit field
Member of a structure whose size (in bits) has been specified
Enable better memory utilization
Must be declared as int or unsigned
Cannot access individual bits
Same address to the memory
18Enumerated Data Types
Declaration Syntax
struct <tagname>
{
datatype name1:bit-length;
datatype name2:bit-length;
.
.
};
Follow unsigned or int member with a colon (:) and an integer constant representing the width of the field
NOTE
19Enumerated Data Types
Example
struct employee
{
unsigned int age: 7;
unsigned int sex: 1;
unsigned int marital_stat: 1;
unsigned int children: 4;
}emp;
20Enumerated Data Types
Bit Field Bit length Range
Age 7 27 - 1(0 to 127)
Sex 1 0 or 1
Marital_stat 1 0 or 1
children 4 24 – 1(0 to 15)
21Enumerated Data Types
22Enumerated Data Types