Scanf
Encyclopedia : S : SC : SCA : Scanf
- The correct title of this } is }}}. The initial letter is capitalized due to [Naming conventions #Lower case first lettertechnical restrictions].
The scanf function prototype is:
int scanf (char *format, ...);
So far as is traceable, "scanf" stands for "scan format".
Usage
The scanf function is especially important for C, which reads input for numbers and other datatypes directly from a console or command prompt.
The following shows code in C that reads a variable number of unformatted decimal integers from the console and prints out each of them on a separate line:
#includeAfter being processed by the program above, a messy list of integers such asint main ()
456 123 789 456 12 456 1 2378will appear neatly as:
456 123 789 456 12 456 1 2378No matter what the datatype the programmer wants the program to read, the arguments (such as
&n above) must appear in form of pointers pointing to memory locations instead of a variable value. Otherwise, the function will not perform correctly.
As scanf is designated to read input only from the console, many programming languages with interfaces, such as PHP, have derivatives such as sscanf and fscanf but not scanf itself.
Derivatives of scanf
Depending on the actual source of input, programmers can utilize different derivatives ofscanf. Two common examples are sscanf and fscanf.sscanf
The sscanf derivative reads input from a user-specified string source instead of the console (known as standard input,stdin). It has prototypes as follows:
(C or C++)
int sscanf (const char *source, const char *format, ...);
(PHP)
int sscanf (const string source, const string format [, mixed args...])
Unlike scanf and fscanf, sscanf does not remove the read items away from the input flush. Therefore, every time the function is executed, the string source is read again from the very beginning.
fscanf
The fscanf derivative reads input from a file instead of the standard input. The prototypes are as follows:
(C or C++)
int fscanf (FILE *file, const char *format, ...);
(PHP)
int fscanf (resource file, const string format [, mixed args...])
The fscanf derivative works like the original scanf function - parts of the input, once read, will not be read again until the file is closed and reopened.
vscanf, vsscanf, and vfscanf
int vscanf (const char *format, va_list args); int vsscanf (const char *source, const char *format, va_list args); int vfscanf (FILE *file, const char *format, va_list args);These are like the same functions without the
v prefix, except they take their arguments from a va_list. (See stdarg.h.) These variants may be used in variable-argument functions.Format string specifications
The formatting placeholders inscanf are more or less the same as that in printf, its reverse function. There are rarely constants in a format string, mainly because a program is usually not designed to read known data.Some of the most commonly used placeholders follow:
- '%d', '%i' : Scan an integer as a signed decimal number.
- '%u' : Scan for decimal unsigned int, unsigned short, and unsigned char
- '%f' : Scan a floating-point number in normal (fixed-point) notation.
- '%g', '%G' : Scan a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. '%g' uses lower-case letters and '%G' uses upper-case.
- '%x', '%X' : Scan an integer as a hexadecimal number. '%x' uses lower-case letters and '%X' uses upper-case.
- '%s' : Scan a character string.
- '%c' : Scan a character (char).
- '(space)': Spacebar scans for whitespace characters.
An example of a format string is
"%7d%s %c%lf"
Error handling
scanf is usually used in situations when the program cannot guarantee that the input is in the expected format. Therefore a robust program must check whether the scanf call succeeded and take appropriate action. If the input was not in the correct format, the erroneous data will still be on the input stream and must be read and discarded before new input can be read. An alternative method of reading input, which avoids this, is to use fgets and then check the string read in.See also
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
