Live data from Hacker News

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

words.steveklabnik.com

111–120 of 381 posts

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

#111

What really made computers click to me was reading a book that had the premise: "learn just enough assembly to be able to implement C features by hand; we'll show you how". Sadly, I don't remember the title. Another revelation much later on, was, as discussed here, the realisation that C is indeed defined over an abstract machine. I think much of those realisations were because of reading about how crazy compiler opt…

Sounds a bit like "Programming from the Ground Up" [1]. [1] https://savannah.nongnu.org/projects/pgubook/

Or maybe just Professional Assembly Language [1]

[1] http://www.wrox.com/WileyCDA/WroxTitle/Professional-Assembly...

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

#112
post #87

Earlier quoted context omitted.

RISC-type architectures with a branch-and-link instruction (as opposed to a jsr- or call-type instruction) generally have a stack by convention only, because the CPU doesn't need one to operate. (For handling interrupts and exceptions there is usually some other mechanism for storing the old program counter.)

Can you point me to a RISC architecture that doesn't have push and pop instructions?

ARM64 uses regular loads and stores to access the stack, I believe. It also is one of the architectures with branch-and-link. https://community.arm.com/processors/b/blog/posts/using-the-...

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

#113
post #68

I find this article to be disingenuous. Yes, C isnt "how a computer really works". Neither is assembly. The way a computer works is based off of transistors and some concepts built on top of that (ALUs for example). However, there is no need to know about any of that because you're presented with an abstraction (assembly). And thats really what people mean when they say C is closer to how a computer actually works: i…

Except it isn't, not really. Even just the distinction between the stack & heap is wrong. They aren't different things, just different functions called on the otherwise identical memory. It's why things like Go work fine, because the stack isn't special. It's just memory. malloc & free are also totally divorced from how your program interacts with the OS memory allocator, even. GC'd languages don't necessarily sit on…

Would it be fair to say that it will teach you how to dictate the memory layout of your program, which is key to taking proper advantage of "cache locality, cache lines, prefetching, etc..."?

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

#114
I just have so many problems working with the former C programmers on my team as a Python dev. They're constantly worried about memory management, they're not very concerned about architecture and don't make full use of the Python language, they're of the "if it works, ship it" mentality, and they're constantly writing code that violates my tastes.

I can write a C program, but I am not in any way a C ninja. I sometimes wonder if having to write C to pay for the food on your table teaches you bad habits when trying to bring that experience up a layer (or three). Maybe not, I don't know. Am I alone in this thinking?

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

#115
I like this article a lot. There are two ways you can think of looking at the field of programming:

* As a continuum from "low level" to "high level".

* As a giant bag of topics: strings, heap, hash tables, machine learning, garbage collection, function, instruction set, etc.

If your goal is to have a broad understanding of CS, you want to explore the whole continuum and many topics. C is great for that because it exists at a sweet spot that's lower-level than most languages but not so low level that you have to jump into the deep end of modern CPU architectures which are fantastically complex.

Because C has been so successful, there are few other successful languages that sit near it on that line. Those that are (C++, Rust) are much more complex. So if your goal is just to get familiar with that region of the continuum and not become an expert in a new language, C has a good price/performance ratio.

Also, it's a good language for getting exposure to many topics other languages hide. If all you know is JS or Ruby, C will teach manual memory management, static types, heap versus stack allocation, pointers versus values, primitive fixed-sized arrays, structs, and bit manipulation. Its sparse standard library means you'll end up implementing many common data structures yourself from scratch, so you'll get a better understanding of growable arrays, linked lists, trees, strings, hash tables, etc.

"Portable assembly" is a nice slogan for C. But it's worth remembering that back when it was coined, the emphasis was on "portable", not "assembly". At the time, C was an alternative to assembly languages, not higher-level languages.

It's never been very close to an assembly language. C has types while assembly is untyped. C has function calls that abstract the calling convention while assembly languages make that explicit. C implicitly converts between types, assembly doesn't.

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

#116
post #109

Earlier quoted context omitted.

Sometimes the bugs don't happen when optimizations are off. (-:

That'd be a compiler bug, then. I don't think those would be common. EDIT: I suppose the other possibility is that the program is doing some weird things, like reading its own machine code from memory.

Or undefined behavior in the program, which happens to manifest differently under different compile options.

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

#117

What really made computers click to me was reading a book that had the premise: "learn just enough assembly to be able to implement C features by hand; we'll show you how". Sadly, I don't remember the title. Another revelation much later on, was, as discussed here, the realisation that C is indeed defined over an abstract machine. I think much of those realisations were because of reading about how crazy compiler opt…

I have to ask, because I've wondered for a couple years now, is your name a One Piece reference?

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

#118

Earlier quoted context omitted.

We have our differences, but you’re totally correct here, and I’m not sure why you’re downvoted. The “byte addressable” thing is exactly why C was created over B, even, right? That was one of the crucial features not supported.

I don't think he is correct. The underlying representation of a pointer is not defined by the C standard. You could have a C implementation that works on segmented, non-flat architectures. Look at the C compilers from the DOS days, for example...

I think we're talking past each other, and in some ways, this is what the post is about.

The C abstract machine presents a flat, uniform, byte-addressed address space.[1] The reason that it does not define what a pointer's representation is is because it needs to map that to what the hardware actually does, which is your point.

1: Actually, my impression is that it does, but this is actually an area of the spec in which I'm less sure. I'm going to do some digging. Regardless, the point is that the memory model != the machine model, which is your point.

EDIT: For example, https://en.cppreference.com/w/c/language/memory_model

> The data storage (memory) available to a C program is one or more contiguous sequences of bytes. Each byte in memory has a unique address.

Though cppreference is not the spec itself, of course. It is the conceptual model that's often presented.

LAST EDIT: I happened to run into someone who seemed knowledgeable on this topic on Reddit: https://www.reddit.com/r/programming/comments/9kruju/should_...

TL;DR no! This is another misconception. Tricky!

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

#119
C teaches you how a computer works because the C abstract machine is defined such that operations that must be manually performed in assembly language must also be manually performed in C.

C doesn't let you write something like:

    string x = y;
...because to actually create a copy of a string the computer must allocate memory, copy memory, and eventually free the memory. In C each of these steps is manual. Higher-level languages support high-level operations like the above, which obscures how much work a machine has to actually do to implement them.

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

#120
Good article.

If your goal is to learn how computers work I think an education in C will follow a long, twisting pedagogy. It is more likely you will encounter subjects that will teach you how computers work by using C. However you can also happily program in C without understanding how memory is managed beyond developing an intuition for using malloc, free, and pointers.

If you want to be more direct why not build a computer from scratch [0]? Or take a course on operating systems [1]?

[0] https://www.nand2tetris.org/ [1] https://wiki.osdev.org/Getting_Started

Post reply on HN