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.

Formatted I/O: pr<strong>in</strong>tf and scanf<br />

355<br />

The f<strong>in</strong>al set of output shows the use of the %n format characters. In this case, the<br />

correspond<strong>in</strong>g argument to pr<strong>in</strong>tf must be of type po<strong>in</strong>ter to <strong>in</strong>t, unless a type modifier<br />

of hh, h, l, ll, j, z, or t is specified. pr<strong>in</strong>tf actually stores the number of characters it<br />

has written so far <strong>in</strong>to the <strong>in</strong>teger po<strong>in</strong>ted to by this argument. So, the first occurrence<br />

of %n causes pr<strong>in</strong>tf to store the value 4 <strong>in</strong>side the <strong>in</strong>teger variable c1 because that’s how<br />

many characters have been written so far by this call.The second occurrence of %n causes<br />

the value 12 to be stored <strong>in</strong>side c2. This is because 12 characters had been displayed at<br />

that po<strong>in</strong>t by pr<strong>in</strong>tf. Notice that <strong>in</strong>clusion of the %n <strong>in</strong>side the format str<strong>in</strong>g has no<br />

effect on the actual output produced by pr<strong>in</strong>tf.<br />

The scanf Function<br />

Like the pr<strong>in</strong>tf function, many more formatt<strong>in</strong>g options can be specified <strong>in</strong>side the format<br />

str<strong>in</strong>g of a scanf call than have been illustrated up to this po<strong>in</strong>t. As with pr<strong>in</strong>tf,<br />

scanf takes optional modifiers between the % and the conversion character.These<br />

optional modifiers are summarized <strong>in</strong> Table 16.5.The possible conversion characters that<br />

can be specified are summarized <strong>in</strong> Table 16.6.<br />

When the scanf function searches the <strong>in</strong>put stream for a value to be read, it always<br />

bypasses any lead<strong>in</strong>g so-called whitespace characters, where whitespace refers to either a<br />

blank space, horizontal tab ('\t'), vertical tab ('\v'), carriage return ('\r'), newl<strong>in</strong>e<br />

('\n'), or form-feed character ('\f').The exceptions are <strong>in</strong> the case of the %c format<br />

characters—<strong>in</strong> which case, the next character from the <strong>in</strong>put, no matter what it is, is<br />

read—and <strong>in</strong> the case of the bracketed character str<strong>in</strong>g—<strong>in</strong> which case, the characters<br />

conta<strong>in</strong>ed <strong>in</strong> the brackets (or not conta<strong>in</strong>ed <strong>in</strong> the brackets) specify the permissible characters<br />

of the str<strong>in</strong>g.<br />

Table 16.5 scanf Conversion Modifiers<br />

Modifier Mean<strong>in</strong>g<br />

* Field is to be skipped and not assigned<br />

size<br />

Maximum size of the <strong>in</strong>put field<br />

hh<br />

Value is to be stored <strong>in</strong> a signed or unsigned char<br />

h<br />

Value is to be stored <strong>in</strong> a short <strong>in</strong>t<br />

l<br />

Value is to be stored <strong>in</strong> a long <strong>in</strong>t, double, or wchar_t<br />

j, z, or t Value is to be stored <strong>in</strong> a size_t (%j), ptrdiff_t (%z), <strong>in</strong>tmax_t,or<br />

u<strong>in</strong>tmax_t (%t)<br />

ll<br />

Value is to be stored <strong>in</strong> a long long <strong>in</strong>t<br />

L<br />

Value is to be stored <strong>in</strong> a long double<br />

type<br />

Conversion character

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

Saved successfully!

Ooh no, something went wrong!