Live data from Hacker News

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

words.steveklabnik.com

351–360 of 381 posts

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

#351
post #90

Earlier quoted context omitted.

You can get that as well with NEWP, PL/I, Extended Pascal, BLISS, Modula-2 and tons of other system languages.

Maybe but there's a reason C emerged as the winner because none of those systems really has an established reference model to look at. C has several - e.g. UNIX being written in C.

UNIX being available for free with source code is what helped C gain adoption.

The nomimal fee universities had to pay AT&T was meaningless compared to what something like VMS or OS/360 would cost them.

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

#352
post #39

Earlier quoted context omitted.

That bread analogy is on point. The old quote comes to mind, >A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyse a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die g…

Learning to read Ancient Greek should be on that list.

I would add being able to clean & jerk your own body-weight, swim a mile, and be able to barter with anyone from any culture.

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

#353
Looking at asm dumps of (non-optimized) C code is a great learning tool to understand "how the computer works"(see https://godbolt.org/ ) . C by itself is not, but building up knowledge from assembler doesn't touch how actual(C-level) software works. If you study only assembler there is a large information gap from primitive opcodes to software functions and general architectural features that opcodes only briefly interact with, to 'get' overall design you have to study C/C++ implementations.

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

#354
post #327

Earlier quoted context omitted.

...like returning a pointer to the stack: char *dupstr(const char *src) { char new[1024]; strlcpy(new, src, 1024); return new; }

stack_ret.c:5:10: warning: function returns address of local variable [-Wreturn-local-addr] return new;

Why is this a warning and not an error? Are there situations in which you would want to return the address of a local variable?

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

#355
post #253

Earlier quoted context omitted.

Why do you claim that local variables and function calls are specific to C? They seem to be very popular among programming languages in general.

Because I'm an assembler coder, and when one codes assembler by hand, one almost never uses the stack: it's easy for a human to write subroutines in such a way that only the processor registers are used, especially on elegant processor designs which have 16 or 32 registers.

I surely did use the stack a lot, back in the old days when coding Z80 and 80x86 Assembly.

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

#356

Depends on what do you mean by "work" here, which is a very vague term. One can argue that assembly doesn't teach you how computers work either because it "abstracts away" how transistors do their job. The point is: Depending on how deep you want to go in your understanding of the underlying system, there is always a level of abstraction that you have to settle on before building your knowledge on top of it. Turns ou…

I can get all of that from languages like Basic, Pascal or Ada, while enjoying safer code.

https://www.mikroe.com/compilers

https://www.ptc.com/en/products/developer-tools/apexada

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

#357
post #355

Earlier quoted context omitted.

Because I'm an assembler coder, and when one codes assembler by hand, one almost never uses the stack: it's easy for a human to write subroutines in such a way that only the processor registers are used, especially on elegant processor designs which have 16 or 32 registers.

I surely did use the stack a lot, back in the old days when coding Z80 and 80x86 Assembly.

You almost had to, because both Z80 and x86 processors have a laughably small number of general purpose registers. To write code which works along that limitation would have required lots of care and cleverness, far more than on UltraSPARC and MC68000.

On MOS 6502 we used self-modifying code rather than the stack because it was more efficient and that CPU has no instruction and data caches, so they couldn't be corrupted or invalidated.

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

#358

Earlier quoted context omitted.

C is their craft and they're experts so what's not to like? I'm not sure what this 'good enough' angle is and why you think it's specific to C. It's an issue to other language more than C as those other languages are often used by shops without proper software engineering practices in place. All the places I've worked which use C enforce those SW practices but it's not clear to me that the same could be said for othe…

You keep talking about Python's problems, and I'm trying to talk about the habits I've noticed C programmers form and are rewarded for, but are bad for Python development. There are literally no habits in your mind that could be good in C but bad in Python?

As I am a veteran C programmer with decades of experience with several years of straight python now, what I see is a people who think they understand how to write complex software but don't grasp the fundamentals. This isn't explicitly a C V Python programmer discussion so much as exposing that Python insulates you from the peculiarities of things like debugging complex problems, writing robust code, good software practices, understanding libraries, scalability, parallel programming. The list is endless for the weaknesses of python and the developer who is comfortable there, which is fine given Python is a scripting language at heart.

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

#359
post #63

Earlier quoted context omitted.

But in addition to the mismatch between the abstractions provided and the real hardware, C qua C is missing a huge number of abstractions that are how real hardware works, especially if we pick C99 as Steve did, which doesn't have threading since that came later. I don't think it has any support for any sort of vector instruction (MMX and its followons), it doesn't know anything about your graphics card which by raw…

A computer does not need to implement a stack What general purpose computer exists that doesn't have a stack? Push/pop have been fundamental to all the architectures I've used.

From the perspective of "stack" as an underlying computation structure: Have you ever played a Nintendo game? You've used a program that did not involve a stack. The matter of "stack" gets really fuzzy in Haskell, too; it certainly has something like one because there are functions, but the way the laziness gets resolved makes it quite substantially different from the way a C program's stack works.

If a time traveler from a century in the future came back and told me that their programming languages aren't anywhere as fundamentally based on stacks as ours are, I wouldn't be that surprised. I'm not sure any current AI technology (deep learning, etc.) is stack-based.

From the perspective of "stack" as in "stack vs. heap", there's a lot of existing languages where at the language level, there is no such distinction. The dynamic scripting language interpreters put everything in the heap. The underlying C-based VM may be stack based, but the language itself is not. The specification of Go actually never mentions stack vs. heap; all the values just exist. There is a stack and a heap like C, but what goes where is an implementation detail handled by the compiler, not like in C where it is explicit.

To some extent, I think the replies to my post actually demonstrate my point to a great degree. People's conceptions of "how a computer works" are really bent around the C model... but that is not "how a computer works". Computers do not care if you allocate all variables globally and use "goto" to jump between various bits of code. Thousands if not millions of programs have been written that way, and continue to be written in the embedded arena. In fact, at the very bottom-most level (machines with single-digit kilobytes), this is not just an option, but best practice. Computers do not care if you hop around like crazy as you unwind a lazy evaluation. Computers do not care if all the CPU does is ship a program to the GPU and its radically different paradigm. Computers do not care if they are evaluating a neural net or some other AI paradigm that has no recognizable equivalent to any human programming language. If you put an opcode or specialized hardware into something that really does directly evaluate a neural net without any C code in sight, the electronics do not explode. FPGAs do not explode if you do not implement a stack.

C is not how computers work.

It is a particular useful local optimum, but nowadays a lot of that use isn't because "it's how everything works" but because it's a highly supported and polished paradigm used for decades that has a lot of tooling and utility behind it. But understanding C won't give you much insight into a modern processor, a GPU, modern AI, FPGAs, Haskell, Rust, and numerous other things. Because learning languages is generally good regardless, yes, learning C and then Rust will mean you learn Rust faster than just starting from Rust. Learn lots of languages. But there's a lot of ways in which you could equally start from Javascript and learn Rust; many of the concepts may shock the JS developer, but many of the concepts in Rust that the JS programmer just goes "Oh, good, that feature is different but still there" will shock the C developer.

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

#360

Earlier quoted context omitted.

But, I mean, how useful is that point? Virtually everything, including the X86 ISA, is an "abstract machine".

So, this post isn't attempting to address the usefulness angle; that's going to be in the two follow-ups. But I'll give you a summary of where I'm going with this, because you were actually in the back of my mind when I was writing this, and I'm interested in your thoughts. First though, this post. The first reason that this is useful is that there are a lot of people out there who believe that C is somehow fundament…

I think if you'd led with the UB thing, the notion of C targeting an abstraction rather than being an abstraction would have been more compelling to me (the perf argument gets back to a place where basically everything in every language is artifacts and leaky abstractions; it's worth visiting but doesn't persuade me in either direction).

I guess my issue, even with the whole outline laid out (thanks!), is that while C "isn't how the computer works" in sort of the same sense as a SPICE model isn't really how a circuit design works and ns isn't really how a network works, it's close enough to be illuminating in ways most other languages aren't (and has the virtue of most mainstream hardware being explicitly designed to make it, and particularly it, faster).

I think more developers should know C (though I think almost nobody should write in it anymore).

Post reply on HN