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

0 Members and 1 Guest are viewing this topic.

Offline Chris CTopic starter

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
I hate C printf format specifiers. Alternatives?
« on: August 27, 2015, 12:02:25 am »
I just can't seem to remember what all these almost randomly chosen letters do.  And after using a reference chart for the third time today, I wondered, am I alone?  Or are there others like me?  Maybe even someone who has designed an alternative, with user-friendliness in mind?

I know it would be non-standard, but I could still use it for some things.  Googling turned up some possibilities for C++, Haskell, and so on.  But not plain C.
 

Offline Macbeth

  • Super Contributor
  • ***
  • Posts: 2571
  • Country: gb
Re: I hate C printf format specifiers. Alternatives?
« Reply #1 on: August 27, 2015, 12:24:26 am »
Seriously? Nothing could be simpler. More modern stuff uses regular expressions. Try that then go back to the simplicity of printf()
 

Offline TopLoser

  • Supporter
  • ****
  • Posts: 1925
  • Country: fr
Re: I hate C printf format specifiers. Alternatives?
« Reply #2 on: August 27, 2015, 12:38:28 am »
Nothing could be more simple! Give us an example of how you would do it better?!
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2320
  • Country: au
Re: I hate C printf format specifiers. Alternatives?
« Reply #3 on: August 27, 2015, 12:43:52 am »
Conciseness = user friendliness when it comes to coding. You could write one where you go

printFbyChris("I have %DECIMAL(figures=5,show_leading_zeros=true) bananas", ...)

But I think even you would soon go back to

printf("I have %05d bananas", ...)
 

Offline TopLoser

  • Supporter
  • ****
  • Posts: 1925
  • Country: fr
Re: I hate C printf format specifiers. Alternatives?
« Reply #4 on: August 27, 2015, 12:49:23 am »
digits=5 surely?

and so the confusion builds!
 

Offline Zeyneb

  • Regular Contributor
  • *
  • Posts: 238
  • Country: nl
Re: I hate C printf format specifiers. Alternatives?
« Reply #5 on: August 27, 2015, 02:00:50 am »
I do like the old fashioned printf type specifiers. In engineering education I first learned C and later C++. Then I was suggested to use cout for text output. With all that type safety propaganda. I wasn't able to see the benefit of that and simply sticked to printf and it's comrades in stdio.h. For me it was easy to verify the correctness of something that fits into a single line.

I even do goto sometimes in C. No I don’t write spaghetti code. It is easier to follow a few goto’s than a complex while condition for me.
goto considered awesome!
 

Offline codeboy2k

  • Super Contributor
  • ***
  • Posts: 1836
  • Country: ca
Re: I hate C printf format specifiers. Alternatives?
« Reply #6 on: August 27, 2015, 02:27:26 am »
I've used C for over 30 years now, I am quite used to the % format specifiers and I know the common ones like the back my hand, the more esoteric format specifiers I still have to look up.  But once you know them they are quick and concise to write.

However, I am still fond of the COBOL PICTURE clauses, even though I didn't have a COBOL career  :)

For those of us who remember Mark Williams COHERENT, there is a picture() function there, for C.  In 2015, they open-sourced COHERENT and that particular picture() call is available in the libmisc source.

picture(3101.1, "*$ZZZ,ZZZ.99", output_string) yields "$***3,101.10" in output_string.
picture(5.1, "ZZ99", output_string) yields "  05" in output_string. (two spaces and 2 digits)

http://www.nesssoftware.com/home/mwc/manpage.php?page=libmisc (scroll down to Pictures)

The main page is here http://www.nesssoftware.com/home/mwc/
COHERENT source is here : http://www.nesssoftware.com/home/mwc/source.php
 

Offline SL4P

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
  • There's more value if you figure it out yourself!
Re: I hate C printf format specifiers. Alternatives?
« Reply #7 on: August 27, 2015, 03:04:17 am »
I like sprintf() for flexibility and the ability to arbitrarily concatenate formatted strings (without using strcat())
If Chris is using Arduino - problem solved for simplicity, but not flexibility / performance.

OMG - I almost forgot the COBOL picture statement... clumsy, but certainly readable)
« Last Edit: August 27, 2015, 03:07:01 am by SL4P »
Don't ask a question if you aren't willing to listen to the answer.
 

Offline Chris CTopic starter

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
Re: I hate C printf format specifiers. Alternatives?
« Reply #8 on: August 27, 2015, 03:15:35 am »
Heh.  Maybe it is just me.

C is NOT my primary language.  So I see "d", and the first two thoughts that come to my mind is "double precision float".  Or "decimal", which in the contexts I'm more used to, doesn't mean base 10 - it means a number stored much like a string, with no limit on the number of digits or precision.  I know I can use "i" instead, and that makes more sense to me as an integer.  But it seems like everyone else uses "d", so I feel obliged to use it as well.  And then I don't mess with printf for a while, go back to old code and see "d", and get confused all over again.  I try to figure it out without the reference chart, and it goes something like this:

Hmm, "d" is an integer, right?  But wait, "u" is an unsigned integer, so that would make a signed integer "i', and that makes "d"...double?  Arg, gotta check that chart again...

Seriously, WHY did they assign two letters to the same darn thing?  I don't care what anyone says, this is NOT intuitive.

I'm also trying to code for portability to other hardware.  Currently in the middle of the second port, this time from 16 to 32 bit architecture.  Had the foresight to write it from the start using types like int16_t where appropriate, which I typedef'ed to I16 for conciseness.  So most of it's been super easy.  But I'm now getting some stack issues using printf.  In the process of looking into it, I find something that says in this scenario, I really should have been writing:

printf("I have %05" PRIi16 " bananas", ...)

Which is no longer concise, like [rs20]'s original example.  But at least it's totally unambiguous.  And different enough that I no longer feel obliged to use "d", when I really want to use "i".  Yet, if I want those bananas in hex, I have to do this:

printf("I have %05" PRIx16 " bananas", ...)

But what I'll end up doing is this:

printf("I have %05" PRIi16x " bananas", ...)

Remember I typedef'ed int16_t to I16?  That means I'm going to instinctively want to put that "i" in there, right before the "16", every time.  Which makes perfect sense, really, because IT IS STILL AN I16.  That I'm asking for output in hex, has NOTHING to do with the input data type, and should NOT replace or require changes to that convention.

So I can fix this with more defines.  Give myself "PRIi16x", or more likely "priI16X" which fits in better with my existing I16 convention, and whatever else I want to do that is comfortable and intuitive.  In fact, by adding a preprocessing wrapper around printf, I could make it accept any of these:

printf("I have %05" priI16 priX " bananas", ...) // I16, uppercase hex
printf("I have %05" priX priI16 " bananas", ...) // I16, uppercase hex
printf("I have %05" priI16 prix " bananas", ...) // I16, lowercase hex
printf("I have %05" prix priI16 " bananas", ...) // I16, lowercase hex


Which is slightly less concise, but even more intuitive.  Because then I don't even have to remember where in a naming convention the "x" is supposed to go.  I will not be confused on whether the "x" replaces "I" or not, even if I have recently seen someone else using "PRIx16".

And should I ever actually need a hex floating-point number (though I can't imagine why), I don't have to remember that somehow becomes "A".  What is that "A" supposed to stand for anyway?  Arcane?  Who thought that was even useful?  A signed hex integer is less weird, yet apparently impossible...  Seriously, the more I look at all this, the more arbitrary it seems.

Ok, rant mode off.

My intent in posting this question was that before doing my own defines and whatnot, I just wanted to see if anyone else had come up with something similar, but possibly more elegant.
« Last Edit: August 27, 2015, 03:27:25 am by Chris C »
 

Offline Chris CTopic starter

  • Frequent Contributor
  • **
  • Posts: 259
  • Country: us
Re: I hate C printf format specifiers. Alternatives?
« Reply #9 on: August 27, 2015, 03:17:57 am »
For those of us who remember Mark Williams COHERENT, there is a picture() function there, for C.  In 2015, they open-sourced COHERENT and that particular picture() call is available in the libmisc source.

That looks similar to formatting functions I've used in other languages.  I'll have to check that out, thanks.

COBOL.  My grandma coded in that, no kidding. ;)
« Last Edit: August 27, 2015, 03:28:15 am by Chris C »
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: I hate C printf format specifiers. Alternatives?
« Reply #10 on: August 27, 2015, 06:43:23 am »
Seriously? Nothing could be simpler. More modern stuff uses regular expressions. Try that then go back to the simplicity of printf()

Uhh, regular expression are used for string parsing - not string formatting...


I share Chris' sentiments. My day job makes me program in C# that uses a different set of placeholders. Result is the same, I also have to look these up when it gets more complex. So I guess, no matter what you think up - when you cannot remember the logic behind the placeholders it will always feel as a PitA.
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3645
  • Country: us
Re: I hate C printf format specifiers. Alternatives?
« Reply #11 on: August 27, 2015, 06:54:28 am »
The big problem with C interfaces like printf() isn't that they are hard to remember, it's that they are completely untyped.
 

Offline jwm_

  • Frequent Contributor
  • **
  • Posts: 319
  • Country: us
    • Not A Number
Re: I hate C printf format specifiers. Alternatives?
« Reply #12 on: August 27, 2015, 07:04:38 am »

Hmm, "d" is an integer, right?  But wait, "u" is an unsigned integer, so that would make a signed integer "i', and that makes "d"...double?  Arg, gotta check that chart again...

Seriously, WHY did they assign two letters to the same darn thing?  I don't care what anyone says, this is NOT intuitive.

I know this one :), when reading text, %d is always base 10, while %i accepts any of the C standard number formats (like 0xff for 256, or 0177 for octal). This difference isn't important for printf because the default for %i is base 10, so it looks the same as %d, however for scanf there is a big difference. It would be weird for a specifier to work in scanf but not printf so printf accepts both even though they behave the same so you can reuse the same formatting strings to read/write the same data.

And there is nothing wrong at all with using %i in printf for int. I use it exclusively, most modern programmers do I think for the reasons you state.
« Last Edit: August 27, 2015, 07:07:59 am by jwm_ »
 

Offline android

  • Regular Contributor
  • *
  • Posts: 134
  • Country: au
Re: I hate C printf format specifiers. Alternatives?
« Reply #13 on: August 27, 2015, 08:08:16 am »
picture(3101.1, "*$ZZZ,ZZZ.99", output_string) yields "$***3,101.10" in output_string.
...and, on IBM mainframes at least, the conversion is done with a single machine instruction.  8)
Lecturer: "There is no language in which a double positive implies a negative."
Student:  "Yeah...right."
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 7426
  • Country: nl
  • Current job: ATEX product design
Re: I hate C printf format specifiers. Alternatives?
« Reply #14 on: August 27, 2015, 09:35:04 am »
The big problem with C interfaces like printf() isn't that they are hard to remember, it's that they are completely untyped.
You sit in front of a computer when writing C code. It takes 4 seconds to type c lib printf into google, but you can print a C reference guide (good ones are 2-4 pages) and stick it on the wall if you are a programmer.
I think it is the most convenient way of outputting a string. I find myself using it even if I'm programming in C++ or on Arduino for example. sprintf it to a buffer, serial.print the buffer, leave me alone with the made up bullshit. C string formatting is simple, elegant and it works regardless the architecture number of bits or platform.
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2320
  • Country: au
Re: I hate C printf format specifiers. Alternatives?
« Reply #15 on: August 27, 2015, 09:43:49 am »
The big problem with C interfaces like printf() isn't that they are hard to remember, it's that they are completely untyped.
You sit in front of a computer when writing C code. It takes 4 seconds to type c lib printf into google, but you can print a C reference guide (good ones are 2-4 pages) and stick it on the wall if you are a programmer.
I think it is the most convenient way of outputting a string. I find myself using it even if I'm programming in C++ or on Arduino for example. sprintf it to a buffer, serial.print the buffer, leave me alone with the made up bullshit. C string formatting is simple, elegant and it works regardless the architecture number of bits or platform.
This doesn't seem to refute the claim that printf is completely untyped. Also, the OP has outlined the not-so elegant hacks you need for your printf to work "regardless the architecture number of bits or platform".
 

Offline helius

  • Super Contributor
  • ***
  • Posts: 3645
  • Country: us
Re: I hate C printf format specifiers. Alternatives?
« Reply #16 on: August 27, 2015, 09:48:49 am »
Quote
Remember I typedef'ed int16_t to I16?  That means I'm going to instinctively want to put that "i" in there, right before the "16", every time.  Which makes perfect sense, really, because IT IS STILL AN I16.  That I'm asking for output in hex, has NOTHING to do with the input data type, and should NOT replace or require changes to that convention.
The problem with this specific strategy is that printf() commands are not type specifiers, they are format conversions. And if you understand the way that C passes arguments, you should know that "I16" is not the type of any actual argument to printf(). All arguments to a variadic function undergo the usual type conversions, which means that what printf() receives is simply an "int". It's seductive to think that you can fix C's unspecified-integer-width problems with type definitions and make everything a known number of bits, but this is not generally the case.

Quote
It takes 4 seconds to type c lib printf into google
More programmers who are only as smart as google  :palm:

Quote
sprintf it to a buffer, serial.print the buffer, leave me alone with the made up bullshit.
Excellent, I'll make sure to give your products special attention when it comes to stack overflow exploits.

Quote
it works regardless the architecture number of bits or platform.
Yes, as long as you don't care whether overflow is detected or how many bits are actually given.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4079
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: I hate C printf format specifiers. Alternatives?
« Reply #17 on: August 27, 2015, 10:41:01 am »
This document explains the interface of the printf family.
http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html
Notice that this is not a description of an implementation, but more the standard used. So results may vary depending on the laziness of the lib people.

There is a lot of implicit type conversion going on, you should be able to make it more strict using the length modifiers. Still, no errors will be shown, it's C, not java.
If you really don't like it, you can get angry for a week and create your own, it not impossible. But remember that the parameters occupy memory. So using %d if more efficient than using %int_16.t
http://chibios.sourceforge.net/html/group__chprintf.html
« Last Edit: August 27, 2015, 10:43:59 am by Jeroen3 »
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6193
  • Country: us
Re: I hate C printf format specifiers. Alternatives?
« Reply #18 on: August 27, 2015, 12:39:16 pm »
The big problem with C interfaces like printf() isn't that they are hard to remember, it's that they are completely untyped.

Since compilers do verify the printf arguments if you use a literal format and complain if you don't. IIRC GCC has a pragma that allows to write functions of your own with printf like format and have the compiler verify it.

EDIT:  from GCC manual:
format (archetype, string-index, first-to-check)
The format attribute specifies that a function takes printf, scanf, strftime or strfmon style arguments which should be type-checked against a format string. For example, the declaration:
          extern int
          my_printf (void *my_object, const char *my_format, ...)
                __attribute__ ((format (printf, 2, 3)));
« Last Edit: August 27, 2015, 01:07:08 pm by zapta »
 

Offline ralphd

  • Frequent Contributor
  • **
  • Posts: 445
  • Country: ca
    • Nerd Ralph
Re: I hate C printf format specifiers. Alternatives?
« Reply #19 on: August 27, 2015, 12:40:57 pm »
The big problem with C interfaces like printf() isn't that they are hard to remember, it's that they are completely untyped.
Smart compilers can recognize the printf call and check the parameters.
Unthinking respect for authority is the greatest enemy of truth. Einstein
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: I hate C printf format specifiers. Alternatives?
« Reply #20 on: August 27, 2015, 02:02:41 pm »
The big problem with C interfaces like printf() isn't that they are hard to remember, it's that they are completely untyped.

exactly, for that reason I prefer to create my collection of functions, one functions for every data type

uint32_t ---> put_uint32
sint32_t ---> put_sint32
char_t ---> put_char (which can be 8 or 16bit)
string_t ---> put_string

the code is also smaller than printf, and I do not need all the support for the stack

ADA works this way, and so my embedded C
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4079
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: I hate C printf format specifiers. Alternatives?
« Reply #21 on: August 27, 2015, 02:48:32 pm »
Translations become more complicated though.
With printf you can just swap the first arguments, with custom stuff and operator<< this becomes more work.
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 27008
  • Country: nl
    • NCT Developments
Re: I hate C printf format specifiers. Alternatives?
« Reply #22 on: August 27, 2015, 02:59:33 pm »
I agree. The elegance of printf is that you can mix fixed text and numbers. But nobody says you can't create your own printf. It isn't difficult to do that; there are many examples on how to create a function with a variable number of arguments. However.. some compilers (GCC for example) check the format specifier of printf against the type of the variable and emit a warning if there is a mismatch.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: I hate C printf format specifiers. Alternatives?
« Reply #23 on: August 27, 2015, 03:36:29 pm »
some compilers (GCC for example) check the format specifier of printf against the type of the variable and emit a warning if there is a mismatch.

SierraC does not, that was the first reason why I have decided to created a dedicated function, one function for every data_type.
ADA works exactly this way, so from my point of view I am simplifying my job (translating from ADA to C)
 

Offline free_electron

  • Super Contributor
  • ***
  • Posts: 8518
  • Country: us
    • SiliconValleyGarage
Re: I hate C printf format specifiers. Alternatives?
« Reply #24 on: August 27, 2015, 04:02:56 pm »
printf is unsafe ...
Professional Electron Wrangler.
Any comments, or points of view expressed, are my own and not endorsed , induced or compensated by my employer(s).
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf