scanf

Scans input from a stream

Local Servers:blocking
Remote Servers:blocking
Classification:ANSI
Platform:NLM
Service:Stream I/O

Syntax

  #include <stdio.h>  
   
  int scanf  (  
     const char   *format,   
     ... );
  

Parameters

format
(IN) Points to the format control string.

Return Values

The scanf function returns EOF when the scanning is terminated by reaching the end of the input stream. Otherwise, the number of input arguments for which values were successfully scanned and stored is returned.

Remarks

The scanf function scans input from the file designated by stdin under control of the argument format. Following the format string is the list of addresses of items to receive values.

The format control string consists of zero or more format directives that specify acceptable input file 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 white-space characters, an ordinary character, or a conversion specifier. An ordinary character in the format string is any character, other than a white-space character or the percent (%) character, that is not part of a conversion specifier. A conversion specifier is a sequence of characters in the format string, which begins with a % and is followed, in sequence, by the following:

  • 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 pointer type specification: one of N or F
  • An optional type length specification: one of h, l or L
  • A character that specifies the type of conversion to be performed. It can be any one of the following characters: c, d, e, f, g, i, o, n, p, s, u, x, or [.

As each format directive in the format string is processed, the directive can successfully complete, fail because of a lack of input data, or fail because of a matching error as defined by the particular 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 white-space 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 white-space characters, including newline characters, are not 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 white-space characters—space, horizontal tab (\t), vertical tab (\v), form feed (\f), carriage return (\r), newline or line feed (\n)—occur in the format string, input data up to the first nonwhite-space character is read, or until no more data remains. If no white-space 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:

  • For conversion types other than [, c, and n, leading white-space characters are skipped.
  • For conversion types other than n, all input characters, up to any specified maximum field length, that can be matched by the conversion type are read and converted to the appropriate type of value; the character immediately following the last character to be matched is left unread; if no characters are matched, the format directive fails.
  • Unless the assignment suppression indicator (*) was specified, the result of the conversion is assigned to the object pointed to by the next unused argument (if assignment suppression was specified, no argument is skipped); the arguments must correspond in number, type, and order to the conversion specifiers in the format string.

A pointer type specification is used to indicate the type of pointer used to locate the next argument to be scanned:

F

Points to a far pointer.

N

Points to a near pointer.

The pointer type defaults to that used for data in the memory model for which the program has been compiled.

A type length specifier affects the conversion as follows:

h

Causes a d, i, o, u, or x (integer) conversion to assign the converted value to an object of type short int or unsigned short int.

It causes an n (read length assignment) operation to assign the number of characters that have been read to an object of type unsigned short int.

l

Causes a d, i, o, u, or x (integer) conversion to assign the converted value to an object of type long int or unsigned long int.

It causes an n (read length assignment) operation to assign the number of characters that have been read to an object of type unsigned long int.

It causes an e, f, or g (floating point) conversion to assign the converted value to an object of type double.

L

Causes an e, f, or g (floating point) conversion to assign the converted value to an object of type long double.

The valid conversion type specifiers are:

c

Any sequence of characters in the input stream of the length specified by the field width, or a single character if no field width is specified, is matched. The argument is assumed to point to the first element of a character array of sufficient size to contain the sequence, without a terminating NULL character (\0). For a single-character assignment, a pointer to a single object of type char is sufficient.

d

A decimal integer, consisting of an optional sign, followed by one or more decimal digits, is matched. The argument is assumed to point to an object of type int.

e, f, g

A floating-point number, consisting of an optional sign (+ or -), followed by one or more decimal digits, optionally containing a decimal-point character, followed by an optional exponent of the form e or E, an optional sign, and one or more decimal digits, is matched. The exponent, if present, specifies the power of ten by which the decimal fraction is multiplied. The argument is assumed to point to an object of type float.

i

An optional sign, followed by an octal, decimal, or hexadecimal constant is matched. An octal constant consists of zero and zero or more octal digits. A decimal constant consists of a nonzero decimal digit and zero or more decimal digits. A hexadecimal constant consists of the characters 0x or 0X followed by one or more (upper- or lowercase) hexadecimal digits. 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 unsigned int that is pointed to by the argument. The number of items that have been scanned and assigned (the return value) is not affected by the n conversion type specifier.

o

An octal integer, consisting of an optional sign, followed by one or more (zero or nonzero) octal digits, is matched. The argument is assumed to point to an object of type int.

p

A hexadecimal integer, as described for x conversions below, is matched. The converted value is further converted to a value of type void* and then assigned to the object pointed to by the argument.

s

A sequence of nonwhite-space characters is matched. The argument is assumed to point to the first element of a character array of sufficient size to contain the sequence and a terminating NULL character, which is added by the conversion operation.

u

An unsigned decimal integer, consisting of one or more decimal digits, is matched. The argument is assumed to point to an object of type unsigned int.

x

A hexadecimal integer, consisting of an optional sign, followed by an optional prefix 0x or 0X, followed by one or more (uppercase or lowercase) hexadecimal digits, is matched. The argument is assumed to point to an object of type int.

[c1c2. ..]

A sequence of characters, consisting of any of the characters c1, c2, ... called the scanset, in any order, is matched. c1 cannot be the caret character (^). If c1 is ], that character is considered to be part of the scanset and a second ] is required to end the format directive. The argument is assumed to point to the first element of a character array of sufficient size to contain the sequence and a terminating NULL character, which is added by the conversion operation.

[^c1c2. ..]

A sequence of characters, consisting of any of the characters other than the characters between the ^ and ], is matched. As with the preceding conversion, if c1 is ], it is considered to be part of the scanset and a second ] ends the format directive. The argument is assumed to point to the first element of a character array of sufficient size to contain the sequence and a terminating NULL character, which is added by the conversion operation.

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 the function to return.

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, skip 34.555e-3, assign 0xabc to hexnum and 1234 to decnum. The return value is 3.

The line

  char fmt[100];  
     strcpy (fmt, "%[abcdefghijklmnopqrstuvwxyz");  
     strcat (fmt,"[ABCDEFGHIJKLMNOPQRSTUVWZ]%*2s%[W\n]");  
     scanf (fmt, string1, string2)
  

with input

     They may look alike, but they don’t perform alike.
  

assigns

      "They may look alike"
  

to string1, skip the comma and the space, and assign

     " but they don’t perform alike.".
  

to string2. (The %*2s only matches the ","; the next blank terminates that field.)

See Also

fscanf, sscanf (NDK: Program Management)

scanf Example

To scan a date in the form "Saturday April 18 1991":

  #include  <stdio.h>  
  int day, year;  
  char weekday[10], month[12];  
  scanf ("%s %s %d %d", weekday, month, &day, &year);