I'd like to convert multiple numbers into some representation and then use the flags, width and precision of *printf()
specifiers. Preference would be to avoid global or static
buffers. The key problem appears to be is how to provide a char[]
for each of the converted numbers?
fprintf(ostream, "some_format", foo(int_a, base_x), foo(int_b, base_y), ...);
How to use C11 compound literals to solve this?
How to use C99 (or later) compound literals to solve this?
C99
C11introduced compound literals which allow not only a complicated initialized structure, but also an "in-line" variable.Code can call a conversion function and pass in a new buffer
(char [UTOA_BASE_N]){0}
per each function call allowing the function to return that same buffer, now written as needed that is still within its lifetime. The returned string is then printed using various flags, width and precision available to the"%s"
specifier.Output
Ref: Is there a printf converter to print in binary format? has a number of answers but none of them allow the simple memory management (no
static
) of the above with access tofprintf()
flags width, precision and use the full range of the number.This is an Answer your own question answer.