20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

324 Chapter 14 More on Data Types<br />

Program 14.1<br />

Output<br />

Enter month number: 5<br />

Number of days is 31<br />

Program 14.1<br />

Output (Rerun)<br />

Enter month number: 2<br />

Number of days is 28<br />

...or 29 if it’s a leap year<br />

Enumeration identifiers can share the same value. For example, <strong>in</strong><br />

enum switch { no=0, off=0, yes=1, on=1 };<br />

assign<strong>in</strong>g either the value no or off to an enum switch variable assigns it the value 0;<br />

assign<strong>in</strong>g either yes or on assigns it the value 1.<br />

Explicitly assign<strong>in</strong>g an <strong>in</strong>teger value to an enumerated data type variable can be done<br />

with the type cast operator. So if monthValue is an <strong>in</strong>teger variable that has the value 6,<br />

for example, the expression<br />

thisMonth = (enum month) (monthValue - 1);<br />

is permissible and assigns the value 5 to thisMonth.<br />

When writ<strong>in</strong>g programs with enumerated data types, try not to rely on the fact that<br />

the enumerated values are treated as <strong>in</strong>tegers. Instead, try to treat them as dist<strong>in</strong>ct data<br />

types.The enumerated data type gives you a way to associate a symbolic name with an<br />

<strong>in</strong>teger number. If you subsequently need to change the value of that number, you must<br />

change it only <strong>in</strong> the place where the enumeration is def<strong>in</strong>ed. If you make assumptions<br />

based on the actual value of the enumerated data type, you defeat this benefit of us<strong>in</strong>g an<br />

enumeration.<br />

The variations permitted when def<strong>in</strong><strong>in</strong>g an enumerated data type are similar to those<br />

permitted with structure def<strong>in</strong>itions:The name of the data type can be omitted, and<br />

variables can be declared to be of the particular enumerated data type when the type is<br />

def<strong>in</strong>ed. As an example show<strong>in</strong>g both of these options, the statement<br />

enum { east, west, south, north } direction;<br />

def<strong>in</strong>es an (unnamed) enumerated data type with values east, west, south, or north,<br />

and declares a variable direction to be of that type.<br />

Enumerated type def<strong>in</strong>itions behave like structure and variable def<strong>in</strong>itions as far as<br />

their scope is concerned: Def<strong>in</strong><strong>in</strong>g an enumerated data type with<strong>in</strong> a block limits the<br />

scope of that def<strong>in</strong>ition to the block. On the other hand, def<strong>in</strong><strong>in</strong>g an enumerated data<br />

type at the beg<strong>in</strong>n<strong>in</strong>g of the program, outside of any function, makes the def<strong>in</strong>ition<br />

global to the file.<br />

When def<strong>in</strong><strong>in</strong>g an enumerated data type, you must make certa<strong>in</strong> that the enumeration<br />

identifiers are unique with respect to other variable names and enumeration identifiers<br />

def<strong>in</strong>ed with<strong>in</strong> the same scope.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!