r/C_Programming • u/ef3d0c3e • Sep 29 '26
Project I built a modern formatting library
https://github.com/ef3d0c3e/formatHey, a while ago I wanted a nicer alternative to printf. I always enjoyed using the modern python-like formatting libraries: fmt for C++, and rust's format!/pritnln! macros.
So I attempted to port them to C! And I must say, it was quite the challenge. C doesn't have the nicer features that are required to make this work: generics, compile time code generation, better type safety, etc.
But C has macros! Macros let you do all kind of crazy stuff, but most of all, did you know that C macros are Turing-complete? Well they aren't strictly-speaking, but they can be used to create an (almost) infinite loop thanks to recursive evaluation.
Anyway, I'm sharing this project hoping you'll enjoy it and it will inspire you in your usage of macros.
This project is still a work in progress, the main missing feature is float formatting, but so far there is:
- Integers/Strings/Char/Array formatting
- Style and color support for most terminals
- Output to
FILE*,fd, and memory buffer - Flushing & Buffering
- User defined formatter/formatting for custom types
Check it out: https://github.com/ef3d0c3e/format
•
u/github-guard Sep 29 '26
⚠️ GitHub Guard: Trust Warning
One or more repositories scored below the threshold (3), but the post was not removed.
Repository: ef3d0c3e/format
Score: 2/6
5
u/fsteff Sep 29 '26
Interesting.
How does it compare to the normal printf formatter in size?
I may try it out on an embedded project sometimes soon, but will probably wait for float support.
4
u/chengfeng-xie Sep 30 '26
Glad to see modern formatting being ported to C. Somewhat related, not long ago I wrote a proof of concept showing how the
fmtC++ library could be used in C (CE) with the extensible_Genericpattern (Jackson).4
u/skeeto Sep 30 '26
Neat library! The
formatmacro works better than I would have expected. Several tests fail out-of-the-box with sanitizers, and I found more bugs beyond that. All examples were compiled like so:Use-after-free from ASan:
Unreachable hit due to an unset variable:
Fix:
Off-by-one in a test macro:
Negative size parameter (ASan):
This only writes 776 bytes due to msicomputed sizes:
EINTRis ignored, and it moves forward with a -1 size, resulting in buffer overflow. Either retry or treat it as a hard error:Write errors are not propagated (connect input to a file):
Incorrect color processing:
More incorrect color processing:
Quick fix:
Improper escape processing, this one straight out of README.md:
Not a bug, but perhaps flushing memory buffers should be silently ignored? I don't object to
asserton misuse, but I expect that such a polymorphic interface would just silently ignore it.Here's all my work in case it's useful:
https://github.com/skeeto/thirdparty-format/commits/master/?author=skeeto
3
u/Waeis Sep 29 '26
Holy macro hell! What's the difference between
FORMAT__EVALandFORMAT__EVAL_OandFORMAT__EVAL_T?2
u/tastygames_official Oct 01 '26
> C doesn't have the nicer features that are required to make this work: generics, compile time code generation, better type safety, etc.
exactly the reasons I love using C. You know exactly what you get and no compile-time wizardry or generic stuff that makes your code unreadable. Although I'd like to know what you mean by "bad type safety"? I feel like higher-level languages have bad type safety since you can do stuff like "any" or have multiple types allowed or overlod functions to handle multiple types or class inheritance that makes actual types unclear.
But yes - if you're used to higher-level string formatting then C can be "tricky". To me it just makes you design with prupose - you need to know how long your strings might potentially be and what types your variables are so you can properly construct the desired string in the desired format. And for that the printf family is more than enough.
1
u/AutoModerator Sep 29 '26
Hi /u/ef3d0c3e,
Your submission in r/C_Programming was filtered because it links to a git project.
You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.
While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.