Live data from Hacker News

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

words.steveklabnik.com

11–20 of 381 posts

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

#12
post #5

Earlier quoted context omitted.

Funny enough, pointers are a great example of an abstraction provided by C that people assume is isomorphic to hardware, when they don't: https://blog.regehr.org/archives/1621 That said, I 10000% agree that pointers are a thing that is great to learn. > You really should understand how things like .size or lengh() aren’t “free” and how they work. You're talking about strings here, right? This is not true in all langu…

> You're talking about strings here, right? This is not true in all languages, but certainly is for C :) It costs O(1) in memory to delimit strings with a null terminator and O(n) in time to execute strnlen(), right? It's not free. Though, if you're lucky your string is in rodata and the compiler can calculate the sizeof() in advance, this is very-nearly-free. EDIT: misunderstanding, disregard

That assumes that all strings are C strings which isn't true. For example, Pascal-style strings have a size before the string data, and Rust and C++ strings have the size as metadata on stack and the string data in heap.

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

#13
I taught myself atmega328 C programming in a breadboard blinking led context last year and thought that was a wonderful learning experience. I sort of knew C, and didn't get much pointer practice, but it was great nevertheless.

Also, Stevens Advanced Programming in the Unix Environment is a great book to work through.

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

#14

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…

IIRC there was some UB feature that when compiled with GCC would launch nethack.

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

#15
Coming from an operating system background, I find the article's use of "virtual machine" very bizarre. The article confuses very different things by stating that "'runtime', 'virtual machine' and 'abstract machine' are different words for the same fundamental thing".

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

#16

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…

IIRC there was some UB feature that when compiled with GCC would launch nethack.

Found it:

>When GCC identified “bad” C++ code, it tried to start NetHack, Rogue, or Towers of Hanoi. Failing all three, it would just print out a nice, cryptic error message.

https://feross.org/gcc-ownage/

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

#17

I learned assembly language before c which I suspect helped understand pointers. There are plenty of fun ways to learn assembly language. The game Shenzhen IO is one.

I also learned (m68000 & x386) asm before I learned C and subsequently C++ and I agree that having some asm knowledge helps learn how the machine works and how the higher level languages work as well. As for fun ways to learn asm, I'd recommend Core War (https://en.m.wikipedia.org/wiki/Core_War) - I had a lot of fun with that back in the day. But, I also want to say; If you plan on learning modern C++, skip C. You will just have to unlearn a bunch of stuff when you switch.

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

#18
post #5

Earlier quoted context omitted.

> You're talking about strings here, right? This is not true in all languages, but certainly is for C :) It costs O(1) in memory to delimit strings with a null terminator and O(n) in time to execute strnlen(), right? It's not free. Though, if you're lucky your string is in rodata and the compiler can calculate the sizeof() in advance, this is very-nearly-free. EDIT: misunderstanding, disregard

Only if your strings are null terminated; many languages do not use null termination for various reasons, and that's one of them. "Pascal strings" being the nickname, given how old this idea is, and given it was a direct competitor to C at the time.

> > You really should understand how things like .size or lengh() aren’t “free” and how they work.

> You're talking about strings here, right? This is not true in all languages, but certainly is for C :)

I realize now that I misread this. It sounded as if you were saying '...but it is [free] for C', and on re-read I now understand that's not what you said at all.

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

#19
Learning C has practical benefits, but gaining an understanding of how computers work is not one of them.

Learning an assembler would be more helpful in this regard, but in my opinion one might want to start with a very well-written article by Mark Smotherman: https://people.cs.clemson.edu/~mark/uprog.html

If you want to see how a CPU works, I highly recommend visiting http://www.visual6502.org/

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

#20

Earlier quoted context omitted.

Only if your strings are null terminated; many languages do not use null termination for various reasons, and that's one of them. "Pascal strings" being the nickname, given how old this idea is, and given it was a direct competitor to C at the time.

> > You really should understand how things like .size or lengh() aren’t “free” and how they work. > You're talking about strings here, right? This is not true in all languages, but certainly is for C :) I realize now that I misread this. It sounded as if you were saying '...but it is [free] for C', and on re-read I now understand that's not what you said at all.

Ah, no worries! I've edited it to make it more clear, hopefully.
Post reply on HN