scanf()

Updated: April 19, 2023

Scan formatted input from stdin

Synopsis:

#include <stdio.h>

int scanf( const char* format, 
           ... );

Arguments:

format
A string that controls the format of the input, as described below. The formatting string determines what additional arguments you need to provide.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The scanf() function scans input from stdin under control of the format argument, assigning values to the remaining arguments.

Note: The scanf() function is generally considered unsafe for string handling; it's safer to use fgets() to get a line of input and then use sscanf() to process the input.

Format control string

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:

multibyte characters
Any character in the format string, other than a whitespace character or the percent character (%), that isn't part of a conversion specifier.
conversion specifiers
A sequence of characters in the format string that begins with a percent character (%) and is followed by:
  • an optional assignment suppression indicator: the asterisk character (*)
  • an optional decimal integer that specifies the maximum field width to be scanned for the conversion
  • an optional type length specification; one of h, L, or l
  • a character that specifies the type of conversion to be performed; one of the characters: c, d, e, f, g, i, n, o, p, s, u, X, x, [

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.

Conversion specifiers

A conversion specifier in the format string is processed as follows:

Type length specifiers

A type length specifier affects the conversion as follows:

Conversion type specifiers

The valid conversion type specifiers are:

a, A, e, E, f, F, g or G
A floating-point number, infinity, or NaN, all of which have a format as expected by strtod(). The argument is assumed to point to an object of type float.
c
Any sequence of characters in the input stream of the length specified by the field width, or a single character if you don't specify a field width. The argument is assumed to point to the first element of a character array of sufficient size to contain the sequence, without a terminating NUL character ('\0'). For a single character assignment, a pointer to a single object of type char is sufficient.

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.

d
A decimal integer with a format as expected by strtol() and a base of 10. The argument is assumed to point to an object of type int.
i
An optionally signed integer with a format as expected by strtol() and a base of 0. The argument is assumed to point to an object of type int.
n
No input data is processed. Instead, the number of characters that have already been read is assigned to the object of type int that's pointed to by the argument. The number of items that have been scanned and assigned (the return value) isn't affected by the n conversion type specifier.
o
An optionally signed octal integer with a format as expected by strtoul() and a base of 8. The argument is assumed to point to an object of type int.
p
A hexadecimal integer, as described for x conversions below. The converted value is taken as a void * and then assigned to the object pointed to by the argument.

If the sequence contains more than 8 digits, the conversion is performed only on the last 8 digits in the sequence.

s
A sequence of non-whitespace characters. The argument is assumed to point to the first element of a character array of sufficient size to contain the sequence of char, signed char or unsigned char and a terminating NUL character, which the conversion operation adds.

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.

u
An unsigned decimal integer, consisting of one or more decimal digits. The argument is assumed to point to an object of type unsigned int.
x, X
A hexadecimal integer, with a format as expected by strtoul() when base is 16. The argument is assumed to point to an object of type unsigned.
[
Matches the scanset, a nonempty sequence of characters. The argument is assumed to point to the first element of a character array of sufficient size to contain the sequence and a terminating NUL character, which the conversion operation adds.

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 % character (The entire specification is %%).

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.

Returns:

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.

Examples:

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( "%10s %12s %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;
}

Classification:

ANSI, POSIX 1003.1

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread Yes