Live data from Hacker News

Mildly interesting quirks of C

gist.github.com

31–40 of 91 posts

Re: Mildly interesting quirks of C

#31
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 :-)

[deleted]

Re: Mildly interesting quirks of C

#33
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.

That was very interesting!

Re: Mildly interesting quirks of C

#34
post #18
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 :-)

That's #15 on the list.

That‘s #list on the 15.

Re: Mildly interesting quirks of C

#35

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

My understanding of how it works is, with constant value, the compiler replaces (x) with the constant 0 and converts (void *) into (int *) which makes the size equality to return true. But I am not entire sure :)

Re: Mildly interesting quirks of C

#36

Quote: "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 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_member

Re: Mildly interesting quirks of C

#37

Quote: "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…

>"Dynamic array" refers to block of memory allocated via malloc() which you just happen to use as array.No. A dynamic array is an array which can be expanded or shrinked during its runtime life. The fact that C/C++ uses malloc for that (and btw, it's not the only way to do it) it's her problem. In other languages you have dynamic arrays that can be expanded/shrinked without using an extra line - main reason why nowadays Rust is a replacement for C/C++

>[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

#38
post #6

Reminds 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

> "Who Says C is Simple?"

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.

https://www.youtube.com/watch?v=eSaIKHQOo4c

https://www.youtube.com/watch?v=y5Tf1EZVj8E

Re: Mildly interesting quirks of C

#40
After learning about a few of these I started to understand why people coming from C always said that PHP is a well designed language…

But 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.

¹ https://grugbrain.dev/

Post reply on HN