20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The #def<strong>in</strong>e Statement<br />

303<br />

Program 13.2<br />

Cont<strong>in</strong>ued<br />

<strong>in</strong>t ma<strong>in</strong> (void)<br />

{<br />

double area (double r), circumference (double r),<br />

volume (double r);<br />

pr<strong>in</strong>tf ("radius = 1: %.4f %.4f %.4f\n",<br />

area(1.0), circumference(1.0), volume(1.0));<br />

pr<strong>in</strong>tf ("radius = 4.98: %.4f %.4f %.4f\n",<br />

area(4.98), circumference(4.98), volume(4.98));<br />

}<br />

return 0;<br />

Program 13.2 Output<br />

radius = 1: 3.1416 6.2832 4.1888<br />

radius = 4.98: 77.9128 31.2903 517.3403<br />

The symbolic name PI is def<strong>in</strong>ed as the value 3.141592654 at the beg<strong>in</strong>n<strong>in</strong>g of the program.<br />

Subsequent use of the name PI <strong>in</strong>side the area, circumference, and volume functions<br />

has the effect of caus<strong>in</strong>g its def<strong>in</strong>ed value to be automatically substituted at the<br />

appropriate po<strong>in</strong>t.<br />

Assignment of a constant to a symbolic name frees you from hav<strong>in</strong>g to remember the<br />

particular constant value every time you want to use it <strong>in</strong> a program. Furthermore, if you<br />

ever need to change the value of the constant (if, perhaps, you f<strong>in</strong>d out that you are<br />

us<strong>in</strong>g the wrong value, for example), you only have to change the value <strong>in</strong> one place <strong>in</strong><br />

the program: <strong>in</strong> the #def<strong>in</strong>e statement.Without this approach, you would have to otherwise<br />

search throughout the program and explicitly change the value of the constant<br />

whenever it was used.<br />

You might have realized that all the def<strong>in</strong>es you have seen so far (YES, NO, NULL, and<br />

PI) have been written <strong>in</strong> capital letters.The reason this is done is to visually dist<strong>in</strong>guish a<br />

def<strong>in</strong>ed value from a variable. Some programmers adopt the convention that all def<strong>in</strong>ed<br />

names be capitalized, so that it becomes easy to determ<strong>in</strong>e when a name represents a<br />

variable and when it represents a def<strong>in</strong>ed name. Another common convention is to prefix<br />

the def<strong>in</strong>e with the letter k. In that case, the follow<strong>in</strong>g characters of the name are not<br />

capitalized. kMaximumValues and kSignificantDigits are two examples of def<strong>in</strong>ed<br />

names that adhere to this convention.<br />

Program Extendability<br />

Us<strong>in</strong>g a def<strong>in</strong>ed name for a constant value helps to make programs more readily extendable.<br />

For example, when you def<strong>in</strong>e an array, you must specify the number of elements <strong>in</strong>

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

Saved successfully!

Ooh no, something went wrong!