User avatar
Posts: 5164
Joined: Sun Jul 22, 2012 4:14 pm

Re: C's 'printf'

Fri Oct 18, 2019 6:33 pm

Heater wrote:
Fri Oct 18, 2019 6:14 pm
Yes of course with a new different language. I did not suggest "fixing" C. I don't believe sal55 meant that either.
I think he did, that's why he keeps on about his compiler that did just that, but of course it would mean any code using his not-printf would be unusable by anyone else !
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Posts: 63
Joined: Sat Sep 21, 2019 7:15 pm

Re: C's 'printf'

Fri Oct 18, 2019 6:51 pm

jahboater wrote:
Fri Oct 18, 2019 6:04 pm
:) Challenge! without looking it up, whats the conversion specifier for size_t ? :)
Hey, I know that one! It's "%zu". On Linux. But it doesn't work on Windows, even with gcc (without messing around with libraries). It might be %lu, or %llu, depending on whether compiling 32 or 64 bits. So already 3 possibilities, if you want portable code.

Now try this:

Code: Select all

#if cond1
   int a = 100;
#elif cond2
   size_t a = 20000000000;
#elif cond3
   float a = 300.921;
   long a = 400;

Quick, what's the format code for a? The cond1/2/3 macros might be hidden deep inside some nested include files, and are themselves inside conditional code.

In a normal language, you might just do 'print a', but even in my modified C, using 'printf("%?\n,a)', you get the proper output for each possible definition of a, without needing to modify the print statement. Magic.

(ETA: yes this C code would be non-portable; you'd just get "?" printed. But it's proof of concept. And there are ways to get it working with portable C that I won't go into, but involve a wrapper around the language.)

Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24195
Joined: Sat Jul 30, 2011 7:41 pm

Re: C's 'printf'

Fri Oct 18, 2019 7:18 pm

Toys thrown, Pram now closed.

Language wars, gotta love them.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

Return to “C/C++”