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.

354 Chapter 16 Input and Output Operations <strong>in</strong> C<br />

specifies that the value of d is to be displayed to three decimal places.The asterisk after<br />

the period <strong>in</strong> the format specification <strong>in</strong>structs pr<strong>in</strong>tf to take the next argument to the<br />

function as the value of the precision. In this case, the next argument is 3.This value<br />

could also have been specified by a variable, as <strong>in</strong><br />

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

which makes this feature useful for dynamically chang<strong>in</strong>g the format of a display.<br />

The f<strong>in</strong>al l<strong>in</strong>e of the floats and doubles set shows the result of us<strong>in</strong>g the format<br />

characters %*.*f for display<strong>in</strong>g the value of d.In this case, both the field width and the<br />

precision are given as arguments to the function, as <strong>in</strong>dicated by the two asterisks <strong>in</strong> the<br />

format str<strong>in</strong>g. Because the first argument after the format str<strong>in</strong>g is 8, this is taken as the<br />

field width.The next argument, 2, is taken as the precision.The value of d is, therefore,<br />

displayed to two decimal places <strong>in</strong> a field size of eight characters. Notice that the m<strong>in</strong>us<br />

sign as well as the decimal po<strong>in</strong>t are <strong>in</strong>cluded <strong>in</strong> the field-width count.This is true for<br />

any field specifier.<br />

In the next set of program output, the character c, which was <strong>in</strong>itially set to the character<br />

X, is displayed <strong>in</strong> various formats.The first time it is displayed us<strong>in</strong>g the familiar %c<br />

format characters. On the next l<strong>in</strong>e, it is displayed twice with a field-width specification<br />

of 3.This results <strong>in</strong> the display of the character with two lead<strong>in</strong>g spaces.<br />

A character can be displayed us<strong>in</strong>g any <strong>in</strong>teger format specification. In the next l<strong>in</strong>e<br />

of output, the value of c is displayed <strong>in</strong> hexadecimal.The output <strong>in</strong>dicates that on this<br />

mach<strong>in</strong>e the character X is <strong>in</strong>ternally represented by the number hexadecimal 58.<br />

In the f<strong>in</strong>al set of program output, the character str<strong>in</strong>g s is displayed.The first time it<br />

is displayed with the normal %s format characters.Then, a precision specification of 5 is<br />

used to display just the first five characters from the str<strong>in</strong>g.This results <strong>in</strong> the display of<br />

the first five letters of the alphabet.<br />

In the third output l<strong>in</strong>e from this set, the entire character str<strong>in</strong>g is once aga<strong>in</strong> displayed,<br />

this time us<strong>in</strong>g a field-width specification of 30. As you can see, the str<strong>in</strong>g is displayed<br />

right-justified <strong>in</strong> the field.<br />

The f<strong>in</strong>al two l<strong>in</strong>es from this set show five characters from the str<strong>in</strong>g s be<strong>in</strong>g displayed<br />

<strong>in</strong> a field-width size of 20.The first time, these five characters are displayed rightjustified<br />

<strong>in</strong> the field.The second time, the m<strong>in</strong>us sign results <strong>in</strong> the display of the first five<br />

letters left-justified <strong>in</strong> the field.The vertical bar character was pr<strong>in</strong>ted to verify that the<br />

format characters %-20.5s actually result <strong>in</strong> the display of 20 characters at the term<strong>in</strong>al<br />

(five letters followed by 15 spaces).<br />

The %p characters are used to display the value of a po<strong>in</strong>ter. Here, you are display<strong>in</strong>g<br />

the <strong>in</strong>teger po<strong>in</strong>ter ip and the character po<strong>in</strong>ter cp.You should note that you will probably<br />

get different values displayed on your system because your po<strong>in</strong>ters will most likely<br />

conta<strong>in</strong> different addresses.<br />

The format of the output when us<strong>in</strong>g %p is implementation-def<strong>in</strong>ed, but <strong>in</strong> this<br />

example, the po<strong>in</strong>ters are displayed <strong>in</strong> hexadecimal format. Accord<strong>in</strong>g to the output, the<br />

po<strong>in</strong>ter variable ip conta<strong>in</strong>ed the address bffffc20 hexadecimal, and the po<strong>in</strong>ter cp conta<strong>in</strong>ed<br />

the address bffffbf0.

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

Saved successfully!

Ooh no, something went wrong!