DEV Community

Discussion on: CS50 Week 1 - Learning C

Collapse
 
pentacular profile image
pentacular

C is all about fixed length arrays accessed by indexes called pointers.

// Variables are effectively stored in arrays of length one.
int i = 1;

// Let's get a pointer to the array element that i is stored in.
int *a = &i;

// Now we can modify i through the pointer into the array it is stored in.
a[0] = 2;

// Just like with an explicit array.
int q[4];
// But the array evaluates to a pointer to its first element.
int *r = q;
// But the elements are modified in the same way.
r[0] = 1;
// Since the array evaluates to a pointer to its first element we can skip using r.
q[0] = 2;
Enter fullscreen mode Exit fullscreen mode

C is reasonably simple once you understand that it is:

  1. All about arrays.
  2. A primitive language designed to make mediocre compilers easy to implement.

Point 2 is why C is so wide-spread -- writing a shoddy C compiler is very easy, so there are shoddy C compilers for pretty much everything.

Writing a good optimizing C compiler is quite difficult, so it took considerable time for C implementations to become competitive with assembly.

Collapse
 
kewbish profile image
Emilie Ma

Thank you for the in-depth comment - I'll be sure to keep this in mind in the coming weeks 😄

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Writing a good optimizing C compiler is quite difficult

Still infinitely easier than for something like C++ though, which means there's not only more bad compilers but also better good compilers.

Also, to expand a bit on the array thing: that's actually a huge benefit when comparing to other languages, since arrays (unpartitioned, sequential bytes in memory) play very nice with caching, which has led to arrays performing better than other data structures even where it's counter-intuitive, like deleting elements faster than linked lists.