Scan formatted input from stdin
#include <stdio.h> int scanf( const char* format, ... );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
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 what you consider to be acceptable input data. Subsequent arguments are pointers to various types of objects that the function assigns values to as it processes the format string.
A format directive can be a sequence of one or more whitespace characters or:
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 (“el”) qualifier is present, a sequence of characters are converted 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.
If the sequence contains more than 8 digits, the conversion is performed only on the last 8 digits in the sequence.
When an l (“el”) qualifier is present, a sequence of characters are converted 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 (“el”) qualifier is present, a sequence of characters are converted 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 the conversion operation adds.
The conversion specification includes all characters in the scanlist between the beginning [ and the terminating ]. If the conversion specification starts with [^, the scanlist matches all the characters that aren't in the scanlist. If the conversion specification starts with [] or [^], the ] is included in the scanlist. (To scan for ] only, specify %[]].)
A - in the scanlist that isn't the first character, nor the second where the first character is a ^, nor the last character, defines a range of characters to be matched. This range consists of characters numerically greater than or equal to the character before the -, and numerically less than or equal to the character after the -.
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 number of input arguments for which values were successfully scanned and stored, or EOF if the scanning stopped by reaching the end of the input stream before storing any values.
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(), fwscanf() sscanf(), swscanf(), vfscanf(), vfwscanf(), vscanf(), vsscanf(), vswscanf(), vwscanf(), wscanf()