Your C is not standard C

(sebsite.pw)

50 points | by dverlaeckt80 1 day ago

4 comments

  • omoikane 1 hour ago
    GCC still not implementing the standard pragmas reminds me of the old GCC pragma easter egg. This is in cccp.c from gcc 0.9:

        /*
         * the behavior of the #pragma directive is implementation defined.
         * this implementation defines it as follows.
         */
        do_pragma ()
        {
          close (0);
          if (open ("/dev/tty", O_RDONLY) != 0)
            goto nope;
          close (1);
          if (open ("/dev/tty", O_WRONLY) != 1)
            goto nope;
          execl ("/usr/games/hack", "#pragma", 0);
          execl ("/usr/games/rogue", "#pragma", 0);
          execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
          execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
        nope:
          fatal ("You are in a maze of twisty compiler features, all different");
        }
    
    It's gone by gcc 1.21:

        #if 0
        /* This was a fun hack, but #pragma seems to start to be useful.
           By failing to recognize it, we pass it through unchanged to cc1.  */
        ...
        #endif
  • dzaima 2 hours ago
    > gcc, however, always defines FLT_ROUNDS to 1

    Well, considering that gcc doesn't support the FENV_ACCESS pragma, and as such all fesetround calls are always UB, it is valid for it to act as if the rounding mode never changes from the default :)

  • MisterTea 1 day ago
    As someone who loves and enjoys writing Plan 9 c, all I can say is "standard c", whatever that means these days, is a mess. Too many things to name ranging from the only two oss compilers being bloated monsters, post c99 additions being pointless, in-line asm ruining portability*, and POSIX can die in a fire (along with typewriter terminals.)

    I have a friend who is currently on the C committee and is likely going to leave because of all the bad ideas which have both been implemented and going to be implemented.

    * No, spamming a million ugly #ifdefs all over the source files is gross and hard to read. Split up the code dammit.

    • WalterBright 5 minutes ago
      It would be nice for the C Standard to say that `char` is unsigned. The damage done by making its signedness implementation-defined is incalculable.

      (D sez `char` is unsigned. No more nonsense!)

      C also needs to implement proper arrays. D does it, and that one feature is worth its weight in gold:

      https://www.digitalmars.com/articles/C-biggest-mistake.html

      It's sooo nice to not have array overflows anymore.

    • glouwbug 2 hours ago
      Plan 9 inheritance is really nice. You can still use it with `gcc -fplan9-extensions`. It builds some nice polymorphic workflow an array of tagged enum void pointers
    • 201984 2 hours ago
      >post c99 additions being pointless

      Atomics are necessary nowadays though.

      • 3836293648 2 hours ago
        As a part of the language? Are they really? You can do it all with library functions that just take regular int* (and so on).

        Not that I'd want it, I like complex type systems, but I see it as philosophically consistent with C.

        • aw1621107 2 hours ago
          > You can do it all with library functions that just take regular int* (and so on).

          It's not just about atomics, but you might still find Hans Boehm's 2005 paper "Threads Cannot Be Implemented As a Library" [0] interesting. It describes (among other things) correctness and performance issues that can arise if the compiler and/or language don't know about thread/atomic semantics.

          [0]: https://web.stanford.edu/class/archive/cs/cs240/cs240.1236/o...

          • Joker_vD 23 minutes ago
            Yeah, it's all fun and games until you realize that you compiler may invent writes in order to eliminate a branch. Which is rather nonsensical, when you stop to think about it — surely an additional store to global can't be faster than a mispredicted branch?
        • jcranmer 1 hour ago
          Compilers have, for several decades now, stopped viewing optimization in terms of optimizing the instructions of a function and instead viewed it as preserving the semantics under some machine model. Consequently, it's rather important to actually nail down some of the object model semantics to be able to do things like understand which loads and stores actually need to be preserved, and you can't really do that with just library functions.

          And, FWIW, in a lot of high-performance code, the overhead of a function call may be too high for an atomic operation.

        • publicmail 2 hours ago
          A library function written in what? Atomics also come along with memory ordering which isn’t otherwise expressible in the language, as in, there was no standard way to do a “load acquire” before, for example.
    • jcranmer 1 hour ago
      There's a lot of post-C99 that has somewhat questionable value (_Generic is very much a we-have-templates-at-home kind of feature), but there are some post-C99 features that I'm rather happy to have:

      * thread_local and _Atomic support from C11

      * fopen("foo", "x") from C11

      * aligned_alloc from C11

      * CMPLX macro from C11

      * C++ attribute syntax ([[foo]] flows better to me than __attribute__((foo)) does)

      * C23 finally gets rid of _Who _The _Hell _Thinks _Users _Like _This-style _Keywords.

      * Multi-level break/continue in C2y

      * Variable declarations in if conditions.

      * Some of the new operations like ckd_add or stdc_rotate_left. I do dislike the names, but meh, at least C is starting to gain a competent set of fundamental operations.

      • Joker_vD 1 hour ago
        Also, C23 (and C2y) is moving ever closer to actually having defined semantics. As in, "just because one branch of your code, for this particular input, has read a value out of an array's bounds, your whole program's execution doesn't suddenly rewinds back to the start (with I/O included) and NOPs/loops infinitely/executes random bytes after main()".

        Also, I'm personally still waiting for a "shift left" operator having cleaner wording in the standard:

            If E1 has an unsigned type, the value of the result is E1 × 2**E2, wrapped
            around. If E1 has a signed type and nonnegative value, and E1 × 2**E2 is
            representable in the result type, then that is the resulting value; otherwise,
            the behavior is undefined.
        
        What does "otherwise" means here? It can't mean "if E1 has an unsigned type, or negative value, or E1 × 2**E2 is unrepresentable, then the behavior is undefined" because the part "if E1 has an unsigned type" has already been covered by the previous clause. Does it mean "If E1 has negative value, or E1 × 2**E2 is unrepresentable, then the behavior is undefined"? Because that's a ridiculous ordinance: (-1) << 1 is UB?!
        • jcranmer 18 minutes ago
          > Does it mean "If E1 has negative value, or E1 × 2*E2 is unrepresentable, then the behavior is undefined"? Because that's a ridiculous ordinance.

          That's exactly what it means, see the change mentioned in https://www.open-std.org/jtc1/sc22/wg14/www/docs/n723.htm. (Previously, in C89, left shift on signed integers was completely undefined, only unsigned integers had a defined left-shift operation.) My guess is that it has to do with signed-magnitude representation causing left shifts to be absolute garbage with negative numbers. You also have to remember that the standard having undefined behavior doesn't preclude implementations from defining that behavior, and this is especially true with the freewheeling nature of a lot of the original undefined behavior introduced in C89 and C99.

          > Also, C23 (and C2y) is moving ever closer to actually having defined semantics.

          Eh, having seen how some of the sausage gets made, there's definitely been some cases where the overeager urge to rip out undefined behavior from the standard has worsened the quality.

        • SAI_Peregrinus 25 minutes ago
          I think it means what you think is ridiculous. Shifting the sign bit out is UB. Unrepresentable results are UB.
          • Joker_vD 21 minutes ago
            > Shifting the sign bit out is UB

            Shifting into the sign bit is also UB. Can I please have a language that actually has bitwise operators with defined semantics? No CPUs had differentiated between SAL/SHL for more than 30 years, if that was the original justification.

            I'm fine with left shift trapping on overflow. I'm fine with it just wrapping around. I'm even fine with bitwise shifts simply unapplicable to ints ("cast to unsigned first, then cast back to int"). I am not fine with an operator that invokes UB for literally half of the values of the lhs's domains.

      • monocasa 34 minutes ago
        > C23 finally gets rid of _Who _The _Hell _Thinks _Users _Like _This-style _Keywords.

        I always took those to mean "we're sure a bunch of you are already using the keyword we wanted to use as typedef in your codebase, so here's something that's in the standard's namespace, and you can typedef it to something sane for your codebase".

  • Syzygies 1 hour ago
    ...and I thought Go was standard C!