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 :-)
Mildly interesting quirks of C
31–40 of 91 posts
Re: Mildly interesting quirks of C
#32Re: Mildly interesting quirks of C
#33Whenever 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.
Re: Mildly interesting quirks of C
#34My 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 :-)
That's #15 on the list.
Re: Mildly interesting quirks of C
#35Can someone explain how "A constant-expression macro that tells you if an expression is an integer constant" works ?
Re: Mildly interesting quirks of C
#36Quote: "4. Flexible array members ..... int elems[]; // TIL that a dynamic array is also called flexible. This generation, out of boringness, is trying to redefine well established paradigms? Because, for me, a 90's formed developer, "flexible" means maybe inheritance, or even better polymorphism. There is nothing flexible about a dynamic array. Its structure is well defined in the stack/heap, and with current compil…
"Flexible array member" [0] is when you have a struct and its last member is an array with unspecified size.
An example:
#include
#include
struct Foo {
int len;
int* arr; // dynamic "array"
};
struct Bar {
int len;
int arr[]; // FAM
};
int main()
{
const int n = 12;
// have to allocate myself; no guarante it will be nearby the rest of struct
struct Foo* a = malloc(sizeof a);
a->arr = malloc(n * sizeof *(a->arr));
// array is part of the memory allocated for struct
struct Bar* x = malloc((sizeof x) + n*(sizeof *(x->arr)));
return 0;
}
[0]: https://en.wikipedia.org/wiki/Flexible_array_memberRe: Mildly interesting quirks of C
#37Quote: "4. Flexible array members ..... int elems[]; // TIL that a dynamic array is also called flexible. This generation, out of boringness, is trying to redefine well established paradigms? Because, for me, a 90's formed developer, "flexible" means maybe inheritance, or even better polymorphism. There is nothing flexible about a dynamic array. Its structure is well defined in the stack/heap, and with current compil…
"Dynamic array" refers to block of memory allocated via malloc() which you just happen to use as array. "Flexible array member" [0] is when you have a struct and its last member is an array with unspecified size. An example: #include #include struct Foo { int len; int* arr; // dynamic "array" }; struct Bar { int len; int arr[]; // FAM }; int main() { const int n = 12; // have to allocate myself; no guarante it will b…
>[0]LMAO, really? Well, that indeed is a bigger C quirk. In Pascal, as an example, I can have it anywhere inside the record (struct equivalent of C), and it can be just as "flexible".
Re: Mildly interesting quirks of C
#38Reminds me a bit of "Who Says C is Simple?" written by the people who wrote a C parser & analyser in OCaml (CIL): https://cil-project.github.io/cil/doc/html/cil/cil016.html Also: https://cil-project.github.io/cil/doc/html/cil/cil012.html
People who don't know what "simple" means and confuse it with "easy".
https://www.entropywins.wtf/blog/2017/01/02/simple-is-not-ea...
https://www.infoq.com/presentations/Simple-Made-Easy/
"Easy" things almost always lead to astonishing complexity.
Also it's easy to see just how complex C is: Have a look at a formal description of it! (And compare to a truly simple language like e.g. LISP).
https://github.com/kframework/c-semantics/tree/master/semant...
In contrast some basic Lambda calculus language semantics fit 0.5 of a page in K.
Re: Mildly interesting quirks of C
#39Re: Mildly interesting quirks of C
#40But OK, I understand that my mind is just not made for the complexity of C. Most likely I'm not a real programmer.
I get instantly knots in my brain and start to bang my head against the wall when I need to look for too long on C code. Actually even C documentation is enough to trigger this. (I get mad every time I have to look on a Linux system man page).
This is highly subjective of course. Other people seem to love C!
I'm more of a grug brain¹, who mostly only understands plain pure functions.
Input in, output out. No magic. Everything else's too taxing.