Live data from Hacker News

Should you learn C to “learn how the computer works”?

words.steveklabnik.com

221–230 of 381 posts

Re: Should you learn C to “learn how the computer works”?

#221
post #186

One valuable property of C that the author didn't hit, C code is easily translatable into assembler in your head. He kind of misses this point with the virtual machine discussion. Yes C code becomes different types of assembly by platform, but you can look at C and have a clear idea of what the assembly will look like. This is at a really good level for driving intuitions about what the computer is actually doing. Yo…

> Your concern 99% of the time when you are trying to think at this level is performance, and thinking in C will give you the right intuition about how many instructions your code is generating

I really don't think that's true given modern optimizing compilers. I remember back when C "best practices" were:

* Avoid moving code out into separate functions since calls are expensive.

* Hoist subexpressions out of loops to avoid recomputing them.

* Cache things in memory to avoid recomputing them.

But inlining means now we refactor things into small functions and usually expect that to be free. Optimizers automatically hoist common subexpressions out of loops.

And caching is useful, but making your data structures larger can cause more data cache misses, which can be more expensive that just recomputing things. I've seen good CPU cache usage make a 50x difference in performance.

Even in C, optimizing is now an empirical art and not something you can reason about from first principles and running the compiler in your head.

Re: Should you learn C to “learn how the computer works”?

#222
post #103
post #66

You should learn C to learn what every language is loosely based on and what unrestricted memory access looks like. It's the programming equivalent of learning Latin. It isn't required, but you will be better at pretty much everything you do involving modern English, Spanish, French, etc., as you will understand where things came from. It's even more useful though, as in the programming language world pretty much eve…

> It's even more useful though, as in the programming language world pretty much everything evolved out of C Not really, there was a world of computing outside AT&T walls. Lots of papers and computing manuals are available online for those that care about the actual history of computing.

This is also true, so maybe it is a better analogy than I gave myself credit for ;)

Re: Should you learn C to “learn how the computer works”?

#223
Great article!

I tried getting through K&R C last tear, but never made it past chapter 2 or 3. I had thought C would teach me about how about all things computer-y below the higher level language I was using. What I really learned was how little C actually does for the things I wanted to understand. I wanted to learn about sockets, file descriptors, protocols, process forking, writing drivers, etc. I had little interest in learning about how many landmines there were to avoid in writing good C, and I had little interest in reimplementing features in some higher level language. When I say little interest, I mean I came to understand my priorities were more about learning how Linux works. I REALLY wanted to understand is how Linux does its thing in whatever language(s) it was written in. At the time, I pivoted to deeply reading about Linux concepts in How Linux Works, and I really enjoyed it. [1]

So for me, starting to learn C opened the door to a whole bunch of cool paths to follow. I may or may not get back to learning C itself at some point, but right now I'm having fun learning other things.

[1] https://nostarch.com/howlinuxworks2

Re: Should you learn C to “learn how the computer works”?

#224
post #103
post #66

You should learn C to learn what every language is loosely based on and what unrestricted memory access looks like. It's the programming equivalent of learning Latin. It isn't required, but you will be better at pretty much everything you do involving modern English, Spanish, French, etc., as you will understand where things came from. It's even more useful though, as in the programming language world pretty much eve…

> It's even more useful though, as in the programming language world pretty much everything evolved out of C Not really, there was a world of computing outside AT&T walls. Lots of papers and computing manuals are available online for those that care about the actual history of computing.

This is a genuine question -- are there non-C-family systems languages from that period (as in languages that allow low level memory manipulation and assembly embedding)? I have never heard of or seen any.

Re: Should you learn C to “learn how the computer works”?

#225

Earlier quoted context omitted.

Or passing by value vs passing a pointer, or the importance of memory management... Modern languages solve problems created developing in legacy languages (primarily C). The issue is that knowing a solution without knowing the prior problem which the solution addresses, doesn't really lend itself to clarity.

They don't solve problems created by C, they just solve problems C doesn't. The problems still exist, and sometimes the high-level compiler even picks the wrong solution. Learning C helps you understand when and why this has happened.

"Exploding" undefined behaviour is a problem created by C. Other languages don't have it, not even assembly languages.

Re: Should you learn C to “learn how the computer works”?

#227

C doesn't necessarily teach you how computers work. But it does teach you how software works. Our modern software empire is built on mountains of C, and deference to C is pervasive throughout higher-level software design. >You may have heard another slogan when talking about C: “C is portable assembler.” If you think about this slogan for a minute, you’ll also find that if it’s true, C cannot be how the computer work…

Exactly, if you want to know how a computer works, you should take a course in Computer Architecture. I remember that subject from BSc quite fondly.

Re: Should you learn C to “learn how the computer works”?

#228
post #186

One valuable property of C that the author didn't hit, C code is easily translatable into assembler in your head. He kind of misses this point with the virtual machine discussion. Yes C code becomes different types of assembly by platform, but you can look at C and have a clear idea of what the assembly will look like. This is at a really good level for driving intuitions about what the computer is actually doing. Yo…

If you want a developer to have a good intuition for what assembly looks like, the only real way to do that is for them to take an assembly class or to play with assembly a bit.

It doesn't really matter if the assembly they play with comes from C or Rust or C++ or any other language that outputs assembly.

In short, learning C doesn't teach you assembly. Learning assembly teaches you assembly.

Re: Should you learn C to “learn how the computer works”?

#229
post #58

Earlier quoted context omitted.

C at the very least teaches the difference between stack and heap memory, a crucial concept obscured by most higher-level languages.

Just playing devil's advocate here... why is this such a "crucial" concept, for someone who is using a higher level language (like Python or Ruby)? If 99% of what that person does is gluing together APIs and software modules, and they can see their memory usages are well within range, why does it matter?

Compared to people who know only high level languages I have noticed that with my C background I can figure out performance problems better because I have an idea how the higher languages are implemented.

Re: Should you learn C to “learn how the computer works”?

#230
post #225

Earlier quoted context omitted.

They don't solve problems created by C, they just solve problems C doesn't. The problems still exist, and sometimes the high-level compiler even picks the wrong solution. Learning C helps you understand when and why this has happened.

"Exploding" undefined behaviour is a problem created by C. Other languages don't have it, not even assembly languages.

There are many languages which have undefined behavior.

C does have a lot of it, and it can be anywhere. In many languages, you can cause undefined behavior through its FFI. Some languages, like Rust, have UB, but only in well-defined places (a module containing unsafe code, in its case).

Post reply on HN