[Toybox] [SC22WG14.32195] ?: proposal for ISO C
Rob Landley
rob at landley.net
Wed Jul 9 18:26:10 PDT 2025
On 7/9/25 03:25, Alex Celeste wrote:
> In simpler bullet-points for the WG:
>
> - this operator enables writing something that is at best, clunky to
> express in C right now (short-circuiting logic that returns _the evaluated
> values_ rather than just 1 or 0 - from a user perspective it provides
> something like an enhances `&&` and `||` pair, that is safe from
> multiple-evaluation and doesn't require temporary objects)
> - it is _dead simple_ to implement (another one on the pile of "I added
> this personally, and it was a trivial change")
You'd think "is already widely used and has been for 30 years" would
work in there somewhere. If nobody had cared it wouldn't have persisted,
gotten used in a bunch of software, and been copied to so many other
compilers.
Checking the compiler explorer on godbolt.org for "int main(int argc,
char *argv[]) { return 1 ? : 2; }"...
It's supported in gcc and clang of course, plus icc, tinycc, zig cc,
nvidia's nvc, frc, arduino's uno and mega, ti cl430, c2rust, chibicc,
and icx.
It is _not_ supported in msvc, sdcc, orca/c, ppci, tendra, vast,
compcert, 6502 cc65, and movfuscator.
The z88dk and cproc-master compilers failed for what looks like config
reasons (I don't think they're set up right in compiler explorer, or at
least need some sort of preamble?), and LC3 complains it doesn't know
what char *argv[] is even when main returns just 0;
Compiler explorer isn't exhaustive, they don't have pcc (which supports
this), libfirm/cparser (ditto), amsterdam compiler kit is that old minix
thing with a web page on sourceforge: https://tack.sourceforge.net/
I just spent half an hour trying to build and run ACK's current git but
only got as far as ".obj/staging/bin/ack: Cannot find description for
fe" and ran out of interest. Minix's
http://www.minix3.org/doc/environ.html doesn't link to the sourceforge
page but says you could instead use gcc, tendra, or Dave Walker's C386
on a web page that archive.org says went down in 2016...
> So there is a high asymmetry between a very low level of "effort needed
> once" from compiler implementers (or not at all since basically everyone
> already has this as an extension),
It's just an "a || b" that doesn't cast to bool. Use this value unless
it's false/null, in which case evaluate/use this other value.
Most compilers added support for this over a decade ago. It already
exists. The request is that the committee acknowledge a long established
syntax for expressing that. Not "why should they do X" but "did you
notice this thing in continuous widespread use since the 1990s".
> and a high level of "effort needed every
> time" by end users to get the exact desired behaviour, making this a very
> good candidate for a valuable core language operator.
>
> The rationale for inclusion is therefore fundamentally similar to that for
> `_Countof`.
Is there a rationale in the standard for || existing instead of always
going:
a = thing();
if (!a) a = thing2();
Because technically || doesn't need to exist, you COULD rewrite every
program not to use it. But lots of existing programs DO use it...
> Thanks,
>
> Alex
Rob
More information about the Toybox
mailing list