|To get started, use %hi to input a short, %i
for an int, %li for a long, %f
for a float, %lf for a double, %Lf
for a long double, %c for a char (or %i
to input it as a number) or %s for a string (char * or char
). Then refine the formatting further as desired.
An extremely common error is to forget that the arguments must be pointers! So if x is a float, the correct call is scanf("%f", &x); (because &x is of type float *), not scanf("%f", x);.
For ease of debugging: if you want to input multiple values from the keyboard, do so one at a time. Each time output a suitable prompt, then call scanf to input a single value.
int scanf(const char *format, ...)
int fscanf(FILE *stream, const char *format, ...)
int sscanf(char *string, const char *format, ...)
The functions return the number of input values converted and assigned, or 'EOF' if the end of file is reached or an error occurs before any conversion.
The format string tells scanf how to interpret the input. It may contain:
% [*] [field_width] [length_modifier] conversion_character
where components in brackets [ ] are optional. The minimum is therefore a % and a conversion character (e.g. %i). There is generally one conversion specification for each variable. The conversion process stops when the format string is exhausted or when an input fails to meet the conversion specification.
An input field is defined as a string of non-whitespace characters.
If the optional * flag is included, assignment is suppressed, i.e. the input field is converted according to the specification, but not assigned to a variable. Otherwise the assignment is made to the corresponding argument, which must be a pointer to a variable of the appropriate type.
An input field (a sequence of non-whitespace characters) extends either to the next whitespace character or until the field width, if specified, is reached. (So scanf will read across line ends to find its input, because newlines are whitespace.)
|h||For d, i, o, u or x conversions, the value is to be input as a short, not an int.|
|l||For d, i, o, u
or x conversions, the value is to be input as a long, not
For e, f or g conversions, the value is to be input as a double, not a float.
|L||For e, f or g
conversions, the value is to be input as a long double.
|d||Input a decimal integer; argument must be int *.|
|i||Input an integer; argument must be int *. The integer may be in octal (with a leading 0) or hexadecimal (with a leading 0x or 0X).|
|o||Input an octal integer (with or without a leading 0); argument must be int *.|
|u||Input an unsigned decimal integer; argument must be unsigned int *.|
|x||Input a hexadecimal integer (with or without a leading 0x or 0X); argument must be int *.|
|c||Input one or more characters; argument must be char *. The characters are put in the character array, up to the number given by the field width. (No terminating '\0' is added.) If a field width is given, whitespace is not skipped. If no field width is given, only one character is input. (To read the next non-white space character, use %1s.)|
|s||Input a string of non-whitespace characters; the argument must be char *. A terminating '\0' is added.|
|e, f, g||Input a floating point number; argument must be float *. The input format is an optional sign, a string of numbers (possibly containing a decimal point), and an optional exponent field containing an E or e followed by a (possibly signed) integer. So numbers to be input can be in either decimal or scientific notation.|
|p||Input a pointer value; argument must be void *. The representation is implementation dependent and is as printed by printf("%p").|
|n||No input is read. Instead, the argument receives the number of characters read so far by this call; argument must be int *.|
|[...]||Inputs a string; argument must be char *. A terminating '\0' is added. Only the characters listed between the brackets are accepted. Input ends when a non-matching character is reached or the field width is reached. To accept the ']' character, list it first: ...]|
|[^...]||Inputs a string; argument must be char *. A terminating '\0' is added. Only characters not listed between the brackets are accepted. Input ends when a non-matching character is reached or the field width is reached. To reject the ']' character, list it first: [^]...]|
|%||Literal %: reads the '%' character. No assignment is made.|