Syntax
#include <stdio.h> int scanf(const char *format-string, argument-list);Description
scanf reads data from the standard input stream stdin into the locations given by each entry in argument-list. Each argument must be a pointer to a variable with a type that corresponds to a type specifier in format-string. The format-string controls the interpretation of the input fields.
The format-string can contain one or more of the following:
scanf reads format-string from left to right. Characters outside of format specifications are expected to match the sequence of characters in stdin; the matched characters in stdin are scanned but not stored. If a character in stdin conflicts with format-string, scanf ends. The conflicting character is left in stdin as if it had not been read.
When the first format specification is found, the value of the first input field is converted according to the format specification and stored in the location specified by the first entry in argument-list. The second format specification converts the second input field and stores it in the second entry in argument-list, and so on through the end of format-string.
An input field is defined as all characters up to the first white-space character (space, tab, or new line), up to the first character that cannot be converted according to the format specification, or until the field width is reached, whichever comes first. If there are too many arguments for the format specifications, the extra arguments are ignored. The results are undefined if there are not enough arguments for the format specifications.
A format specification has the following form: ┌──────────────────────────────────────────────────────────────────────────────┐│
│
│ >>──%──┬────┬──┬───┬──┬───────┬──┬───┬──type──><
│
│ └─n$─┘ └─*─┘ └─width─┘ ├─h─┤ │
│ ├─l─┤ │
│ └─L─┘ │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
In the format specification above, %n$ specifies the nth argument.
Each field of the format specification is a single character or a number signifying a particular format option. The type character, which appears after the last optional format field, determines whether the input field is interpreted as a character, a string, or a number. The simplest format specification contains only the percent sign and a type character (for example, %s). Each field of the format specification is discussed in detail below.
An asterisk (*) following the percent sign suppresses assignment of the next input field, which is interpreted as a field of the specified type. The field is scanned but not stored.
The width is a positive decimal integer controlling the maximum number of characters to be read from stdin. No more than width characters are converted and stored at the corresponding argument. Fewer than width characters are read if a white-space character (space, tab, or new line), or a character that cannot be converted according to the given format occurs before width is reached.
The optional prefix l shows that you use the long version of the following type, while the prefix h indicates that the short version is to be used. The corresponding argument should point to a long or double object (for the l character), a long double object (for the L character), or a short object (with the h character). The l and h modifiers can be used with the d, i, o, x, and u type characters. The l modifier can also be used with the e, f, and g type characters. The L modifier can be used with the e, f and g type characters. The l and h modifiers are ignored if specified for any other type. Note that the l modifier is also used with the c and s characters to indicate a multibyte character or string.
The type characters and their meanings are in the following table:
┌─────────┬─────────────────────────────┬────────────────────────────┐
│ Char- │ Type of Input Expected. │ Type of Argument. │
│ acter │ │ │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "d" │ Decimal integer. │ Pointer to INT. │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "o" │ Octal integer. │ Pointer to UNSIGNED INT. │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "x", │ Hexadecimal integer. │ Pointer to UNSIGNED INT. │
│ "X" │ │ │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "i" │ Decimal, hexadecimal, or │ Pointer to INT. │
│ │ octal integer. │ │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "u" │ Unsigned decimal integer. │ Pointer to UNSIGNED INT. │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "e, f, │ Floating-point value con- │ Pointer to FLOAT. │
│ g" │ sisting of an optional sign │ │
│ │ (+ or -); a series of one │ │
│ "E, G" │ or more decimal digits pos- │ │
│ │ sibly containing a decimal │ │
│ │ point; and an optional │ │
│ │ exponent (e or E) followed │ │
│ │ by a possibly signed │ │
│ │ integer value. │ │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "c" │ Sequence of bytes; white- │ Pointer to CHAR large │
│ │ space characters that are │ enough for input field. │
│ │ ordinarily skipped are read │ │
│ │ when "c" is specified. │ │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "lc, C" │ Multibyte characters. The │ Pointer to wchar_t large │
│ │ multibyte characters are │ enough for input field. │
│ │ converted to wide charac- │ │
│ │ ters as if by a call to │ │
│ │ mbstowcs. The field width │ │
│ │ specifies the number of │ │
│ │ wide characters matched; if │ │
│ │ no width is specified, one │ │
│ │ multibyte character is │ │
│ │ matched. │ │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "s" │ Sequence of bytes that do │ Pointer to char array │
│ │ not include white space. │ large enough for input │
│ │ │ field plus a terminating │
│ │ │ null character ("\0"), │
│ │ │ which is automatically │
│ │ │ appended. │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "ls, S" │ Multibyte string. None of │ Pointer to wchar_t array │
│ │ the characters can be │ large enough for the input │
│ │ single-byte white-space │ field and the terminating │
│ │ characters (as specified by │ null wide character │
│ │ the isspace function). │ ("L\0"), which is added │
│ │ Each multibyte character in │ automatically. │
│ │ the sequence is converted │ │
│ │ to a wide character as if │ │
│ │ by a call to mbstowcs. │ │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "n" │ No input read from stream │ Pointer to INT into which │
│ │ or buffer. │ is stored the number of │
│ │ │ characters successfully │
│ │ │ read from the stream or │
│ │ │ buffer up to that point in │
│ │ │ the call to scanf. │
├─────────┼─────────────────────────────┼────────────────────────────┤
│ "p" │ Pointer to "void" converted │ Pointer to "void". │
│ │ to series of characters. │ │
└─────────┴─────────────────────────────┴────────────────────────────┘
To read strings not delimited by space characters, substitute a set of characters in brackets ([ ]) for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: the input field is read up to the first character that does appear in the rest of the character set.
To store a string without storing an ending null character (\0), use the specification %ac, where a is a decimal integer. In this instance, the c type character means that the argument is a pointer to a character array. The next number of characters is read from the input stream into the specified location, and no null character is added.
The input for a %x format specifier is interpreted as a hexadecimal number.
scanf scans each input field character by character. It might stop reading a particular input field either before it reaches a space character, when the specified width is reached, or when the next character cannot be converted as specified. When a conflict occurs between the specification and the input character, the next input field begins at the first unread character. The conflicting character, if there was one, is considered unread and is the first character of the next input field or the first character in subsequent read operations on stdin.
scanf returns the number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned.
The return value is EOF for an attempt to read at end-of-file if no conversion was performed. A return value of 0 means that no fields were assigned.
#include <stdio.h> int main(void) { int i; float fp; char c,s[81]; printf("Enter an integer, a real number, a character and a string : \n"); if (scanf("%d %f %c %s", &i, &fp, &c, s) != 4) printf("Not all of the fields were assigned\n"); else { printf("integer = %d\n", i); printf("real number = %f\n", fp); printf("character = %c\n", c); printf("string = %s\n", s); } return 0; /**************************************************************************** The output should be similar to: Enter an integer, a real number, a character and a string : 12 2.5 a yes integer = 12 real number = 2.500000 character = a string = yes ****************************************************************************/ }
This example converts a hexadecimal integer to a decimal integer. The while loop ends if the input value is not a hexadecimal integer.
#include <stdio.h> int main(void) { int number; printf("Enter a hexadecimal number or anything else to quit:\n"); while (scanf("%x", &number)) { printf("Hexadecimal Number = %x\n", number); printf("Decimal Number = %d\n", number); } return 0; /**************************************************************************** The output should be similar to: Enter a hexadecimal number or anything else to quit: 0x231 Hexadecimal Number = 231 Decimal Number = 561 0xf5e Hexadecimal Number = f5e Decimal Number = 3934 0x1 Hexadecimal Number = 1 Decimal Number = 1 q ****************************************************************************/ }Related Information