4 ms·
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
by munificent 8y ago
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.
- steveklabnik 8y agoThank you! > 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. I think this is a particularly great point in your post. I wonder what AndyKelley thinks of this, as in my understanding, that's sort of what Zig is trying to do as well. That is, Zig is attempting to be a language on a specific spot on the price/performance ratio, as it were.
- nwmcsween 8y agoI think a new language has to be relatable with another and have some sort of huge defining feature(s) in order to get some sort of momentum, rust has rubyists(still one of my favorite languages)/c++ and safety, hopefully zig has something as well.
- AndyKelley 8y agoI agree with this characterization. Zig is trying to directly replace the niche that C represents, in terms of exactly this tradeoff. So if Zig is successful, in 4 years the title of this post would have been "Should you learn C/Zig to 'learn how the computer works'?" and in 8 years the title would have been "Should you learn Zig to 'learn how the computer works'?". :-)
- steveklabnik 8y agoExcellent :)
- tralarpa 8y agoI still don't understand why you should learn C if you really want to know "how the computer works". The author says "By learning C, you can learn more about how computers work", but why taking the long path if the short path exists? Just learn assembly language. It doesn't take that much time, a few weeks are enough to get a good insight into how simple CPU work (registers, memory, no difference between numbers and pointers, a stack that grows, etc.). Why spending time on learning C if you already know that this is not what you actually wanted? That's like wanting to learn German and then learning Dutch because you heard it's easier than German. To all the people who are going to reply "But machine language is not how the CPU works!!1!!111": Yes, I know that real modern CPUs translate the machine code to something else internally and that they do things like register renaming and microinstruction reordering and they have ports and TLBs and instruction caches and data caches etc. But CPUs still have registers and an instruction pointer and most of them have a stack pointer and interrupts and exceptions. C will definitely NOT help you to understand those things.
- munificent 8y ago> if you really want to know "how the computer works". This depends on what the person is really trying to learn. If they are just interested in learning how a simple CPU does what it does, then, yes, going straight to an assembly language for some old chip from the 80s is a good idea. Writing an emulator is a fun and instructive exercise. But my impression is that most programmers who want to learn how the computer works want to because they want to be able to write more efficient software. They want to understand how computer performance works. Learning a simple CPU is anti-helpful for that. It leads you to believe wrong things like "all memory access is equally fast". If you want to learn how to write faster code, C is pretty good because it strips away the constant factor overhead of higher level languages where things like GC, dynamic dispatch, and "everything is a reference" mask hardware-level performance effects. It lets you control layout in memory, so you can control data cache effects. Without a VM or interpreter overhead, most of the branches the CPU takes will be branches you explicitly write in your code, so you can control things like the branch predictor.