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.
Mildly interesting quirks of C
71–80 of 91 posts
Re: Mildly interesting quirks of C
#72Can 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…
Re: Mildly interesting quirks of C
#73Re: Mildly interesting quirks of C
#74I'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
#75Re: Mildly interesting quirks of C
#76Note 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.
Re: Mildly interesting quirks of C
#77Whenever 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++!
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
#78My 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 :-)
Re: Mildly interesting quirks of C
#79WTF. Here have some keyword soup, the order doesn't matter at all. Must be fun to write a C compiler.
Re: Mildly interesting quirks of C
#80Note 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.