ter | Type of Matching Argument | Auto-skip | Example | Sample Matching Input |
% | % (a literal, matched but not converted or assigned) | no | int anInt; | 23% |
d | int (See note) | yes | int anInt; long l; | -23 200 |
i | int (See note) | yes | int anInt; | 0x23 |
o | unsigned int (See note) | yes | unsigned int aUInt; | 023 |
u | unsigned int (See note) | yes | unsigned int aUInt; | 23 |
x | unsigned int (See note) | yes | unsigned int aUInt; | 1A |
a, e, f, g | float or double (See note) | yes | float f; double d; | 1.2 3.4 |
c | char (See note) | no | char ch; | Q |
s | array of char (See note) | yes | char s[30]; | hello |
p | void (See note) | yes | int* pi; void* ptr; | 0064FE00 |
n | int (See note) | no | int x, cnt; | X: 123 (cnt==6) |
[ | array of char (See note) | no | char s1[64], s2[64]; |
|
scanf Format Specification Syntax
The control of input conversion is much simpler than for output conversions. Any, all, or none of the following format modifiers may be used between the % and the final letter of the conversion specification. Note these must appear (if at all) in the sequence shown here. A · is used to indicate a space in the example output where spacing is not obvious.
% * maximum-field-width length Letter
Conversion | Description | Example | Matching | Results | |
* | Assignment Supression. This modifier causes the corresponding input to be matched and converted, but not assigned (no matching argument is needed). | int anInt; |
| anInt==29, | |
maximum | This is the maximum number of character to read from the input. Any remaining input is left unread. (Always use this with "%s" and "%[...]" in all production quality code! (No exceptions!) You should use one less than the size of the array used to hold the result.) | int anInt; char s[10]; |
| anInt==23, | |
length | This specifies the exact type of the matching arugment. These length codes are the same as theprintf length modifiers, except as noted below: | ||||
| double d; | 3.14 | d==3.14 |