simple no-float 32-bit printf
Does everything I need it to. Will fix bugs as I come across them.
- Copy
mprint.h
andmprint.c
into your project - In
mprint.c
, replace all the calls toputchar()
with the appropriate one for your platform. In embedded projects, this is commonly a function that outputs data over UART. - Once
putchar()
is replaced, the#include <stdio.h>
line inmprintf.c
should no longer be needed. Feel free to delete it. - If needed, adjust the newline encoding in
print_newline()
. - Done!
- copy these files into an embedded project.
- compile the project.
- measure the size of the
mprintf.o
object file usingsize -A
. - the total size is found from summing all relevant sections together.
Compiles down to 1554 bytes on arm-none-eabi-gcc
with -Os
optimizations. This can be made slightly smaller depending on which functions are actually called.
mprintf is about 450 lines of code (per David A. Wheeler's SLOCCount
).
Max RAM usage can be controlled by changing the printf()
buffer size. Shrinking the buffer saves RAM at the cost of decreasing the maximum string length printf()
can handle.
Supports b, c, d, i, p, s, u, X, x, and % conversion flags, minimum field widths, and 0- +# format flags.
Designed with Cortex-M microcontrollers in mind, but there's no microcontroller-specific code in the file.
I also included testing using Greatest from https://github.com/silentbicycle/greatest. There's a number of tests that compare mprintf against the standard printf.