r/C_Programming • u/ByMeno • Jul 19 '26
Small C89 printing library with a custom formatting pipeline
I wanted to make a lightweight printing library for C89 without using a format string parser like printf.
The main idea is using a context-based pipeline system:
file_print(stdout,
arg_str_lit("Value: ")
arg_dec(value)
arg_str_lit("\n")
);The arg_* macros expand into small writing operations that share a print context. Each operation returns a state, allowing the chain to continue or stop when an error happens.
Some features:
- C89 compatible
- Single-header style (
PRINT_IMPLEMENTATION) Output to:
FILE *- fixed buffers
- custom string targets
Integer formatting:
- decimal
- hexadecimal
- octal
- Floating point formatting (in a basic level)
- Optional
printfbackend - Optional removal of
string.h - Configurable output functions
The implementation is built around a context:
struct {
type;
target;
written;
status;
} print_ctx;and all writers operate on that instead of knowing where the output goes.
I know this is probably not something that replaces printf (which is a whole world by itself and extremely powerful, especially for runtime formatting), but I was interested in exploring what a small C89-friendly formatting API could look like without variadic functions or a format string parser.
I would appreciate feedback.
github: https://github.com/byfanes/print.h/
codeberg: https://codeberg.org/fanes/print.h
•
u/AutoModerator Jul 19 '26
Hi /u/ByMeno,
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.
2
u/skeeto Jul 19 '26
Neat project! Quite configurable. Though I don't like the unconditional
stdio, as I expect the point of such a library is stdio avoidance. Also the use of such generic-named reserved identifiers (ex.__buf_print_set,_PRINT_CTX_BUF) is a little concerning, though. That's not so different than what an implementation might choose.Here's a bit of UB and a buffer overflow at once:
Then:
I was curious how it would handle these edge cases and found it didn't.