{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

LEC20081110

# LEC20081110 - Introduction to Computer Programming CSC180...

This preview shows pages 1–6. Sign up to view the full content.

Introduction to Computer Programming November 10, 2008 CSC180 Fall 2008, University of Toronto

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Enumeration constants and variables An enumeration is a list of constant integer values (enumerators). Enumeration variables and enumeration constants are integers. In default, the first enumeration constant has value 0, the next 1, and so on. Enumeration constant values can be specified explicitly; unspecified constant values is one greater than the previous constant. Subject to scope rules CSC180 Fall 2008, University of Toronto 1
Example: /* s and s2 are treated as integer variables */ enum {CLUBS, DIAMONDS, HEARTS, SPADES} s, s2; s = CLUBS; s2 = SPADES; printf("%d\n", s + s2); /* print 3 */ s++; /* OK */ s += 10; /* OK */ enum boolean {NO, YES} b; b = NO; /* b has value 0 */ enum escapes {BELL = ’\a’, BACKSPACE = ’\b’, TAB = ’\t’, NEWLINE = ’\n’, VTAB = ’\v’, RETURN = ’\r’}; enum escapes e; CSC180 Fall 2008, University of Toronto 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
e = TAB; typedef enum months {JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC} Months; Months m; m = FEB; /* m has value 2 */ printf("%d\n", AUG); /* print 8 */ typedef enum { CS_NONE, /* used for reporting errors, etc */ CS_ISO8859_1, CS_ISO8859_1_X11, /* X font encoding with VT100 glyphs */ CS_ISO8859_2, CS_ISO8859_3, CS_ISO8859_4 } charset_t; charset_t type = CS_ISO8859_4; CSC180 Fall 2008, University of Toronto 3
The advantages over macro definitions: (1) constant values can be generated automatically; (2) debugger may print values of enumeration variables in their symbolic form.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 19

LEC20081110 - Introduction to Computer Programming CSC180...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online