Home > Electronics Tutorials > C Language Tutorial > stdio.h - Formatted I/O Functions - scanf

C Language Programming Library Reference Guide

stdio.h - Formatted I/O Functions - scanf

Declarations: int fscanf(FILE *stream, const char *format, ...);
int scanf(const char *
format, ...);
int sscanf(const char *
str, const char *format, ...);
 

The ..scanf functions provide a means to input formatted information from a stream.

fscanf reads formatted input from a stream
scanf reads formatted input from stdin
sscanf reads formatted input from a string

These functions take input in a manner that is specified by the format argument and store each input field into the following arguments in a left to right fashion.

Each input field is specified in the format string with a conversion specifier which specifies how the input is to be stored in the appropriate variable. Other characters in the format string specify characters that must be matched from the input, but are not stored in any of the following arguments. If the input does not match then the function stops scanning and returns. A whitespace character may match with any whitespace character (space, tab, carriage return, new line, vertical tab, or formfeed) or the next incompatible character.

An input field is specified with a conversion specifer which begins with the % character. After the % character come the following in this order:

[*] Assignment suppressor (optional).
[width] Defines the maximum number of characters to read (optional).
[modifier] Overrides the size (type) of the argument (optional).
[type] The type of conversion to be applied (required).
 

Assignment suppressor:
Causes the input field to be scanned but not stored in a variable.

Width:
The maximum width of the field is specified here with a decimal value. If the input is smaller than the width specifier (i.e. it reaches a nonconvertible character), then what was read thus far is converted and stored in the variable.

Modifier:
A modifier changes the way a conversion specifier type is interpreted.

[modifier] [type] Effect  
h d, i, o, u, x The argument is a short int or unsigned short int.< /td>
h n Specifies that the pointer points to a short int.
l d, i, o, u, x The argument is a long int or unsigned long int .
l n Specifies that the pointer points to a long int.
l e, f, g The argument is a double.
L e, f, g The argument is a long double.
 

Conversion specifier type:
The conversion specifier specifies what type the argument is. It also controls what a valid convertible character is (what kind of characters it can read so it can convert to something compatible).

[type] Input
d Type signed int represented in base 10. Digits 0 through 9 and the sign (+ or -).
i Type signed int. The base (radix) is dependent on the first two characters. If the first character is a digit from 1 to 9, then it is base 10. If the first digit is a zero and the second digit is a digit from 1 to 7, then it is base 8 (octal). If the first digit is a zero and the second character is an x or X, then it is base 16 (hexadecimal).
o Type unsigned int. The input must be in base 8 (octal). Digits 0 through 7 only.
u Type unsigned int. The input must be in base 10 (decimal). Digits 0 through 9 only.
x, X Type unsigned int. The input must be in base 16 (hexadecimal). Digits 0 through 9 or A through Z or a through z. The characters 0x or 0X may be optionally prefixed to the value.
e, E, f, g, G Type float. Begins with an optional sign. Then one or more digits, followed by an optional decimal-point and decimal value. Finally ended with an optional signed exponent value designated with an e or E.
s Type character array. Inputs a sequence of non-whitespace characters (space, tab, carriage return, new line, vertical tab, or formfeed). The array must be large enough to hold the sequence plus a null character appended to the end.
[...] Type character array. Allows a search set of characters. Allows input of only those character encapsulated in the brackets (the scanset). If the first character is a carrot (^), then the scanset is inverted and allows any ASCII character except those specified between the brackets. On some systems a range can be specified with the dash character (-). By specifying the beginning character, a dash, and an ending character a range of characters can be included in the scanset. A null character is appended to the end of the array.
c Type character array. Inputs the number of characters specified in the width field. If no width field is specified, then 1 is assumed. No null character is appended to the array.
p Pointer to a pointer. Inputs a memory address in the same fashion of the %p type produced by the printf function.
n The argument must be a pointer to an int. Stores the number of characters read thus far in the int. No characters are read from the input stream.
% Requires a matching % sign from the input.
 

Reading an input field (designated with a conversion specifier) ends when an incompatible character is met, or the width field is satisfied.

On success the number of input fields converted and stored are returned. If an input failure occurred, then EOF is returned.

Note: To report broken links or to submit your projects, tutorials please email to Webmaster

Discover

     more......