←back to thread

The C23 edition of Modern C

(gustedt.wordpress.com)
515 points bwidlar | 5 comments | | HN request time: 0.717s | source
Show context
belter ◴[] No.41850897[source]
Important reminder just in the Preface :-)

Takeaway #1: "C and C++ are different: don’t mix them, and don’t mix them up"

replies(8): >>41850960 #>>41851047 #>>41851166 #>>41851693 #>>41853183 #>>41855660 #>>41857019 #>>41858537 #
jpcfl ◴[] No.41851047[source]
Bjarne should have called it ++C.
replies(2): >>41851328 #>>41854437 #
card_zero ◴[] No.41851328[source]
Because people choose to use pre-increment by default instead of post-increment?

Why is that?

replies(4): >>41851427 #>>41851557 #>>41851650 #>>41853498 #
tialaramex ◴[] No.41851557[source]
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.

replies(4): >>41851920 #>>41852158 #>>41854621 #>>41880388 #
1. layer8 ◴[] No.41852158[source]
The idiomatic

    void strcpy(char *s, char *t)
    {
        while (*s++ = *t++)
            ;
    }
(straight from K&R) wouldn’t work without it.
replies(2): >>41853132 #>>41859785 #
2. n_plus_1_acc ◴[] No.41853132[source]
Which many people find unreadable compared to other versions.
replies(1): >>41857853 #
3. beeforpork ◴[] No.41857853[source]
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.

4. chongli ◴[] No.41859785[source]
K&R actually teaches this as a desirable idiom? People should not be recommending K&R to beginners today!
replies(1): >>41860622 #
5. trealira ◴[] No.41860622[source]
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.