![]() |
![]() |
![]() |
![]() |
Scan formatted input from stdin
#include <stdio.h> int scanf( const char* format, ... );
libc
The scanf() function scans input from stdin under control of the format argument, assigning values to the remaining arguments.
The format control string consists of zero or more format directives that specify acceptable input data. Subsequent arguments are pointers to various types of objects that are assigned values as the format string is processed.
A format directive can be a sequence of one or more whitespace characters or:
![]() |
If the multibyte string is invalid, processing stops and the rest of the format string is output, including the % characters. The C-TRADITIONAL locale switches multibyte processing from UTF-8 to 1-to-1 and safely transfers misformed multibyte characters. |
As each format directive in the format string is processed, the directive may successfully complete, fail because of a lack of input data, or fail because of a matching error as defined by the directive.
If end-of-file is encountered on the input data before any characters that match the current directive have been processed (other than leading whitespace, where permitted), the directive fails for lack of data.
If end-of-file occurs after a matching character has been processed, the directive is completed (unless a matching error occurs), and the function returns without processing the next directive.
If a directive fails because of an input character mismatch, the character is left unread in the input stream.
Trailing whitespace characters, including newline characters, aren't read unless matched by a directive. When a format directive fails, or the end of the format string is encountered, the scanning is completed, and the function returns.
When one or more whitespace characters (space, horizontal tab \t, vertical tab \v, form feed \f, carriage return \r, newline or linefeed \n) occur in the format string, input data up to the first non-whitespace character is read, or until no more data remains. If no whitespace characters are found in the input data, the scanning is complete, and the function returns.
An ordinary character in the format string is expected to match the same character in the input stream.
A conversion specifier in the format string is processed as follows:
A type length specifier affects the conversion as follows:
The valid conversion type specifiers are:
When an l (ell) qualifier is present, a sequence of characters are inputted from the initial shift state to wchar_t wide characters as if by a call to mbrtowc(). The conversion state is described by a mbstate_t object.
When an l (ell) qualifier is present, a sequence of characters are inputted from the initial shift state to wchar_t wide characters as if by a call to mbrtowc(). The conversion state is described by a mbstate_t object.
When an l (ell) qualifier is present, a sequence of characters are inputted from the initial shift state to wchar_t wide characters as if by a call to mbrtowc() with mbstate set to 0. The argument is assumed to point to the first element of a wchar_t array of sufficient size to contain the sequence and a terminating NUL character, which is added by the conversion operation.
The conversion specification includes all characters in the scanlist between the beginning [ and the terminating ]. If the conversion specification starts with [^, the scanlist will contain all the characters that aren't in the scanlist. If the conversion specification starts with [] or [^], the ] is included in the scanlist. (Scanning for ] only would be specified as %[]].)
A conversion type specifier of % is treated as a single ordinary character that matches a single % character in the input data. A conversion type specifier other than those listed above causes scanning to terminate, and the function to returns with an error.
The line:
scanf( "%s%*f%3hx%d", name, &hexnum, &decnum )
with input:
some_string 34.555e-3 abc1234
copies "some_string" into the array name, skips 34.555e-3, assigns 0xabc to hexnum and 1234 to decnum. The return value is 3.
The program:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main( void ) { char string1[80], string2[80]; memset( string1, 0, 80 ); memset( string2, 0, 80 ); scanf( "%[abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWZ ]%*2s%[^\n]", string1, string2 ); printf( "%s\n", string1 ); printf( "%s\n", string2 ); return EXIT_SUCCESS; }
with input:
They may look alike, but they don't perform alike.
assigns "They may look alike" to string1, skips the comma (the "%*2s" matches only the comma; the following blank terminates that field), and assigns " but they don't perform alike." to string2.
To scan a date in the form "Friday March 26 1999":
#include <stdio.h> #include <stdlib.h> #include <string.h> int main( void ) { int day, year; char weekday[10], month[12]; int retval; memset( weekday, 0, 10 ); memset( month, 0, 12 ); retval = scanf( "%s %s %d %d", weekday, month, &day, &year ); if( retval != 4 ) { printf( "Error reading date.\n" ); printf( "Format is: Friday March 26 1999\n" ); return EXIT_FAILURE; } printf( "weekday: %s\n", weekday ); printf( "month: %s\n", month ); printf( "day: %d\n", day ); printf( "year: %d\n", year ); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
fscanf(), sscanf(), vfscanf(), vsscanf()
![]() |
![]() |
![]() |
![]() |