Skip to content

qxov/ezprintf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

ezprintf

A tiny, header-only C library that provides explicit format-argument binding for standard printf using preprocessor tuples.

Instead of writing a single, detached format string followed by a separate list of arguments, ezprintf allows you to associate format specifiers directly with their corresponding values using short, type-aware macros (like _i32(10) or _x16("04", 123)).

Why?

In standard C, keeping a long printf format string in sync with its arguments is error-prone and hard to read, especially when dealing with fixed-width integers and PRI* macros from <inttypes.h>.

Standard way:

printf("Values: %" PRIu8 " %0*" PRIx32 "\n", 255, 8, 0xdead);

With ezprintf:

ezprintf("Values: ", _u8(255), " ", _x32("0*", 8, 0xdead), "\n");

It makes it immediately clear which format belongs to which argument.

How It Works

At compile time, ezprintf processes the arguments as preprocessor tuples:

  1. Bare string literals (like "text") are automatically wrapped into tuples.
  2. The MAP macro separates all format fragments from their arguments.
  3. String literal concatenation merges all format fragments into a single string.
  4. The remaining arguments are unpacked and passed directly into a standard printf call.

Usage Example

#include <stdio.h>
#include "ezprintf.h"

int main(void) {
    // 1. Explicit binding for fixed-width types
    ezprintf("test ", _i32(10), "\n");
    // Expands to: printf("test %" PRId32 "\n", 10);

    // 2. Handling custom formatting flags (width and padding)
    ezprintf(_x16("04", 123), "\n");
    // Expands to: printf("%04" PRIx16 "\n", 123);

    // 3. Mixing with standard printf tuple syntax if needed
    ezprintf(("Hello, %s!\n", "world"));
    // Expands to: printf("Hello, %s!\n", "world");

    return 0;
}

Supported Macros

The library provides wrappers for all standard PRI* macros from <inttypes.h> for signed/unsigned decimals, octals, hexadecimals, fast/least types, and pointers:

  • _d8(...) to _d64(...), _dPTR(...), _dMAX(...)
  • _i8(...) to _i64(...), _iPTR(...), _iMAX(...)
  • _u8(...) to _u64(...), _uPTR(...), _uMAX(...)
  • _x8(...) to _x64(...), _xPTR(...), _xMAX(...)
  • _X8(...) to _X64(...), _XPTR(...), _XMAX(...)
  • _o8(...) to _o64(...), _oPTR(...), _oMAX(...)

Dependencies & Compatibility

  • C99 or later (requires variadic macros).
  • GCC or Clang (relies on the GNU extension , ##__VA_ARGS__ for empty argument elimination).
  • map-macro.h by William R. Swanson (included in the repository).

License

This is free and unencumbered software released into the public domain. For details, see the header or UNLICENSE.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages