manpagez: man pages & more
info gmp
Home | html | info | man
[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

11.1 Formatted Input Strings

gmp_scanf and friends accept format strings similar to the standard C scanf (see Formatted Input in The GNU C Library Reference Manual). A format specification is of the form

% [flags] [width] [type] conv

GMP adds types ‘Z’, ‘Q’ and ‘F’ for mpz_t, mpq_t and mpf_t respectively. ‘Z’ and ‘Q’ behave like integers. ‘Q’ will read a ‘/’ and a denominator, if present. ‘F’ behaves like a float.

GMP variables don’t require an & when passed to gmp_scanf, since they’re already “call-by-reference”. For example,

/* to read say "a(5) = 1234" */
int   n;
mpz_t z;
gmp_scanf ("a(%d) = %Zd\n", &n, z);

mpq_t q1, q2;
gmp_sscanf ("0377 + 0x10/0x11", "%Qi + %Qi", q1, q2);

/* to read say "topleft (1.55,-2.66)" */
mpf_t x, y;
char  buf[32];
gmp_scanf ("%31s (%Ff,%Ff)", buf, x, y);

All the standard C scanf types behave the same as in the C library scanf, and can be freely intermixed with the GMP extensions. In the current implementation the standard parts of the format string are simply handed to scanf and only the GMP extensions handled directly.

The flags accepted are as follows. ‘a’ and ‘'’ will depend on support from the C library, and ‘'’ cannot be used with GMP types.

*read but don’t store
aallocate a buffer (string conversions)
'grouped digits, GLIBC style (not GMP types)

The standard types accepted are as follows. ‘h’ and ‘l’ are portable, the rest will depend on the compiler (or include files) for the type and the C library for the input.

hshort
hhchar
jintmax_t or uintmax_t
llong int, double or wchar_t
lllong long
Llong double
qquad_t or u_quad_t
tptrdiff_t
zsize_t

The GMP types are

Fmpf_t, float conversions
Qmpq_t, integer conversions
Zmpz_t, integer conversions

The conversions accepted are as follows. ‘p’ and ‘[’ will depend on support from the C library, the rest are standard.

ccharacter or characters
ddecimal integer
e E f g Gfloat
iinteger with base indicator
ncharacters read so far
ooctal integer
ppointer
sstring of non-whitespace characters
udecimal integer
x Xhex integer
[string of characters in a set

e’, ‘E’, ‘f’, ‘g’ and ‘G’ are identical, they all read either fixed point or scientific format, and either upper or lower case ‘e’ for the exponent in scientific format.

C99 style hex float format (printf %a, see section Format Strings) is always accepted for mpf_t, but for the standard float types it will depend on the C library.

x’ and ‘X’ are identical, both accept both upper and lower case hexadecimal.

o’, ‘u’, ‘x’ and ‘X’ all read positive or negative values. For the standard C types these are described as “unsigned” conversions, but that merely affects certain overflow handling, negatives are still allowed (per strtoul, see Parsing of Integers in The GNU C Library Reference Manual). For GMP types there are no overflows, so ‘d’ and ‘u’ are identical.

Q’ type reads the numerator and (optional) denominator as given. If the value might not be in canonical form then mpq_canonicalize must be called before using it in any calculations (see section Rational Number Functions).

Qi’ will read a base specification separately for the numerator and denominator. For example ‘0x10/11’ would be 16/11, whereas ‘0x10/0x11’ would be 16/17.

n’ can be used with any of the types above, even the GMP types. ‘*’ to suppress assignment is allowed, though in that case it would do nothing at all.

Other conversions or types that might be accepted by the C library scanf cannot be used through gmp_scanf.

Whitespace is read and discarded before a field, except for ‘c’ and ‘[’ conversions.

For float conversions, the decimal point character (or string) expected is taken from the current locale settings on systems which provide localeconv (see Locales and Internationalization in The GNU C Library Reference Manual). The C library will normally do the same for standard float input.

The format string is only interpreted as plain chars, multibyte characters are not recognised. Perhaps this will change in the future.


[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on March 31, 2014 using texi2html 5.0.

© manpagez.com 2000-2022
Individual documents may contain additional copyright information.