Home

Awesome

Standalone printf/sprintf formatted printing function library

tests passing GitHub license Github Bug-type issues <sup>Parent repository: </sup>Github issues (original repo)

<!-- Can't use Travis - they stopped offering free builds [![Build Status](https://travis-ci.com/eyalroz/printf.svg?branch=master)](https://travis-ci.com/eyalroz/printf) --> <!-- No releases yet... [![Github Releases](https://img.shields.io/github/release/eyalroz/printf.svg)](https://github.com/mpaland/eyalroz/releases)-->
Table of contents
<sub>Highlights, design goals and the fork<br>Using the printf library in your project<br> - CMake options and preprocessor definitions<br>Library API<br> - Implemented functions<br> - Supported Format Specifiers<br> - Return Value<br>Contributing<br>License </sub>

This is a small but fully-loaded implementation of C's formatted printing family of functions. It was originally designed by Marco Paland, with the primary use case being in embedded systems - where these functions are unavailable, or when one needs to avoid the memory footprint of linking against a full-fledged libc. The library can be made even smaller by partially excluding some of the supported format specifiers during compilation. The library stands alone, with No external dependencies.

It is a fork of the original mpaland/printf repository by Marco Paland, with multiple bug fixes and a few more features.

Highlights, design goals and the fork

If you use a typical libc's sprintf() implementation (or similar function), you are likely to pull in a lot of unwanted library definitions and can bloat code size - typically by as much as 20 KiB. Now, there is a boatload of so called 'tiny' printf()-family implementations around. So why this one? Or rather, why mpaland/printf, and then why this fork?

Well, Marco tried out many of the available printf() implementations, but was disappointed: Some are not thread-safe; some have indirect dependencies on libc or other libraries, making them inconvenient to build and larger when compiled; some only offer extremely limited flag and specifier support; and some produce non-standard-compiled output, failing many tests no found in the repository's test suite.

Marco therefore decided to write his own implementation, with the following goals in mind (I've dropped a few relative to his original description):

Marco's repository upheld most of these goals - but did not quite make it all of the way. As of mid-2021, it still had many C-standard-non-compliance bugs; the test suite was quite lacking in coverage; some goals were simply discarded (like avoiding global/local-static constants) etc. The repository had become quite popular, but unfortunately, Marco had been otherwise preoccupied; he had not really touched the code in the two years prior; many bug reports were pending, and so were many pull requests from eary adopters who had fixed some of the bugs they had encountered.

The author of this fork was one of the latercomer bug-reporters-and-PR-authors; and when noticing nothing was moving forward, decided to take up the same goals (sans the discarded ones); and integrate the existing forks and available PRs into a single "consensus fork" which would continue where Marco had left off. Along the way, numerous other issues were observed; the build system was improved; the test suite streamlined and expanded; and other contributors also lent a hand (especially @mickjc750). We are now very close to fully realizing the project goals.

Using the printf library in your project

Use involving CMake:

  1. Use CMake to configure, build and install the library. Then, in another CMake project, use find_package(printf) and make sure the library's install location is in CMake's package search path.

  2. Use CMake to configure and build the library. This results in the following files:

    • An object code library file (named printf.a, or printf.so, or printf.dll depending on your platform and choice of static vs dynamic linking)
    • A header file named printf.h
    • (Not strictly necessary) An optional extra header file printf_config.h with the build configuration details.

    Now, in your project, include printf.h and link against the library file, you're all set: There are no dependencies to satisfy or keep track of.

  3. Use CMake's FetchContent module to obtain the project source code and make it part of your own project's build, e.g.:

    FetchContent_Declare(printf_library
        GIT_REPOSITORY https://github.com/eyalroz/printf.git
        GIT_TAG v12.34.45 # Replace this with a real available version
    )
    FetchContent_MakeAvailable(printf_library)
    

Use not involving CMake:

  1. Copy printf.c and printf.h into your own project, and compile the source however you see fit. Remember that the library requires compilation with the C99 language standard enabled.
  2. Include the contents of printf.c into your own code - which can be either C or C++. Remember, though, the library is written in the "intersection" of C99 and C++11, so older-standard C programs may not just accept it.

Whichever way you choose to use the library:

Finally, if you've started using the library in a publicly-available (FOSS or commercial) project, please consider emailing @eyalroz, or open an issue, to announce this.

CMake options and preprocessor definitions

Options used both in CMake and in the library source code via a preprocessor define:

Option nameDefaultDescription
PRINTF_ALIAS_STANDARD_FUNCTION_NAMESNOAlias the standard library function names (printf(), sprintf() etc.) to the library's functions.<br>Note: If you build the library with this option turned on, you must also have written<br>#define PRINTF_ALIAS_STANDARD_FUNCTION_NAMES 1<br>before including the printf.h header.
PRINTF_INTEGER_BUFFER_SIZE32ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number including leading zeros, normally 32 is a sufficient value. Created on the stack.
PRINTF_DECIMAL_BUFFER_SIZE32ftoa (float) conversion buffer size. This must be big enough to hold one converted float number including leading zeros, normally 32 is a sufficient value. Created on the stack.
PRINTF_DEFAULT_FLOAT_PRECISION6Define the default floating point precision
PRINTF_MAX_INTEGRAL_DIGITS_FOR_DECIMAL9Maximum number of integral-part digits of a floating-point value for which printing with %f uses decimal (non-exponential) notation
PRINTF_SUPPORT_DECIMAL_SPECIFIERSYESSupport decimal notation floating-point conversion specifiers (%f, %F)
PRINTF_SUPPORT_EXPONENTIAL_SPECIFIERSYESSupport exponential floating point format conversion specifiers (%e, %E, %g, %G)
SUPPORT_MSVC_STYLE_INTEGER_SPECIFIERSYESSupport the 'I' + bit size integer specifiers (%I8, %I16, %I32, %I64) as in Microsoft Visual C++
PRINTF_SUPPORT_WRITEBACK_SPECIFIERYESSupport the length write-back specifier (%n)
PRINTF_SUPPORT_LONG_LONGYESSupport long long integral types (allows for the ll length modifier and affects %p)

Within CMake, these options lack the PRINTF_ prefix.

CMake-only options:

Option nameDefaultDescription
PRINTF_BUILD_STATIC_LIBRARYNOBuild a library out of a shared object (dynamically linked at load time) rather than a static one (baked into the executables you build)

Source-only options:

Option nameDefaultDescription
PRINTF_INCLUDE_CONFIG_HNOTriggers inclusing by printf.c of a "printf_config.h" file, which in turn contains the values of all of the CMake-and-preprocessor options above. A CMake build of the library uses this mechanism to apply the user's choice of options, so it can't have the mechanism itself as an option.

Note: The preprocessor definitions are taken into account when compiling printf.c, not when using the compiled library by including printf.h.

Library API

Implemented functions

The library offers the following, with the same signatures as in the standard C library (plus an extra underscore):

int printf_(const char* format, ...);
int sprintf_(char* s, const char* format, ...);
int vsprintf_(char* s, const char* format, va_list arg);
int snprintf_(char* s, size_t n, const char* format, ...);
int vsnprintf_(char* s, size_t n, const char* format, va_list arg);
int vprintf_(const char* format, va_list arg);

Note that printf() and vprintf() don't actually write anything on their own: In addition to their parameters, you must provide them with a lower-level putchar_() function which they can call for actual printing. This is part of this library's independence: It is isolated from dealing with console/serial output, files etc.

Two additional functions are provided beyond those available in the standard library:

int fctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, ...);
int vfctprintf(void (*out)(char c, void* extra_arg), void* extra_arg, const char* format, va_list arg);

These higher-order functions allow for better flexibility of use: You can decide to do different things with the individual output characters: Encode them, compress them, filter them, append them to a buffer or a file, or just discard them. This is achieved by you passing a pointer to your own state information - through (v)fctprintf() and all the way to your own out() function.

"... but I don't like the underscore-suffix names :-("

You can configure the library to alias the standard library's names, in which case it exposes printf(), sprintf(), vsprintf() and so on.

If you alias the standard library function names, be careful of GCC/clang's printf() optimizations!: GCC and clang recognize patterns such as printf("%s", str) or printf("%c", ch), and perform a "strength reduction" of sorts by invoking puts(stdout, str) or putchar(ch). If you enable the PRINTF_ALIAS_STANDARD_FUNCTION_NAMES option (see below), and do not ensure your code is compiled with the -fno-builtin-printf option - you might inadvertantly pull in the standard library implementation - either succeeding and depending on it, or failing with a linker error. When using printf as a CMake imported target, that should already be arranged for, but again: Double-check.

<br>

Alternatively, you can write short wrappers with your preferred names. This is completely trivial with the v-functions, e.g.:

int my_vprintf(const char* format, va_list va)
{
  return vprintf_(format, va);
}

and is still pretty straightforward with the variable-number-of-arguments functions:

int my_sprintf(char* buffer, const char* format, ...)
{
  va_list va;
  va_start(va, format);
  const int ret = vsprintf_(buffer, format, va);
  va_end(va);
  return ret;
}

Supported Format Specifiers

A format specifier follows this prototype: %[flags][width][.precision][length]type The following format specifiers are supported:

Types

TypeOutput
d or iSigned decimal integer
uUnsigned decimal integer
bUnsigned binary
oUnsigned octal
xUnsigned hexadecimal integer (lowercase)
XUnsigned hexadecimal integer (uppercase)
f or FDecimal floating point
e or EScientific-notation (exponential) floating point
g or GScientific or decimal floating point
cSingle character
sString of characters
pPointer address
nNone; number of characters produced so far written to argument pointer

Notes:

Flags

FlagsDescription
-Left-justify within the given field width; Right justification is the default.
+Forces to precede the result with a plus or minus sign (+ or -) even for positive numbers.<br>By default, only negative numbers are preceded with a - sign.
(space)If no sign is going to be written, a blank space is inserted before the value.
#Used with o, b, x or X specifiers the value is preceded with 0, 0b, 0x or 0X respectively for values different than zero.<br>Used with f, F it forces the written output to contain a decimal point even if no more digits follow. By default, if no digits follow, no decimal point is written.
0Left-pads the number with zeros (0) instead of spaces when padding is specified (see width sub-specifier).

Width Specifiers

WidthDescription
(number)Minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with blank spaces. The value is not truncated even if the result is larger.
*The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Precision Specifiers

PrecisionDescription
.(number)For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.<br>For f and F specifiers: this is the number of digits to be printed after the decimal point. By default, this is 6, and a maximum is defined when building the library.<br>For s: this is the maximum number of characters to be printed. By default all characters are printed until the ending null character is encountered.<br>If the period is specified without an explicit value for precision, 0 is assumed.
.*The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.

Length modifiers

The length sub-specifier modifies the length of the data type.

LengthWith d, iWith u,o,x, XSupport enabled by...
(none)intunsigned int
hhsigned charunsigned char
hshort intunsigned short int
llong intunsigned long int
lllong long intunsigned long long intPRINTF_SUPPORT_LONG_LONG
jintmax_tuintmax_t
zsigned version of size_tsize_t
tptrdiff_tptrdiff_t
I8int8_tuint8_tSUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS
I16int16_tuint16_tSUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS
I32int32_tuint32_tSUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS
I64int64_tuint64_tSUPPORT_MSVC_STYLE_INTEGER_SPECIFIERS

Notes:

Return Value

Upon successful return, all functions return the number of characters written, excluding the terminating NUL character used to end the string. Functions snprintf() and vsnprintf() don't write more than count bytes, including the terminating NUL character ('\0'). Anyway, if the output was truncated due to this limit, the return value is the number of characters that could have been written. Notice that a value equal or larger than count indicates a truncation. Only when the returned value is non-negative and less than count, the string has been completely written with a terminating NUL. If any error is encountered, -1 is returned.

If NULL is passed for the buffer parameter, nothing is written, but the formatted length is returned. For example:

int length = sprintf(NULL, "Hello, world"); // length is set to 12

Contributing

The following assumes Marco Paland's original repository remains mostly-inactive in terms of commits.

  1. Give this repository a :star: (even if you've already starred the original repository).
  2. Create an issue and describe your idea. Make sure it is in line with the library's design goals.
  3. Fork the repository
  4. Create your feature branch (git checkout -b my-new-feature).
  5. Implement your feature/idea; don't forget to make sure all existing tests still pass.
  6. Add new checks or test-cases to the test suite - both for any problems you have identified and for any new functionality you have introduced.
  7. Commit your changes (git commit -a -m "Added some feature")
  8. Publish the branch (git push origin my-new-feature)
  9. Create a new pull request against this repository. Note: Please don't create a PR without a related issue.

I try to attend to issues and PRs promptly.

License

This library is published under the terms of the MIT license.