Programming in C

(Barry) #1

354 Chapter 16 Input and Output Operations in C


specifies that the value of dis to be displayed to three decimal places.The asterisk after
the period in the format specification instructs printfto take the next argument to the
function as the value of the precision. In this case, the next argument is 3 .This value
could also have been specified by a variable, as in
printf ("%.*f\n", accuracy, d);
which makes this feature useful for dynamically changing the format of a display.
The final line of the floats and doubles set shows the result of using the format
characters %*.*ffor displaying the value of d.In this case, both the field width and the
precision are given as arguments to the function, as indicated by the two asterisks in the
format string. Because the first argument after the format string is 8, this is taken as the
field width.The next argument, 2, is taken as the precision.The value of dis, therefore,
displayed to two decimal places in a field size of eight characters. Notice that the minus
sign as well as the decimal point are included in the field-width count.This is true for
any field specifier.
In the next set of program output, the character c, which was initially set to the char-
acter X, is displayed in various formats.The first time it is displayed using the familiar %c
format characters. On the next line, it is displayed twice with a field-width specification
of 3.This results in the display of the character with two leading spaces.
A character can be displayed using any integer format specification. In the next line
of output, the value of cis displayed in hexadecimal.The output indicates that on this
machine the character Xis internally represented by the number hexadecimal 58.
In the final set of program output, the character string sis displayed.The first time it
is displayed with the normal %sformat characters.Then, a precision specification of 5 is
used to display just the first five characters from the string.This results in the display of
the first five letters of the alphabet.
In the third output line from this set, the entire character string is once again dis-
played, this time using a field-width specification of 30. As you can see, the string is dis-
played right-justified in the field.
The final two lines from this set show five characters from the string sbeing dis-
played in a field-width size of 20.The first time, these five characters are displayed right-
justified in the field.The second time, the minus sign results in the display of the first five
letters left-justified in the field.The vertical bar character was printed to verify that the
format characters %-20.5sactually result in the display of 20 characters at the terminal
(five letters followed by 15 spaces).
The %pcharacters are used to display the value of a pointer. Here, you are displaying
the integer pointer ipand the character pointer cp.You should note that you will proba-
bly get different values displayed on your system because your pointers will most likely
contain different addresses.
The format of the output when using %pis implementation-defined, but in this
example, the pointers are displayed in hexadecimal format. According to the output, the
pointer variable ipcontained the address bffffc20 hexadecimal, and the pointer cpcon-
tained the address bffffbf0.
Free download pdf