News for hackers. Stuff that matters.
Re:The C23 edition of Modern C is now available for free by tialaramex on Tuesday October 15, 2024 @02:27PM (#41851557)
Why use this operator? Like most C and C++ features the main reason tends to be showing off, you learned a thing (in this case that there are four extra operators here) and so you show off by using it even if it doesn't make the software easier to understand.

This is not one of those beginner -> journeyman -> expert cycles where coincidentally the way you wrote it as a beginner is identical to how an expert writes it but for a very different reason. I'd expect experts are very comfortable writing either { x = k; k += 1; } or { k += 1; x = k; } depending on which they meant and don't feel an itch to re-write these as { x = k++; } and { x = ++k; } respectively.

I'm slightly surprised none of the joke languages add equally frivolous operators. a%% to set a to the remainder after dividing a by 10, or b** to set b as two to the power b or some other silliness.

Re:The C23 edition of Modern C is now available for free by layer8 on Tuesday October 15, 2024 @03:23PM (#41852158)
The idiomatic

    void strcpy(char *s, char *t)
    {
        while (*s++ = *t++)
            ;
    }
(straight from K&R) wouldn’t work without it.
Re:The C23 edition of Modern C is now available for free by n_plus_1_acc on Tuesday October 15, 2024 @05:06PM (#41853132)
Which many people find unreadable compared to other versions.
Re:The C23 edition of Modern C is now available for free by beeforpork on Wednesday October 16, 2024 @07:24AM (#41857853)
And for several reasons.

  * is it (*s)++ or *(s++)?
  * it is not *++s nor ++*s
And I have seen

  *(*s)++
in some places!

It is concise syntax but very confusing.

Re:The C23 edition of Modern C is now available for free by chongli on Wednesday October 16, 2024 @10:54AM (#41859785)
K&R actually teaches this as a desirable idiom? People should not be recommending K&R to beginners today!
Re:The C23 edition of Modern C is now available for free by trealira on Wednesday October 16, 2024 @11:56AM (#41860622)
Kernighan spends a page whittling strcpy down to just that, with various intermediate versions. After showing you that version, he describes it like this:

Although this may seem cryptic at first sight, the notational convenience is considerable, and the idiom should be mastered, because you will see it frequently in C programs.

Re:The C23 edition of Modern C is now available for free by cozzyd on Tuesday October 15, 2024 @02:58PM (#41851920)
It's more useful for pointers than for values, IMO
Re:The C23 edition of Modern C is now available for free by trealira on Tuesday October 15, 2024 @08:54PM (#41854621)
They can be useful when adding things to an array in a loop. A trivial example which removes a character from a null terminated string:

  void remove_char(char *s, char c) {
    size_t i, j;

    for (i = j = 0; s[i] != '\0'; i++)
      if (s[i] != c)
        s[j++] = c;
    s[j] = '\0';
  }

This might be better expressed with a higher order filter function, but C is too low level for things like that.

There are also idioms for stack manipulation using them: "stack[sp++] = pushed" and "popped = stack[--sp]".

C code does a lot of incrementing and decrementing by one, and so having dedicated syntax for it is convenient.

Re:The C23 edition of Modern C is now available for free by jancsika on Wednesday October 16, 2024 @01:26AM (#41855882)
Note that in your example there appear to be three distinct meanings:

1. prefix incr/decr precedence: "stack[--sp]"

2. postfix incr/decr precedence: "s[j++]"

3. i have no particular preference for the precedence and am just using a shorthand I inherited from my ancestors whose use cases are no longer relevant to me: "i++" in your for loop

My rank speculation is that C programmers get in a habit of #3 and then forget to consider precedence in an expression where it matters.

In any case, it would be interesting to do a scan of github to see how often prefix and suffix incr/decr had to get switched up in a bugfix patch.

Re:The C23 edition of Modern C is now available for free by trealira on Thursday October 24, 2024 @08:42PM (#41941250)
Silly mistake: that should read "s[j++] = s[i]".
Re:The C23 edition of Modern C is now available for free by int_19h on Friday October 18, 2024 @11:36AM (#41880388)
For iterators, += may not even be available.