Printf with width field as argument

Tried using a * width specifier with @printf but widthout luck:
julia version 1.1.1

julia> @printf(“hello %*s\n”, 10, “hej”)
ERROR: LoadError: ArgumentError: invalid printf format string: “hello %*s\n”

This works fine in C. How to do it?

Unfortunately, not all features of C printf are supported. You ran into

https://github.com/JuliaLang/julia/issues/19582

I would just truncate the string in Julia before printing.

Thanks.

opens up an opportunity to help :slight_smile:

1 Like

There are other issues like this:

https://github.com/JuliaLang/julia/issues/29077

so if you are interested in getting involved, and want to invest the time in learning the internals of this package, this is indeed a good start.

I’m taking a look at it. It’s some serious macro hacking going on in printf.jl but I have the parsing working and hope to have something that generates the right code.

Note that there is a PR to completely overhaul the implementation of printf: https://github.com/JuliaLang/julia/pull/32859.

1 Like

Thanks, I’ll start from there.

Added this proposal that implements dynamic width and precision

https://github.com/JuliaLang/julia/pull/33278

1 Like

Adding a ’ flag is a bot more work and I don’t know if it should be done. As it is now, everything (I think) that is generated by printf can also be parsed back to an Int or Float. Adding a ’ flag would break this “1,234” is not parsed as an Int. Nor is the ’ flag part of standard C (added in SUSv2, but not recognized by gcc).