scanf


Description:

[ CCode ( cname = "sscanf" ) ]
[ ScanfFormat ]
public int scanf (string format, ...)

Reads the data from string

The format string consists of whitespace characters (any single whitespace character in the format string consumes all available consecutive whitespace characters from the input), non-whitespace characters except % (each such character in the format strings consumes exactly one identical character from the input) and conversion specifications. Each conversion specification has the following format:

%[flags][width][.precision][length]specifier

Flags:

Character Description
+ The sign of signed conversions is always prepended to the result of the conversion
<space> if the result of a signed conversion does not start with a sign character, or is empty, space is prepended to the result. It is ignored if + flag is present.
- The result of the conversion is left-justified within the field
# Alternative form of the conversion is performed.
0 Use 0 instead of spaces for padding when width is specified

Width:

Integer value or * that specifies minimum field width. The result is padded with space characters (by default), if required, on the left when right-justified, or on the right if left-justified. In the case when * is used, the width is specified by an additional argument of type int. If the value of the argument is negative, it results with the - flag specified and positive field width.

Precision:

. followed by integer number or * that specifies precision of the conversion. In the case when * is used, the precision is specified by an additional argument of type int. If the value of this argument is negative, it is ignored.

Length:

Character Description
hh char
h short
l long
ll long-long
L long double
z sizue_t
j intmax_t
t ptrdiff_t

Specifier:

specifier Output Example
a Floating point, hexadecimal, lowercase -0xc.90fep-2
A Floating point, hexadecimal, uppercase -0XC.90FEP-2
c Character c
d, i integer 251
e Mantissa/exponent (scientific), lowercase 2.5121e+2
E Mantissa/exponent (scientific), uppercase 2.5121E+2
f Floating point, decimal, lowercase 2.32
F Floating point, decimal, uppercase 2.32
g Floating point, shortest representation (%e or %f) 512.23
G Floating point, shortest representation (%E or %F) 512.23
n Nothing
o Octal 320
p Pointer address b2010020
s String my str
u Decimal integer 93535
x Integer, hexadecimal, lowercase 3ab
X Integer, hexadecimal, uppercase 3AB
% %% to output % %

Examples:

Format: Parameters: Output:
"Characters:\t%c %%\n" 65 "Characters: A %"
"Decimal:\t%i %d %.6i %i %.0i %+i %u\n" 1, 2, 3, 0, 0, 4, -1 "Decimal: 1 2 000003 0 +4 4294967295"
"Hexadecimal:\t%x %x %X %#x\n" 5, 10, 10, 6 "Hexadecimal: 5 a A 0x6"
"Octal:\t%o %#o %#o\n" 10, 10, 4 "Octal: 12 012 04"
"Rounding:\t%f %.0f %.32f\n" 1.5, 1.5, 1.3 "Rounding: 1.500000 2 1.30000000626"
"Padding:\t%05.2f %.2f %5.2f\n" 1.5, 1.5, 1.5 "Padding: 01.50 1.50 1.50"
"Scientific:\t%E %e\n" 1.5, 1.5 "Scientific: 1.500000E+00 1.500000e+00"
"Hexadecimal:\t%a %A\n" 1.5, 1.5 "Hexadecimal: 0x1.8p+0 0X1.8P+0"

Example: Parse a string with scanf:

public static int main (string[] args) {
string data = "7-19-abc";
int seg1 = -1;
int seg2 = -1;
int seg3 = -1;

// Output: ``7, 19, -1``
data.scanf ("%d-%d-%d", &seg1, &seg2, &seg3);
print ("%d, %d, %d\n", seg1, seg2, seg3);
return 0;
}

valac --pkg glib-2.0 string.scanf.vala

Parameters:

format

character string specifying how to read the input (See GLib.FileStream.printf)

...

receiving arguments