190 likes | 407 Views
Enumerated data type. C Programming Lecture 15. Topics. … how to work with enumerated data Definition of enumerated data – enums Use of enumerated types Syntax to define an enumerated data type Declare a variable of enumerated type Input/ output. Using typedef with enums
E N D
Enumerated data type C ProgrammingLecture 15
Topics … how to work with enumerated data • Definition of enumerated data – enums • Use of enumerated types • Syntax to define an enumerated data type • Declare a variable of enumerated type • Input/ output. • Using typedef with enums • Examples using enums
User Defined Data Types • C allows programmers the flexibility of defining their own data types. • We can use custom data types for building more complex data types that we might use in multifaceted applications. • Specifically, C supports: • Structures • Enumerations
The Enumerated Type • “The enumerated type is a user-defined type based on the standard integer type.” • We give each value in an enumerated type an identifier that we call an enumeration constant, which we can use as a symbolic name. • Use for validation • A list of values a variable of that type can have
Declaring Enumerated Types • Syntax:enum typeName{ //identifierListitem1, item 2, item3 }; • If we don’t assign values to the members of the identifier list, C will assign values, beginning at 0.
Example:enum color{ RED, BLUE, GREEN, WHITE}; In this example, C assigns the member RED the value of 0, BLUE the value 1 ..etc. Enumeration Example
Creating Variables from Enumerations • Once we’ve declared an enumeration, we can create variables from them. • We can use those variables anywhere in a program where we could also use integers.
Declaring Enumeration-Type Variables enum color x; enum color y; enum color z; Assigning values x = BLUE; y = WHITE; z = PURPLE; //Error - PURPLE not defined x = y; z = y;
Initializing Enumerations • We can initialize enumerations by assigning an integer value to list members:enum colour{ RED = 1, BLUE = 5, GREEN = 8, WHITE = 15};
Initializing Enumerations • If we want to assign sequential values, beginning with a specified value, we only need to specify the first value:enum months{ JAN = 1, FEB, MAR, … NOV DEC};
Input/Outputs with Enumerations • Remember that enumerations are derived integers. Thus, we must input/output as if they were integers. • Example:enum months month1;enum months month2;scanf(“%d %d”, &month1, &month2);printf(“%d %d”, month1, month2);
typedef • We can use the typedef statement to re-define one of C’s standard data types (int, float, etc.) or a data type that you created.
typedef Enumerations typedef enum colourList{ RED = 1, BLUE = 2, GREEN = 3, WHITE = 15}colour; Declare a variable colour shirt; //note no need for enum colour
/* Print colour of items. */ #include <stdio.h> int main () { typedefenumcolourList {red = 2, blue = 4, green = 5} colour; colour item; printf(“Enter item colour “); scanf(“%d”, &item); //enum are derived from integers switch (item) { case red: printf(“RED”); break; case blue: printf(“BLUE”); break; case green: printf(“GREEN”); break; } return 0; }
/* Print selected TV stations for our cable TV system. */ #include <stdio.h> int main () { enum TV {fox = 2, nbc = 4, cbs = 5, abc = 11, hbo = 15, show = 17, max = 31, espn = 39, cnn = 51}; printf("Here are my favourite cable stations:\n"); printf(" ABC: \t%2d\n", abc); printf(" CBS: \t%2d\n", cbs); printf(" CNN: \t%2d\n", cnn); printf(" ESPN:\t%2d\n", espn); printf(" Fox: \t%2d\n", fox); printf(" HBO: \t%2d\n", hbo); printf(" Max: \t%2d\n", max); printf(" NBC: \t%2d\n", nbc); printf(" Show:\t%2d\n", show); printf("End of my favourite stations. \n"); return 0; }