Previous Table of Contents Next


Finally, the overall formatting character determines the type of conversion performed and (subject to the type size modifiers) the expected type of the pointed-to argument that will receive the result:

c One character or, if a field width is present, that number of characters, is read and stored in the character or character array pointed to by the corresponding char * argument. No leading whitespace is consumed. No \0 character is added to the array. The array is quietly assumed to be big enough. (Under NA1, the size modifier l indicates a wchar_t * argument.)
d A decimal number is read and stored in the integer pointed to by the corresponding int * (or, with the h or l modifiers, short or long int *) argument.
e, E, f, g, G A decimal fraction or scientific-notation number (all five format characters accept either style of input) is read and stored in the floating-point value pointed to by the corresponding argument. The corresponding argument is of type float * by default, or double * if the l modifier is present, or long double * if the L modifier is present. (Notice the discrepancy with respect to printf, which uses %f for both types float and double.)
i A number is read and stored in the integer pointed to by the corresponding int * (or, with the h or l modifiers, short or long int *) argument. The number is converted as if by the strtol function (see section 3.10.4.2) with a base of 0; that is, it is converted from hexadecimal if it begins with 0x or 0X; otherwise from octal if it begins with 0; otherwise from decimal.
n No whitespace is consumed; no input conversion is performed at all. The number of characters read so far is stored in the integer pointed to by the corresponding int * argument.
o An octal number is read and stored in the integer pointed to by the corresponding unsigned int * (or, with the h or l modifiers, unsigned short or unsigned long int *) argument.
p An implementation-defined pointer representation (corresponding to the one printed by printf, et al., using %p) is read and stored in the pointer pointed to by the corresponding void ** argument.
s A string of nonwhitespace characters is read and stored (along with a terminating \0) in the character array pointed to by the corresponding char * argument. The array is quietly assumed to be big enough. (Under NA1, the size modifier l indicates a wchar_t * argument.)
u An unsigned decimal number is read and stored in the integer pointed to by the corresponding unsigned int * (or, with the h or l modifiers, unsigned short or unsigned long int *) argument.
x, X A hexadecimal number is read and stored in the integer pointed to by the corresponding unsigned int * (or, with the h or l modifiers, unsigned short or unsigned long int *) argument.
[ The complete format specifier (less the optional * flag) is %[…], with a character class or range contained between the brackets. The character class is a string of characters that is to be matched or, if a leading ^ is present, not matched. If the implementation permits it, a notation of the form a-c represents all characters in the range a to c. No leading whitespace is consumed. Characters are read from the input as long as they match the requested character class, or as long as they do not match the character class if the leading ^ is present, or until the input field width is exhausted. Characters are stored (along with a terminating \0) in the character array pointed to by the corresponding char * argument. The array is quietly assumed to be big enough. (Under NA1, the size modifier l indicates a wchar_t * argument.)
For example: the specifier %[abc] would read a string consisting only of a’s, b’s, and c’s; the specifier %[^)] would read all characters (including whitespace) up to but not including a close parenthesis.
% A single % character is expected in the input. No conversion is performed; no corresponding argument is expected.

scanf and fscanf return the number of conversions and assignments successfully performed (excluding those performed by %n or suppressed by the * flag). If a conversion fails or an unexpected character appears, scanning is terminated and unused characters are left on the input. If end-of-file is reached before performing any conversions, EOF is returned.

scanf is superficially powerful but inherently dangerous. It can be difficult or impossible to resynchronize in case of unexpected or erroneous input (because unmatched input is left on the input stream), and the conversion specifiers %c, %s, and %[ (unless accompanied by carefully chosen field width specifiers) can easily overflow their destination arrays. To overcome the first difficulty, it is often a good idea to read entire lines with fgets (see section 3.10.1.2) and then parse them using sscanf (see section 3.10.1.6).


Previous Table of Contents Next