22
Arpana Arpana Shree.A Shree.A By 1 Enumerated Data Types

Enumerated data types in C

Embed Size (px)

DESCRIPTION

Enumerated data types in C

Citation preview

Page 1: Enumerated data types in C

Arpana Shree.AArpana Shree.A

By1Enumerated Data Types

Page 2: Enumerated data types in C

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

Page 3: Enumerated data types in C

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

Page 4: Enumerated data types in C

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

Page 5: Enumerated data types in C

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

Page 6: Enumerated data types in C

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

Page 7: Enumerated data types in C

Another Example:

enum Weekdays{ Monday = 1, Tuesday, Wednesday, Thursday,Friday };

EXAMPLE

enum Weekdays{ Monday = 1, Tuesday, Wednesday=6, Thursday,Friday };

EXAMPLE

7Enumerated Data Types

Page 8: Enumerated data types in C

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

Page 9: Enumerated data types in C

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

Page 10: Enumerated data types in C

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

Page 11: Enumerated data types in C

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

Page 12: Enumerated data types in C

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

Page 13: Enumerated data types in C

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

Page 14: Enumerated data types in C

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

Page 15: Enumerated data types in C

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

Page 16: Enumerated data types in C

• 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

Page 17: Enumerated data types in C

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

Page 18: Enumerated data types in C

-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

Page 19: Enumerated data types in C

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

Page 20: Enumerated data types in C

Example

struct employee

{

unsigned int age: 7;

unsigned int sex: 1;

unsigned int marital_stat: 1;

unsigned int children: 4;

}emp;

20Enumerated Data Types

Page 21: Enumerated data types in C

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

Page 22: Enumerated data types in C

22Enumerated Data Types