Author Topic: I hate C printf format specifiers. Alternatives?  (Read 20672 times)

0 Members and 1 Guest are viewing this topic.

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: I hate C printf format specifiers. Alternatives?
« Reply #50 on: September 03, 2015, 05:34:56 am »
This is because the location of const can cause confusion for many programmers.
https://en.wikipedia.org/wiki/Const_(computer_programming)#Pointers_and_references

Printf doesn't care where the format string comes from. You can generate multiple tables, for each language, of format strings which you can select. And printf just delivers. You can even have the user enter one at runtime. It is unfortunate printf can not dereference pointers, if it could it would be another bit more helpful displaying templated layouts.
« Last Edit: September 03, 2015, 05:37:48 am by Jeroen3 »
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: I hate C printf format specifiers. Alternatives?
« Reply #51 on: September 03, 2015, 05:49:21 am »
Quote
But not so optimal
I was thinking of mentioning that things like the "picture" based formatting have to be pretty expensive at runtime.  Something like ForTran can do a lot of the format processing at compile time because the feature is part of the language, but library-based functions as required by C are a lot harder to do.  The current printf() seems like it's a pretty reasonable compromise (although, I think printf() can be a lot "heavier" than some of the simple versions would lead you to believe.  I think I've seen printf implementations that end up calling malloc(), and running through the formatting twice for one reason or another.)
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: I hate C printf format specifiers. Alternatives?
« Reply #52 on: September 04, 2015, 10:03:28 am »
yet an other C++ alternative: C++format  :-+
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: I hate C printf format specifiers. Alternatives?
« Reply #53 on: September 04, 2015, 10:05:50 am »
the new strategy uses the variadic template method.
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 26896
  • Country: nl
    • NCT Developments
Re: I hate C printf format specifiers. Alternatives?
« Reply #54 on: September 04, 2015, 02:12:43 pm »
There is no requirement that the string passed to printf or fprintf is a constant string literal.
True but as I wrote before some compilers enforce that a format specifier is a string literal and not a pointer to a variable to prevent format string attacks.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf