Formatted output
#include <flint/flint.h>int flint_printf(const char * format, ...)sourceint flint_fprintf(FILE * fs, const char * format, ...)sourceint flint_sprintf(char * s, const char * format, ...)sourceint flint_snprintf(char * s, size_t n, const char * format, ...)sourceint flint_vprintf(const char * format, va_list vlist)sourceint flint_vfprintf(FILE * fs, const char * format, va_list vlist)sourceint flint_vsprintf(char * s, const char * format, va_list vlist)sourceint flint_vsnprintf(char * s, size_t n, const char * format, va_list vlist)sourceVersions of printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf and vsnprintf that also accept the length modifier w for ulong and slong (%wu, %wd, %wx, ...) and conversions %{type} that print FLINT and GMP objects, such as %{fmpz}, %{fmpz_poly} or %{arb}. They return the number of characters written, or a negative value on error.
flint_printf and its variants work like the C library's printf family, with two extensions: the length modifier w for the word types ulong and slong, and the conversion %{type}, which prints a FLINT or GMP object. Ordinary conversions such as %d, %s, %f and %p, with flags, field widths and precisions, are passed to the C library and behave as usual.
Words#
%wd, %wi, %wu, %wx, %wX and %wo print an slong or a ulong with the conversion d, i, u, x, X or o. Since ulong is unsigned long on most platforms but unsigned long long on 64-bit Windows, %lu is not portable, while %wu is. The macros WORD_FMT and UWORD serve the same purpose for the standard printf and for constants.
FLINT types#
A conversion %{type} consumes one argument, the object to print, passed as for any function taking that type (for example an fmpz_t, which decays to a pointer). The supported types are:
| Conversion | Argument | Prints |
|---|---|---|
%{ulong}, %{slong} | ulong, slong | the integer |
%{fmpz}, %{fmpq} | fmpz_t, fmpq_t | the number, a fraction as p / q |
%{mpz}, %{mpq} | mpz_t, mpq_t | the number |
%{mag}, %{arf}, %{arb}, %{acb} | mag_t, arf_t, arb_t, acb_t | the number, as by the module's _print function |
%{nmod} | nmod_t | the modulus, as mod n |
%{fmpz_mod_ctx} | fmpz_mod_ctx_t | the modulus |
%{truth} | truth_t | T_TRUE, T_FALSE or T_UNKNOWN |
%{nmod_poly}, %{fmpz_poly}, %{fmpz_mod_poly}, %{fmpq_poly}, %{arb_poly}, %{acb_poly} | the polynomial | the polynomial in x |
%{nmod_mat}, %{fmpz_mat}, %{fmpz_mod_mat}, %{fmpq_mat}, %{arb_mat}, %{acb_mat} | the matrix | the matrix, row by row |
Vectors are printed with %{type*}, which consumes two arguments, the pointer to the first entry and the length as an slong, and prints [a, b, ...]. It is available for ulong (also for vectors of residues of nmod type), slong, fmpz (also for fmpz_mod vectors), fmpq, mag, arf, arb and acb. The length must be an slong: passing an int such as the literal 3 reads garbage on platforms where int is narrower than slong, so write (slong) 3.
Elements of generic rings are printed with %{gr} (element and context), %{gr*} (pointer, length and context), %{gr_poly}, %{gr_ore_poly} and %{gr_mat} (object and context), and the context itself with %{gr_ctx}. The context argument always comes after the object, so printing an element and its ring takes the context twice: flint_printf("%{gr} in %{gr_ctx}", x, ctx, ctx).
Example#
#include <flint/flint.h>
#include <flint/fmpz.h>
#include <flint/fmpq.h>
#include <flint/nmod.h>
#include <flint/fmpz_poly.h>
#include <flint/nmod_poly.h>
#include <flint/arb.h>
#include <flint/gr.h>
int main(void)
{
ulong u = UWORD_MAX;
slong s = WORD_MIN;
fmpz_t a;
fmpq_t q;
nmod_t mod;
fmpz v[3];
slong w[4] = {3, 1, 4, 1};
fmpz_poly_t f;
nmod_poly_t g;
arb_t x;
gr_ctx_t ZZ;
char buf[32];
int n;
fmpz_init(a);
fmpz_ui_pow_ui(a, 3, 50);
fmpq_init(q);
fmpq_set_si(q, -22, 7);
nmod_init(&mod, 17);
fmpz_init_set_si(v + 0, 1);
fmpz_init_set_si(v + 1, -2);
fmpz_init_set_si(v + 2, 3);
fmpz_poly_init(f);
fmpz_poly_set_str(f, "3 1 0 -2");
nmod_poly_init(g, 17);
nmod_poly_set_coeff_ui(g, 2, 5);
nmod_poly_set_coeff_ui(g, 0, 1);
arb_init(x);
arb_set_ui(x, 2);
arb_sqrt(x, x, 30);
gr_ctx_init_fmpz(ZZ);
flint_printf("%wu %wd %wx\n", u, s, u);
flint_printf("%{ulong} %{slong}\n", u, s);
flint_printf("a = %{fmpz}, q = %{fmpq}, %{nmod}\n", a, q, mod);
flint_printf("v = %{fmpz*}, w = %{slong*}\n", v, (slong) 3, w, (slong) 4);
flint_printf("f = %{fmpz_poly}, g = %{nmod_poly}\n", f, g);
flint_printf("x = %{arb}\n", x);
flint_printf("%{gr} in %{gr_ctx}\n", a, ZZ, ZZ);
flint_printf("%-8s|%5.2f|%{fmpz}|\n", "text", 3.14159, v + 1);
n = flint_snprintf(buf, sizeof(buf), "%{fmpz}", a);
flint_printf("%d characters: %s\n", n, buf);
gr_ctx_clear(ZZ);
arb_clear(x);
nmod_poly_clear(g);
fmpz_poly_clear(f);
fmpz_clear(v + 0);
fmpz_clear(v + 1);
fmpz_clear(v + 2);
fmpq_clear(q);
fmpz_clear(a);
return 0;
}18446744073709551615 -9223372036854775808 ffffffffffffffff 18446744073709551615 -9223372036854775808 a = 717897987691852588770249, q = -22 / 7, mod 17 v = [1, -2, 3], w = [3, 1, 4, 1] f = -2 * x^2 + 1, g = 5 * x^2 + 1 x = [1.41421 +/- 3.57e-6] 717897987691852588770249 in Integer ring (fmpz) text | 3.14|-2| 24 characters: 717897987691852588770249
Limitations#
- Flags, field widths and precisions are not supported for
%{type}conversions:%5{slong}is printed literally, like an unknown type name such as%{foo}, and no argument is consumed. - Formats with a
*width or precision mixed with unusual combinations of flags (such as"%* p") may be parsed differently from the C library. %nis not supported and is not reported as an error.- The compiler cannot check the arguments against the format string, so mismatched types are not diagnosed.
Guides#
- Branch cuts (
acb) - Getting started with acb (
acb) - Using acb_hypgeom (
acb_hypgeom) - Taylor series of complex functions (
acb_poly) - Elementary functions (
arb) - Getting started with arb (
arb) - Getting started with arb_mat (
arb_mat) - Power series and Taylor expansions (
arb_poly) - Rounding, precision and the inexact flag (
arf) - Using ca (
ca) - Decimal ball arithmetic (
decimal) - Getting started with decimal arithmetic (
decimal) - Using dfloat (
dfloat) - Characters in the Conrey numbering (
dirichlet) - Building and printing expressions (
fexpr) - Multiplying integers and polynomials (
fft) - Arithmetic in transform space (
fft_small) - Where FLINT uses fft_small (
fft_small) - Canonical form and the fmpq interface (
fmpq) - Getting started with fmpq_poly (
fmpq_poly) - Getting started with fmpz (
fmpz) - The fmpz representation (
fmpz) - Exact linear algebra over the integers (
fmpz_mat) - Getting started with fmpz_mod_poly (
fmpz_mod_poly) - Getting started with fmpz_mpoly (
fmpz_mpoly) - Getting started with fmpz_poly (
fmpz_poly) - Representation and arithmetic of rational functions (
fmpz_poly_q) - Getting started with finite fields (
fq_default) - Getting started with fq_nmod_mpoly (
fq_nmod_mpoly) - Getting started with generic rings (
gr) - Implementing a ring (
gr) - Getting started (
gr_dft) - Linear algebra over general and inexact rings (
gr_mat) - Getting started with multivariate polynomials (
gr_mpoly) - Getting started with gr_poly (
gr_poly) - Working with power series rings (
gr_series) - Two-word arithmetic with longlong.h (
longlong) - Portable vec4d code (
machine_vectors) - Computing bounds with mag (
mag) - Computing with mp_real balls (
mp_real) - Fixed-point kernels (
mp_real) - Getting started with nf_elem (
nf_elem) - Getting started with nfloat (
nfloat) - Basic arithmetic modulo n (
nmod) - Computing discrete logarithms (
nmod) - Using Montgomery arithmetic (
nmod) - Linear algebra modulo n (
nmod_mat) - Getting started with nmod_mpoly (
nmod_mpoly) - Getting started with nmod_poly (
nmod_poly) - Computing modulo a fixed polynomial (
nmod_poly) - Precision in the padic module (
padic) - Getting started with padic_radix (
padic_radix) - Computing in the class group (
qfb) - Using qqbar (
qqbar) - Arithmetic in radix b^e (
radix) - Using the thread pool directly (
thread_pool)
The FLINT team, “Formatted output (flint.h),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/flint/printf.html (generated 2026-10-20).
@misc{flint-doc-flint-printf,
author = {{The FLINT team}},
title = {{Formatted output (flint.h)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/flint/printf.html},
note = {Generated 2026-10-20}
}