enums
play

Enums CSCI 112: Programming in C Fitting C data types to the real - PowerPoint PPT Presentation

Enums CSCI 112: Programming in C Fitting C data types to the real world So far, weve seen int , double , float and char For a lot of real world data, these fit well! What sorts of things would be difficult to represent with these


  1. Enums CSCI 112: Programming in C

  2. Fitting C data types to the real world • So far, we’ve seen int , double , float and char • For a lot of real world data, these fit well! • What sorts of things would be difficult to represent with these types?

  3. Fitting C data types to the real world • So far, we’ve seen int , double , float and char • For a lot of real world data, these fit well! • What sorts of things would be difficult to represent with these types? • Cardinal directions • Left or right handedness • Types of beer (IPA, Amber, etc.) • Days of week

  4. Enumerated types • C gives us enumerated types ( enums) to represent items in a category—for example NORTH, SOUTH, EAST and WEST • Enums let associate a numeric value with a meaningful alphanumeric name enum directions { NORTH, // 0 SOUTH, // 1 EAST, // 2 WEST // 3 }; enum directions my_direction = WEST;

  5. Why?? • We could easily just say, ”We’ll call North 0, South 1, etc.” and store the value as an int • Why is an enum a better choice?

  6. How they work • In code, enums let us refer to things with an identifier (string name) • But to C, these different items in an enum are actually of type int enum directions { NORTH, SOUTH, EAST, WEST }; • NORTH, SOUTH, EAST, WEST correspond to 0, 1, 2, 3 enum directions my_direction = WEST; int x = (my_direction== 3); // This expression is TRUE (x is 1)

  7. Important to know… • You can only use an enum identifier once per scope! • For example, you can’t have NORTH as part of two enum definitions • You cannot have another variable with the same identifier as an enum identifier • Ex: We can’t declare int NORTH; since NORTH is an identifier in directions enum • An enum we declare represents a new type • enum directions my_direction; says that the variable “my_direction” is of type the enumerated type “directions”

  8. Important to know…still… • We can only assign values of int type to an enumerated type variable. • Either an actual int type, or an identifier from the enum • Ex: SOUTH and 2 are equivalent, and can both be assigned to directions • C does no checking to make sure the value you assign is specified in the enum you declared • We can assign 4356 to directions, even though it has no meaning since only 0 through 3 (NORTH through SOUTH) are specified

  9. Creating a named type from an enum • Instead of having to refer to our enumerated type as enum <name> , we can define a new C data type! • The typedef reserved word allows us to rename types typedef enum { NORTH, SOUTH, EAST, WEST } directions_t; directions_t my_direction = WEST;

  10. Creating a named type from an enum • The _t at the end of custom type names is a standard practice • It lets us immediately see that this is a type, as opposed to a variable or function • typedef can be used to alias any C type, not just enums: typedef int fred_t; • This snippet allows us to use “fred_t” instead of int fred_t x = 345;

  11. Ok, what can we do with enums? • Let’s use an enum to represent days of the week. typedef enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } day_t; • Because an enum is really an int under the hood, we can do a lot of familiar operations with them

  12. Ok, what can we do with enums? • Switch statement day_t day_of_week = Tuesday; switch( day_of_week ) { case Monday: // ugh, monday? really? break; case Tuesday: // meh break; ... }

  13. Ok, what can we do with enums? • Arithmetic direction_t direction = EAST; direction_t toLeft = --direction; // Returns NORTH direction_t toRight = ++direction; // Returns SOUTH • The order we declare our enum members in does matter for arithmetic • It should be “in order” for the type you’re storing • Ex: NORTH, EAST, SOUTH, WEST (not SOUTH, WEST, NORTH, EAST)

  14. Example program • Let’s write a program to do some gymnastics with days of the week

Recommend


More recommend