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

enums
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Enums

CSCI 112: Programming in C

slide-2
SLIDE 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?
slide-3
SLIDE 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
slide-4
SLIDE 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;

slide-5
SLIDE 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?
slide-6
SLIDE 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
  • NORTH, SOUTH, EAST, WEST correspond to 0, 1, 2, 3

enum directions { NORTH, SOUTH, EAST, WEST }; enum directions my_direction = WEST; int x = (my_direction== 3); // This expression is TRUE (x is 1)

slide-7
SLIDE 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”

slide-8
SLIDE 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

slide-9
SLIDE 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;

slide-10
SLIDE 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
  • r function
  • typedef can be used to alias any C type, not just enums:
  • This snippet allows us to use “fred_t” instead of int

typedef int fred_t; fred_t x = 345;

slide-11
SLIDE 11

Ok, what can we do with enums?

  • Let’s use an enum to represent days of the week.
  • Because an enum is really an int under the hood, we can do a lot of

familiar operations with them

typedef enum { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } day_t;

slide-12
SLIDE 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; ... }

slide-13
SLIDE 13

Ok, what can we do with enums?

  • Arithmetic
  • 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)

direction_t direction = EAST; direction_t toLeft = --direction; // Returns NORTH direction_t toRight = ++direction; // Returns SOUTH

slide-14
SLIDE 14

Example program

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