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.

Work<strong>in</strong>g with Arithmetic Expressions<br />

33<br />

is evaluated accord<strong>in</strong>g to the rules stated previously as<br />

(a * b) + (c * d)<br />

or<br />

(100 * 2) + (25 * 4)<br />

The result of 300 is handed to the pr<strong>in</strong>tf rout<strong>in</strong>e.<br />

Integer Arithmetic and the Unary M<strong>in</strong>us Operator<br />

Program 4.3 re<strong>in</strong>forces what you just learned and <strong>in</strong>troduces the concept of <strong>in</strong>teger<br />

arithmetic.<br />

Program 4.3 More Examples with Arithmetic Operators<br />

// More arithmetic expressions<br />

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

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

{<br />

<strong>in</strong>t a = 25;<br />

<strong>in</strong>t b = 2;<br />

float c = 25.0;<br />

float d = 2.0;<br />

pr<strong>in</strong>tf ("6 + a / 5 * b = %i\n", 6 + a / 5 * b);<br />

pr<strong>in</strong>tf ("a / b * b = %i\n", a / b * b);<br />

pr<strong>in</strong>tf ("c / d * d = %f\n", c / d * d);<br />

pr<strong>in</strong>tf ("-a = %i\n", -a);<br />

}<br />

return 0;<br />

Program 4.3 Output<br />

6 + a / 5 * b = 16<br />

a / b * b = 24<br />

c / d * d = 25.000000<br />

-a = -25<br />

Extra blank spaces are <strong>in</strong>serted between <strong>in</strong>t and the declaration of a, b, c,and d <strong>in</strong> the<br />

first four statements to align the declaration of each variable.This helps make the program<br />

more readable.You also might have noticed <strong>in</strong> each program presented thus far that<br />

a blank space was placed around each operator.This, too, is not required and is done

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

Saved successfully!

Ooh no, something went wrong!