Parameters | const char *format The format string may contain three classes of characters or specifications: - White space characters
- Any characters except white space characters and the percent character (%).
- Format statements beginning with the percent character (%)
'BLANK' | blank | \n | newline | \t | tab |
'BLANK' | blank | \n | newline | \t | tab | \f | form feed | \v | vertical tab | \r | carriage return |
The format string may contain any number of white space characters (or none). These characters have no control function. Any white space characters in the input are treated as delimiters between input fields and are not converted (cf. %c and %[ ] for exceptions). The character must match the next character of input. scanf reads the input character, but does not convert it or store it in a variable. If the input character does not match the character specified here, input processing is aborted. argumentlist Pointer to variables in which scanf is to store the converted results. No pointer arguments may be specified for %* statements (skip assignment) in format. There must be one pointer argument each for all other % statements. The data type of the pointer argument is determined by the type specification in the corresponding format statement. The format statement is described below separately for KR functionality and ANSI functionality. Format statements contain specifications on how the input fields are to be interpreted and converted. They may be structured as follows:
% [*][n]{ [{l | h} {d | o | x} |
[l] {e | f} |
{D | O | E | F} |
{c | s}
{[...] | [^...]} |
% }
A format statement is associated with one input field. An input field is a string of characters that is terminated by the first white space character by a character that does not match the type specification in the format statement when the explicitly specified field length n is reached.
Leading white space characters are ignored during input. Each format statement must begin with a percent character (%). The remaining characters are interpreted as follows: * | Skip an assignment.The next input field is read and converted, but not stored in a variable. | n | Maximum length of the input field to be converted. If a white space character or a character that does not match the type specification in the format statement appears before this, the length is correspondingly shortened. | l | l before d, o, x: Conversion of an argument of type pointer to long int (d) or unsigned long int (o, x). The specification is identical to the uppercase letters D, O, X. l before e, f: Conversion of an argument of type pointer to double . The specification is identical to the uppercase letters E, F. | h | h before d, o, x: Conversion of an argument of type pointer to short int (d) or unsigned short int (o, x). | d | A decimal integer value is expected. The corresponding argument must be a pointer to int . | o | An octal integer value is expected. The corresponding argument may be a pointer to unsigned int or int . Internally the value is represented as unsigned . | x | A hexadecimal integer value is expected. The corresponding argument may be a pointer to unsigned int or int . Internally the value is represented as unsigned . | e, f | A floating-point number is expected. The corresponding argument must be a pointer to float . The floating-point number can contain a sign as well as an exponent (E or e, followed by an unsigned integer value). The decimal point character is determined by the locale (category LC_NUMERIC). The default is a period. | c | A character is expected. The corresponding argument should be a pointer to character . In this case scanf also reads blanks. "%1s" should be used to read the next character that is not a blank. c is suitable for reading strings that also contain blanks. To do so, a pointer to a char array must be passed as an argument and a field length of n must be specified (e.g. "%10c"). The scanf function does not automatically terminate the string with the null byte in this case. | s | A string is expected. The corresponding argument must be a pointer to a char array and large enough to be able to accept the string and a terminating null byte (\0).
scanf automatically terminates the string with the null byte. Leading white space characters in the input are ignored and a trailing white space character is interpreted as a delimiter (end of the string). | [ ] | A string is expected. The corresponding argument must be a pointer to a char array and large enough to be able to accept the string (including the null byte that is automatically appended). In this specification, as opposed to %s, blanks do not automatically function as delimiters. [...] | In this specification, characters are read in until the first character not listed in the square brackets appears. Thus, the string may only consist of the characters appearing within [ ]; any characters not specified are treated as delimiters. | [^...] | In this specification, characters are read in until one of the characters listed in the square brackets after ^ is encountered. Only the characters specified within the [ ] are treated as delimiters. |
| % | Input of the % character, no conversion. | Format statements contain information as to how the input fields are to be interpreted and converted. They may be structured as follows:
% [*][n]{ [h | hh | l | ll| j | z | t] {d | i | o | u | x | X} |
[h | hh | l | ll| j | z | t] n |
[l | L] {a | A | e | E | f | F | g | G} |
p |
[l] { [...] | [^...] | c | s} |
% }
A format statement is associated with an input field. An input field is a sequence of characters which is terminated by the first white space character, by a character which does not match the format statement (type specification), when the explicitly specified field length n is reached.
Leading white space characters are ignored. Every format statement must begin with a percent character (%). The remaining characters are interpreted as follows: * | Skip an assignment.The next input field is read and converted, but not stored in a variable. | n | Maximum length of the input field to be converted. If a white space character or a character that does not match the type specification in the format statement appears before this, the length is correspondingly shortened. | l | l before d, i, o, u, x, X: Conversion of an argument of type pointer to long int (d, i) or unsigned long int (o, u, x, X). l before a, A, e, E, f, F, g, G: Conversion of an argument of type pointer to double . l before c, s, oder [ : Conversion of an argument of type pointer to wchar_t . l before n: The argument is of the type pointer to long int (no conversion). | ll | ll before d, i, o, u, x, X: Conversion of an argument of type pointer to long long int (d, i) or unsigned long long int (o, u, x, X). ll vor n: The argument is of the type pointer to long long int (no conversion). | hh | hh before d, i, o, u, x, X: Conversion of an argument of type pointer to signed char (d, i) or unsigned char (o, u, x, X). hh before n: The argument is of the type pointer to signed char (no conversion). | h | h before d, i, o, u, x, X: Conversion of an argument of type pointer to short int (d, i) bzw. unsigned short int (o, u, x, X). h before n: The argument is of the type pointer to short int (no conversion). | j | j before d, i, o, u, x, X: Conversion of an argument of type pointer to intmax_t (d , i ) or uintmax_t (o , u , x , X ). j before n: The argument is of the type pointer to intmax_t (no conversion). | z | z before d, i, o, u, x, X: Conversion of an argument of type pointer to signed long int (d , i ) bzw. size_t (o , u , x , X ). z before n : The argument is of the type pointer to signed long int (no conversion). | t | t before d, i, o, u, x, X: Conversion of an argument of type pointer to ptrdiff_t (d , i ) bzw. unsigned long int (o , u , x , X ). t before n: The argument is of the type pointer to ptrdiff_t (no conversion). | L | L before a, A, e, E, f, F, g, G: Conversion of an argument of the type pointer to long double . | d | A decimal integer value is expected. The corresponding argument must be a pointer to int . | i | An integer value is expected. The base (hexadecimal, octal, decimal) is determined from the structure of the input field. Leading 0x or 0X: hexadecimal Leading 0: octal Otherwise: decimal The corresponding argument must be a pointer to int . | o | An octal integer value is expected. The corresponding argument may be a pointer to unsigned int or int . Internally the value is represented as unsigned . | e, f | A floating-point number is expected. The corresponding argument must be a pointer to float . The floating-point number can contain a sign as well as an exponent (E or e, followed by an unsigned integer value). The decimal point character is determined by the locale (category LC_NUMERIC). The default is a period. | u | A decimal integer value is expected. The corresponding argument must be a pointer to unsigned int . | x, X | A hexadecimal integer value is expected. The corresponding argument may be a pointer to unsigned int or int . Internally the value is represented as unsigned . | a, A, e, E, f, F, g, G |
| A floating-point number is expected. The expected format is the same as expected for the function strtod . The corresponding argument must be a pointer to float . The decimal point character is determined by the locale (category LC_NUMERIC). The default is a period. | c | Without formatting character l : A character is expected. The corresponding argument should be a pointer to character . In this case scanf also reads blanks. "%1s" should be used to read the next character that is not a blank. c is suitable for reading strings that also contain blanks. To do so, a pointer to a char array must be passed as an argument and a fieldlength of n must be specified (e.g. "%10c"). The scanf function does not automatically terminate the string with the null byte in this case. With formatting character l : A multibyte character-string that begins in the initial shift state is expected. Each character in the sequence is converted to a wide character as if by a call to the mbrtowc function, with the conversion state described by an mbstate_t object initialized to zero before the first character is converted. No null wide character is added. The application shall ensure that the corresponding argument is a pointer to an array of wchar_t large enough to accept the resulting sequence of wide characters. | p | An 8-digit pointer value is expected, analogous to the format %08.8x . The corresponding argument must be of type pointer to void . | s | Without formatting character l : A string is expected. The corresponding argument must be a pointer to a char array and large enough to be able to accept the string and a terminating null byte (\0).
scanf automatically terminates the string with the null byte. Leading white space characters in the input are ignored and a trailing white space character is interpreted as a delimiter (end of the string). With formatting character l : A multibyte character-string that begins in the initial shift state is expected. Each character is converted to a wide character as if by a call to the mbrtowc function, with the conversion state described by an mbstate_t object initialized to zero before the first character is converted.The application shall ensure that the corresponding argument is a pointer to an array of wchar_t large enough to accept the sequence and the terminating null wide character, which is added automatically. Leading white-space characters in the input are ignored; a trailing whitespace character is interpreted as a delimiter (end of the string). | [ ] | Without formatting character l : A string is expected. The corresponding argument must be a pointer to a char array and large enough to be able to accept the string (including the null byte that is automatically appended). In this specification, as opposed to %s, blanks do not automatically function as delimiters. With conversion character l : A multibyte character-string that begins in the initial shift state is expected. Each character in the sequence shall be converted to a wide character as if by a call to the mbrtowc function, with the conversion state described by an mbstate_t object initialized to zero before the first character is converted. The application shall ensure that the corresponding argument is a pointer to an array of wchar_t large enough to accept the sequence and the terminating null wide character, which shall be added automatically. [...] | In this specification, characters are read until the first character not specified in the square brackets is encountered. In other words, the string may only consist of characters within the square brackets [ ]. All characters not specified are treated as delimiters.The closing bracket ] can be included in the list of characters to be read if it is specified as the first character immediately after the opening bracket: [ ]...]. | [^...] | In this specification, characters are read in until one of the characters listed in the square brackets after ^ is encountered. Only the characters specified within the [ ] are treated as delimiters. The closing bracket ] can be included in the list of delimiters if it is specified as the first character immediately after the character ^: [^]...]. |
| n | No characters are read from the input field. The argument is of type pointer to int . This integer variable is assigned the number of characters that scanf has processed up to this time. | % | Input of the % character, no conversion..
| |
Notes | In converting integer values to unsigned int (o, u, x, X) the twos complement is formed from a value with a negative sign. For example, format %u for input -1 gives X’FFFFFFFF’. You should always check the result of a scanf call to be sure that no error has occurred! The next scanf call starts reading immediately after the character last processed by the previous call. If an input character does not correspond to the format specified, it is written back to the input buffer. It must be fetched there with getc ; otherwise, the next scanf call will receive the same character again. If there are more pointer arguments than format statements (excluding the %* specifications), the excess arguments are ignored. If there are fewer arguments, results are undefined. The following applies in the case of text files with SAM access mode and variable record length for which a maximum record length is also specified: When the specification split=no was entered for fopen , records of maximum record length are not concatenated with the subsequent record when they are read. By default or with the specification split=yes , when a record with maximum record length is read, it is assumed that the following record is the continuation of this record and the records are concatenated. |