#include <stdio.h> ret = scanf( format [, p1, p2, ...] );
"scanf" reads input from "stdin" and assigns it to the areas of memory pointed to by the pointers "p1", "p2", and so on. The string pointed to by "format" indicates how to interpret the characters being read in. For example, the format string tells whether ASCII digits should be treated as ASCII character data or converted as part of an integer.
The format string usually contains conversion specifications that are used to control the interpretation of input data. The format string may contain any of the following.
A conversion specification tells the scanning function how to interpret or convert the next input field. The result is placed in the variable pointed to by the corresponding argument, unless this assignment is suppressed by including a * in the conversion specification. An input field is defined as a string of non-space characters; it extends to the next inappropriate character or until the field width limit (if specified) is reached.
The conversion character following the % indicates how to interpret the input field. The corresponding pointer must point to an argument of an appropriate type. The following conversion characters are recognized.
%[0123456789]will gather characters until it finds a character that is not a digit. If the first character inside the square brackets IS a circumflex, the input field consists of all subsequent input characters until the function finds a character that IS in the bracketed character set. Thus
%[^0123456789]will gather characters until it finds a digit. The corresponding argument must point to a character array.
To read a "long char" value, use one of the "h" placeholders.
The function returns the number of successfully matched and assigned input items. This may be used to determine how many input items were found. EOF is returned when end-of-file is encountered. A zero is returned when no conversions have been performed; this may occur if an inappropriate character is discovered in input, causing the scanning function to terminate.
#include <stdio.h> int i; char s[100]; scanf("%d%s",&i,s);may be executed on the input line
666 cheers!to assign 666 to "i" and the string "cheers!\0" to "s". On the input line
0084 0084the number 84 is assigned to "i" and the string "0084\0" is assigned to "s".
The "scanf" function (and its relatives "fscanf" and "sscanf") tend to vary from machine to machine. For example, other implementations of C may have additional control specifiers recognized in the "scanf" format string. Users who are familiar with C on one system should check for differences in "scanf" when moving to a new system.
Previous versions of "scanf" were not affected by the +18bitShorts option. Now, however, if you link with +18bitShorts, the arguments corresponding to "short" placeholders like "%hd" should be pointers to 18-bit short quantities.
Copyright © 2000, Thinkage Ltd.