Programming in C

(Barry) #1
Formatted I/O: printfand scanf 353

The fourth printfcall first uses the +flag to force a sign to appear, even if the value
is positive (normally, no sign is displayed).Then, the space modifier is used to force a
leading space in front of a positive value. (Sometimes this is useful for aligning data that
might be positive or negative; the positive values have a leading space; the negative ones
have a minus sign.) Next,%07is used to display the value of iright-justified within a
field width of seven characters.The 0 flag specifies zero fill.Therefore, four leading zeroes
are placed in front of the value of i, which is 425 .The final conversion in this call,%.7i
is used to display the value of iusing a minimum of seven digits.The net effect is the
same as specifying %07i:Four leading zeroes are displayed, followed by the three-digit
number 425.
The fifth printfcall displays the value of the short intvariable jin various for-
mats. Any integer format can be specified to display the value of a short int.
The next printfcall shows what happens when %iis used to display the value of an
unsigned int. Because the value assigned to uis larger than the maximum positive
value that can be stored in a signed inton the machine on which this program was
run, it is displayed as a negative number when the %iformat characters are used.
The next to last printfcall in this set shows how the lmodifier is used to display
longintegers, and the final printfcall in the set shows how long longintegers can be
displayed.
The second set of output illustrates various formatting possibilities for displaying
floats and doubles.The first output line of this set shows the result of displaying a
floatvalue using %f,%e,and %gformats. As mentioned, unless specified otherwise, the
%fand %eformats default to six decimal places.With the %gformat,printfdecides
whether to display the value in either %eor %fformat, depending upon the magnitude
of the value and on the specified precision. If the exponent is less than –4 or greater
than the optionally specified precision (remember, the default is 6),%eis used; otherwise,
%fis used. In either case, trailing zeroes are automatically removed, and a decimal point
is displayed only if nonzero digits follow it. In general,%gis the best format to use for
displaying floating-point numbers in the most aesthetically pleasing format.
In the next line of output, the precision modifier .2is specified to limit the display of
fto two decimal places. As you can see,printfis nice enough to automatically round
the value of ffor you.The line that immediately follows shows the use of the .0preci-
sion modifier to suppress the display of any decimal places, including the decimal point,
in the %fformat. Once again, the value of fis automatically rounded.
The modifiers 7.2, as used for generating the next line of output, specify that the
value is to be displayed in a minimum of seven columns, to two decimal places of accu-
racy. Because both values need fewer than seven columns to be displayed,printfright-
justifies the value (adding spaces on the left) within the specified field width.
In the next three lines of output, the value of the doublevariable dis displayed with
various formats.The same format characters are used for the display of floats and
doublevalues, because, as you’ll once again recall,floats are automatically converted to
doubles when passed as arguments to functions.The printfcall


printf ("%.*f\n", 3, d);

Free download pdf