# Printf vs. Format?

**URL:** <https://discuss.ocaml.org/t/printf-vs-format/14130>\
**Category:** Learning\
**Tags:** pretty-printing\
**Created:** [February 20, 2024, 10:22am UTC](https://discuss.ocaml.org/t/printf-vs-format/14130 "2024-02-20T10:22:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ninjaaron/32/1885_2.png) [@ninjaaron](https://discuss.ocaml.org/u/ninjaaron)\
**Post date:** [February 20, 2024, 10:22am UTC](https://discuss.ocaml.org/t/printf-vs-format/14130/1 "2024-02-20T10:22:42Z")

</div>

I know there are projects like Fmt and ppx derivers that simplify the generation of pretty printers for OCaml objects, but in the general interest of knowing things, today I’m trying to discover the Stdlib way of doing such things.

I am generally familiar with the `Printf` module and use it happily. Today, I looked more deeply into the `Format` module and I discover that it is more or less the same thing, but with a lot of extra boxes and breaks. Seems a little over-complicated, but I’m sure all these “boxes” are useful when you need them, and these features seem to be opt-in.

I also discoverd `"%a"` today, and this is a very good discovery. It seems have a similar—but not _exactly_ the same—meaning within the contexts of Printf and Format.

So my general observation is this:

- `Printf` and `Format` are two libraries that do almost the same thing in almost the same way, but are nonetheless incompatible for most purposes.

I have several questions:

- Is this observation correct?
- If so, is there a compatibility shim between `Printf` and `Format`?
- What is the preferred way to create pretty printers for my own types? I’ve been using Printf happily for a a while, but the more I look into it, the more it seems like this is not the “blessed” path; i.e. that `pp` functions should be created in terms of `Format`.

And a side thingy: how do I plug my pretty printers into the toplevel/utop?

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [February 20, 2024, 11:11am UTC](https://discuss.ocaml.org/t/printf-vs-format/14130/2 "2024-02-20T11:11:54Z")

</div>

> [@ninjaaron](#):
>
> `Printf` and `Format` are two libraries that do almost the same thing in almost the same way, but are nonetheless incompatible for most purposes.

> [@ninjaaron](#):
>
> Is this observation correct?

Not quite: `Printf` is an output library similar in scope to the IO of the standard C library, its main feature is the support for “formatting strings” which contain placeholders which can be replaced by the textual representation of various arguments. `Format`, on the other hand, is a _pretty-printing_ library: it will introduce line breaks and indentation to make sure that your output looks “nice” (the exact line breaks and indentation introduced depends on the hints specified by using “boxes”). In other words, one can easily use `Format` in place of `Printf` (simply by not introducing any boxing annotations), but not the other way around.

To learn more about `Format`:

- [Using the Format module](https://caml.inria.fr/resources/doc/guides/format.en.html)

May also be of interest:

- [A note about the performance of Printf and Format | LexiFi](https://www.lexifi.com/blog/ocaml/note-about-performance-printf-and-format/)

Cheers,  
Nicolas

---

<div class="post-metadata">

**Author:** ![yawaramin](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/yawaramin/32/3384_2.png) [@yawaramin](https://discuss.ocaml.org/u/yawaramin)\
**Post date:** [February 20, 2024, 2:45pm UTC](https://discuss.ocaml.org/t/printf-vs-format/14130/3 "2024-02-20T14:45:27Z")

</div>

> [@ninjaaron](#):
>
> how do I plug my pretty printers into the toplevel/utop?

Use the `#install_printer` utop pragma.
