Nanoprintf - A tiny embeddable printf replacement written in C89/C99

About

nanoprintf is an implementation of snprintf and vsnprintf for embedded systems that, when fully enabled, aims for C11 standard compliance.

nanoprintf makes no memory allocations, uses less than 100 bytes of stack and is implemented within a single header file. It is written in C89 for maximal compiler compatibility and compiles cleanly at the highest warning levels on clang, gcc, and msvc in both 32- and 64-bit modes.

Motivation

  • tinyprintf doesn’t print floating point values.
  • printf” defines the actual standard library printf symbol, which isn’t always what you want. It stores the final converted string (with padding and precision) in a temporary buffer, which makes supporting longer strings more costly. It also doesn’t support the %n “write-back” specifier.
  • Also, no embedded-friendly printf projects that I could find are both in the public domain and have single-file implementations.

Overview

nanoprintf makes no memory allocations and uses less than 100 bytes of stack. Compiling with all optional features disabled yields ~2.4KB of ARM Cortex-M object code, and compiling with all optional features enabled is closer to 5KB.

nanoprintf is a single header file in the style of the stb libraries. The rest of the repository is tests and scaffolding and not required for use.

nanoprintf is written in C89 for maximal compiler compatibility, and compiles cleanly at the highest warning levels on clang, gcc, and msvc in both 32- and 64-bit modes. C99 or C++11 compilers are required (for uint64_t and other types) if floating point conversion or large modifiers are enabled, but pull requests that fix this are welcome!

Details

nanoprintf does include C standard headers but only uses them for C99 types and argument lists; no calls are made into stdlib / libc, with the exception of any internal double-to-float conversion ABI calls your compiler might emit. As usual, some Windows-specific headers are required if you’re compiling natively for msvc.

nanoprintf is statically configurable so users can find a balance between size, compiler requirements, and feature set. Floating point conversion, “large” length modifiers, and size write-back are all configurable and are only compiled if explicitly requested, see Configuration for details.

Author

image
http://cnicholson.net/

1 Like