Live data from Hacker News

Mildly interesting quirks of C

gist.github.com

71–80 of 91 posts

Re: Mildly interesting quirks of C

#71
post #25

As someone who's moved on to Rust, I see this as one long list of nightmares.

I don't think the "I use Rust btw" comments contribute much to the discussion. C and Rust don't perfectly overlap, especially since Rust is more a replacement to C++ than C.

Pointing out that a comment doesn't contribute to discussion doesn't contribute to discussion either. I'm definitely not contributing much by saying this.

Re: Mildly interesting quirks of C

#72

Can someone explain how "A constant-expression macro that tells you if an expression is an integer constant" works ?

If `x` is a constant, `(x) * 0l` is a zero constant, so `(void*)((x) * 0l)` is a null pointer. When a null void pointer is one branch of a ternary conditional, the expression takes the (pointer) type of the other branch. If `x` is not a constant, `(void*)((x) * 0l)` is a void pointer to address 0 (which may not even be a null pointer at runtime, since null may have a runtime address distinct from zero!). The ternary…

Thanks!

Re: Mildly interesting quirks of C

#74
The "Compound literals are lvalues" is one that caught me out recently.

I've been doing C a long time and thought I knew all the "decent" tricks. When I saw it, I went "Oh, that's one of the silly new dynamic features that I ignore."

Nope.

It's been in the language forever. I'm surprised I never tripped over it before given all the embedded work I do.

Re: Mildly interesting quirks of C

#76
post #62

Note that: void foo(int p[static 1]); is effectively a standard way to declare that p must be non-null pointer. I always wondered if any compiler actually makes use of this for optimization purposes.

If you have a null check on the pointer in the body, GCC will elide it.

Re: Mildly interesting quirks of C

#77
post #24
post #15

Whenever the subject of C/C++ quirks is brought up, I always like to point out the Deep C/C++ presentation: http://www.pvv.org/~oma/DeepC_slides_oct2011.pdf Source: https://freecomputerbooks.com/Deep-C-and-Cpp.html#downloadLi... Previous discussion: https://news.ycombinator.com/item?id=3093323 It could be considered a bit dated at this point (It's before C++11) but I find it still both entertaining and educating.

Loved that, read it start to finish. C is already a minefield and it looks positively tame when compared to C++!

Yea this also one reason I don't like how these two languages are often lumped as C/C++ so often.

These days they have diverged even further. So I find funny to see C/C++ on job listings like it's one thing. While you can write code in a very C like manner in C++. That is not how typical C++ is often written these days.

Re: Mildly interesting quirks of C

#78
post #16

My favorite C "quirk": If you have an array and you want to access an item of it, you can swap the variable and the index number (put the variable name inside brackets and the number outside): a[5] is the same as: 5[a] why? a[5] is actually sugar for *(a + 5), so by commutative property, you can also do *(5 + a) to access the same memory position :-)

Was about to post this. I may have posted it before on HN. IIRC, I first read about it in the K&R C book.

Re: Mildly interesting quirks of C

#80
post #62

Note that: void foo(int p[static 1]); is effectively a standard way to declare that p must be non-null pointer. I always wondered if any compiler actually makes use of this for optimization purposes.

Wonder no more: at least clang does, since around v3.5. https://godbolt.org/z/jEq5xbMna
Post reply on HN