1 / 15

Enumerated data type

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

calla
Download Presentation

Enumerated data type

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enumerated data type C ProgrammingLecture 15

  2. 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

  3. 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

  4. 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

  5. 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.

  6. 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

  7. 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.

  8. 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;

  9. Initializing Enumerations • We can initialize enumerations by assigning an integer value to list members:enum colour{ RED = 1, BLUE = 5, GREEN = 8, WHITE = 15};

  10. 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};

  11. 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);

  12. 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.

  13. 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

  14. /* 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; }

  15. /* 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; }

More Related