printf with variable field width and precision

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

Looking through the WinAVR docs there is no mention of the printf supporting variable field width and varable precision. Is this so or is it overlooked in the docs. I did try a quick test but it failed so I assume it isn't supported.

Regards, Mike.

-=mike=-

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

not sure what you mean by variable field width. Are you saying you want to change the width of the field at runtime? If so, then no there is no direct way to do this. The solution is to use something like sprintf() to generate your format string, specifying the widths you desire, and then using this format string in your actual printf() statment.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

He's talking about using '*' in the width or precision parts of the field specifiers. I haven't seen it in the avr-libc documentation so assume (I know!) that it's not supported. But a squint at the vsprintf source would confirm the situation.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
He's talking about using '*' in the width or precision parts of the field specifiers.

Yes, thats right - its in the 'standard' printf - just pass the ints in the right order in the va_args part. It would have been useful for a project I've just finished.

Cheers.

-=mike=-

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

crap... forgot all about that... never had a need to use it.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Workaround: sprintf() your desired format string into a char
buffer, and then use it as the actual format string.

I could give it a try implementing the feature, it shouldn't
be too hard. The question is how much bloat it will cause,
and it all boils down that there's no simple way to combine
all the optional printf features. Having to use a separate
library for each combination of printf() options is quite
cumbersome.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.