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.

16 Chapter 3 Compil<strong>in</strong>g and Runn<strong>in</strong>g Your First Program<br />

The pr<strong>in</strong>tf rout<strong>in</strong>e call <strong>in</strong> Program 3.4 now has two items or arguments enclosed<br />

with<strong>in</strong> the parentheses.These arguments are separated by a comma.The first argument to<br />

the pr<strong>in</strong>tf rout<strong>in</strong>e is always the character str<strong>in</strong>g to be displayed. However, along with<br />

the display of the character str<strong>in</strong>g, you might also frequently want to have the value of<br />

certa<strong>in</strong> program variables displayed. In this case, you want to have the value of the variable<br />

sum displayed at the term<strong>in</strong>al after the characters<br />

The sum of 50 and 25 is<br />

are displayed.The percent character <strong>in</strong>side the first argument is a special character recognized<br />

by the pr<strong>in</strong>tf function.The character that immediately follows the percent sign<br />

specifies what type of value is to be displayed at that po<strong>in</strong>t. In the preced<strong>in</strong>g program, the<br />

letter i is recognized by the pr<strong>in</strong>tf rout<strong>in</strong>e as signify<strong>in</strong>g that an <strong>in</strong>teger value is to be<br />

displayed. 2<br />

Whenever the pr<strong>in</strong>tf rout<strong>in</strong>e f<strong>in</strong>ds the %i characters <strong>in</strong>side a character str<strong>in</strong>g, it<br />

automatically displays the value of the next argument to the pr<strong>in</strong>tf rout<strong>in</strong>e. Because<br />

sum is the next argument to pr<strong>in</strong>tf, its value is automatically displayed after the characters<br />

“The sum of 50 and 25 is” are displayed.<br />

Now try to predict the output from Program 3.5.<br />

Program 3.5 Display<strong>in</strong>g Multiple Values<br />

#<strong>in</strong>clude <br />

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

{<br />

<strong>in</strong>t value1, value2, sum;<br />

value1 = 50;<br />

value2 = 25;<br />

sum = value1 + value2;<br />

pr<strong>in</strong>tf ("The sum of %i and %i is %i\n", value1, value2, sum);<br />

}<br />

return 0;<br />

Program 3.5 Output<br />

The sum of 50 and 25 is 75<br />

2. Note that pr<strong>in</strong>tf also allows you to specify %d format characters to display an <strong>in</strong>teger.This<br />

book consistently uses %i throughout the rema<strong>in</strong><strong>in</strong>g chapters.

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

Saved successfully!

Ooh no, something went wrong!