2. What is Enumerated DataWhat 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
3. Enumerated typeEnumerated type is a data type whose list ofis 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
4. 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
5. 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
6. enum
BOOLEAN
{TRUE,FALSE
}
b1,b2;
EXAMPLE
In 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
7. Another Example:
enum Weekdays
{ Monday = 1,
Tuesday,
Wednesday,
Thursday,
Friday
};
EXAMPLE
enum Weekdays
{ Monday = 1,
Tuesday,
Wednesday=6,
Thursday,
Friday
};
EXAMPLE
7Enumerated Data Types
8. 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();
}
0
1
2
3
4
Output
8Enumerated Data Types
9. 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
error
Error: NUMERIC
CONSTANTS TOO LARGE
9Enumerated Data Types
10. 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
13. Type-definition
The type definition statement is used to allow user defined
data types to be defined
using other already available data types.
Typedef
existing_data_type
new_user_define_
data_type;
SYNTAX
Typedef
int
Integer
EXAMPLE
13Enumerated Data Types
14. 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
15. 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
16. • 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
17. 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
18. -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
19. 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
20. Example
struct employee
{
unsigned int age: 7;
unsigned int sex: 1;
unsigned int marital_stat: 1;
unsigned int children: 4;
}emp;
20Enumerated Data Types
21. 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