Programming in C

(Barry) #1

356 Chapter 16 Input and Output Operations in C


Ta b le 16.6 scanfConversion Characters
Character Action
d The value to be read is expressed in decimal notation; the corresponding argu-
ment is a pointer to an intunless the h,l, or llmodifier is used, in which
case the argument is a pointer to a short,long, or long long int,respec-
tively.
i Like %d, except numbers expressed in octal (leading 0 ) or hexadecimal (lead-
ing 0xor 0X) also can be read.
u The value to be read is an integer, and the corresponding argument is a point-
er to an unsigned int.
o The value to be read is expressed in octal notation and can be optionally pre-
ceded by a 0 .The corresponding argument is a pointer to an int, unless h,l,
or llprecedes the letter o, in which case the argument is a pointer to a
short,long, or long long,respectively.
x The value to be read is expressed in hexadecimal notation and can be option-
ally preceded by a leading 0xor 0X; the corresponding argument is a pointer
to an unsigned int, unless a h,l, or llmodifies the x.
a,e,f, or g The value to be read is expressed in floating-point notation; the value can be
optionally preceded by a sign and can optionally be expressed in exponential
notation (as in 3.45 e-3); the corresponding argument is a pointer to
float, unless an lor Lmodifier is used, in which case it is a pointer to a
doubleor to a long double,respectively.
c The value to be read is a single character; the next character that appears on
the input is read, even if it is a space, tab, newline, or form-feed character.The
corresponding argument is a pointer to char; an optional count before the c
specifies the number of characters to be read.
s The value to be read is a sequence of characters; the sequence begins with the
first nonwhitespace character and is terminated by the first whitespace charac-
ter.The corresponding argument is a pointer to a character array, which must
contain enough characters to contain the characters that are read plus the null
character that is automatically added to the end. If a number precedes the s,
the specified number of characters is read, unless a whitespace character is
encountered first.
[...] Characters enclosed within brackets indicate that a character string is to be
read, as in %s; the characters within the brackets indicate the permissible char-
acters in the string. If any character other than that specified in the brackets is
encountered, the string is terminated; the sense of how these characters are
treated can be “inverted” by placing a ^as the first character inside the brack-
ets. In such a case, the subsequent characters are taken to be the ones that will
terminate the string; that is, if any of the subsequent characters are found on
the input, the string is terminated.
Free download pdf