# Printf with width field as argument

**URL:** <https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685>\
**Category:** New to Julia\
**Tags:** question, formatting\
**Created:** [September 12, 2019, 10:09am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685 "2019-09-12T10:09:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johanmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johanmon/32/10328_2.png) [@johanmon](https://discourse.julialang.org/u/johanmon)\
**Post date:** [September 12, 2019, 10:09am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/1 "2019-09-12T10:09:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2019, 10:36am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/2 "2019-09-12T10:36:14Z")

</div>

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

[https://github.com/JuliaLang/julia/issues/19582](https://github.com/JuliaLang/julia/issues/19582)

I would just truncate the string in Julia before printing.

---

<div class="post-metadata">

**Author:** ![johanmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johanmon/32/10328_2.png) [@johanmon](https://discourse.julialang.org/u/johanmon)\
**Post date:** [September 12, 2019, 10:52am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/3 "2019-09-12T10:52:26Z")

</div>

Thanks.

opens up an opportunity to help 🙂

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2019, 11:03am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/4 "2019-09-12T11:03:38Z")

</div>

There are other issues like this:

[https://github.com/JuliaLang/julia/issues/29077](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.

---

<div class="post-metadata">

**Author:** ![johanmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johanmon/32/10328_2.png) [@johanmon](https://discourse.julialang.org/u/johanmon)\
**Post date:** [September 13, 2019, 9:03am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/5 "2019-09-13T09:03:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 13, 2019, 9:05am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/6 "2019-09-13T09:05:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johanmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johanmon/32/10328_2.png) [@johanmon](https://discourse.julialang.org/u/johanmon)\
**Post date:** [September 13, 2019, 9:19am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/7 "2019-09-13T09:19:30Z")

</div>

Thanks, I’ll start from there.

---

<div class="post-metadata">

**Author:** ![johanmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johanmon/32/10328_2.png) [@johanmon](https://discourse.julialang.org/u/johanmon)\
**Post date:** [September 18, 2019, 9:45am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/8 "2019-09-18T09:45:27Z")

</div>

Added this proposal that implements dynamic width and precision

[https://github.com/JuliaLang/julia/pull/33278](https://github.com/JuliaLang/julia/pull/33278)

---

<div class="post-metadata">

**Author:** ![johanmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johanmon/32/10328_2.png) [@johanmon](https://discourse.julialang.org/u/johanmon)\
**Post date:** [September 18, 2019, 10:36am UTC](https://discourse.julialang.org/t/printf-with-width-field-as-argument/28685/9 "2019-09-18T10:36:48Z")

</div>

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).
