284 points · by qwertyuiop_ · 3y ago · 64 comments
The goal is to have a comprehensive, free, zero ads or tracking, up-to-date guide to C that fits between cppreference and other higher-level books. Target audience is college students who suddenly have to learn C for their OS or Networking course. :)
I also haven't put any C23 stuff in there yet, but that's on deck. I have until December, right? ;)
Your guide was one of the first 'serious' programming material I was exposed to, close to 20 years ago.
Let's just say it was immensely helpful, and I might not be where I am now, without it.
As for C23, quality takes time :)
I envy modern languages that were able to simple shrug their shoulders of decades of history and declare "UTF-8 everywhere!"
thank you so much for your work!
the socket guide was my home page for like 10 years :)
I used it as a companion to Richard Stevens' classics on TCP/IP.
> I'd started off with Beej's Guide to Network Programming back in 1995 or so
Beej’s Guide to C Programming [pdf] - https://news.ycombinator.com/item?id=26911399 - April 2021 (171 comments)
Beej's Guide to C Programming - https://news.ycombinator.com/item?id=26100391 - Feb 2021 (1 comment)
Beej's Guide to C Programming (2007) - https://news.ycombinator.com/item?id=15198093 - Sept 2017 (79 comments)
Beej's Guide to C Programming - https://news.ycombinator.com/item?id=8295024 - Sept 2014 (1 comment)
Here we are, closing in on 15 years later, and it’s both still-great and up to date.
While obviously C is closer to the metal than say, Python, it's far less clear that this is true for a language like Rust† and I am increasingly dubious about the value of any of these high level languages (yes, even C) to learn "How the machine really works". I reckon it's probably more instructive to write a little bit of ARM assembly than to learn enough C to get to the same place today.
† Trivial example: Your CPU arithmetic operations probably natively behave exactly the way Rust's Wrapping<u32> Wrapping<i64> etc. types do. In C the unsigned integer types, once you figure out how big yours are, are similar, but the signed ones are not!
But those norms are still not universal and it’s useful to maintain an understanding of other viable architectures. C was born from a time of of extremely high innovation and experimentation in hardware architecture and it’s baked many of those possibilities into its language. Often, those are the very footguns that can make writing commercial software in C something to be considered carefully, especially because performant and safer alternatives like Rust exist.
Yet, using C is still sometimes the right choices for commercial software and remains a unique instrument for privately expanding one’s understanding of system and hardware architecture.
The quoted claim about C doesn’t have to be wrong just to argue that Rust might often be a better choice in practice for commercial bare metal programming on common systems. It’s making a subtly different claim, and the difference can matter.
What you can learn about in C is the C abstract machine, but the abstract machine is not any modern computer. It starts as a greatly simplified model of the PDP-11, a computer which hasn't been commercially relevant in decades.
So, on the one hand it's not much like the machines we own today, yet on the other hand it's also not like any real machine ever because all the complexities the C designers didn't express in their language are absent from the abstract machine. Sometimes this is an intentional deviation to make C simpler than a PDP-11, which is understandable but not so useful. Much worse though are places where they just didn't know.
The actual PDP-11 has specific semantics because it was a real thing, you could try stuff and find out. You could experiment. However the abstract machine is imaginary, and so where its properties were under-specified you can't just experiment there actually is no answer. WG14 (the C language committee) has tried, since it came into existence, to straight out some of this and choose definite answers, but it's a long way from finished.
So on the one hand, C is bad theoretically because it ignores much of what theoretical CS discovered in subsequent decades, but then on the other hand C is bad practically because its abstract machine models how somebody in the 1970s thought a 1970s computer worked, so it's wrong and it's 50 years old.
And again, I write these things as, IMNSHO an expert C programmer. I'm somebody who has found bugs in GNU libc and who wrote their own debugging tools so they could live debug problems which only happened in a production service. I'm not speaking out of ignorance, but out of understanding.
But I'm not sure how to draw that out of your original comment, or even how to interpret the original comment while "ignoring the Rust stuff entirely". That comment seems to be suggesting that Rust exposes its users to the bare metal just as much as C does, so if you take that comparison out, it doesn't seem like it's saying anything at all. C is flawed, and is not even ideal as a window into bare metal (because of its own abstract machine), but Beej noted that it's more connected than any "present-day" language, and that claim does seem to hold.
A type having similar algebraic properties to a set of assembly instructions isn’t what being close to the metal is about. It isn’t a thought experiment or learning experience - it is the ability to interact directly with lower level parts of the system, including the kernel and the hardware. It’s a great power that comes with great responsibility - a power and responsibility that safe rust will never, ever grant you.
Unsafe rust is essentially the same as C++ in terms of level of control. It allows volatile memory access, for example, and it allows embedding asm blocks. It basically gives you the abilities of C with additional tooling and compile time power, which is why I compare it to C++.
Safe rust would be quite far away from the metal. It provides a safety net that prohibits you from doing anything metal-like. The lower you go, the more “trust me, bro” and “hold my beer” you’ll need to incorporate, and safe rust is designed specifically to void bro-trusting, beer-holding situations. A kernel or a driver written in safe rust will never be possible.
But that’s where my terminology falls apart. Rust isn’t a choice of safe and unsafe, it is and will always be a dance between the two.
See if you can guess how those "similar [actually, identical] algebraic properties" are achieved when the Rust software is compiled to machine code. Keep in mind that Rust has a reputation for going real fast...
If you guessed that the answer is it produces exactly the same machine code, you're correct.
> Safe rust would be quite far away from the metal. It provides a safety net that prohibits you from doing anything metal-like.
Nope. This is a crucial misunderstanding, it supposes that Safe Rust is something like Java or even Python and it really isn't.
It's perfectly safe to implement wrapping integers using just the actual CPU's wrapping arithmetic instructions and so that's exactly what Rust does.
Very often the machine code emitted for a nice safe, expressive Rust program is identical to the code for the trickier to get right C program that looks more "bare metal" - if it copes with all the edge cases correctly. Example: C code that properly checks whether the thing it just got wasn't NULL, versus Rust code that writes if Some(thing) = ... thus pattern matching the "it's a thing" check. If you forget to check in the C code it faults (or worse) at runtime, same error in the Rust won't type check and doesn't compile. Once you've made both of them check the machine code when you compile them is identical, that if Some(thing) check will compile to the same conditional jump as the C not-NULL check.
Sometimes there's less machine code... for the Rust, because the compiler knows Rust isn't stupidly aliasing mutable variables, so it can avoid some spills that must be generated in C "just in case".
The general issue with safe rust in these circumstances is exactly what makes it safe in the first place - it wants to be firmly in the driving seat. Want to access memory? You can only do so if it is being “managed” by rust (an abuse of terms, really, as that management is done at compile time). But with device drivers, there’s a pretty good chance you’ll want to access memory at a certain fixed address, managed outside of your program, and there’s a pretty good chance that it is volatile.
Safe rust has already slammed on the brakes, exited the driver’s seat and is running in the rain just to get away from the thought of such things. Unsafe rust could easily manage it, otherwise embedded rust wouldn’t exist.
The JNode guys managed to do that in pure Java.
Rust, sure--in some cases, when you're using it in a C-like sense, could serve the same purpose.
[1] https://www.iso-9899.info/wiki/Main_Page#Stuff_that_should_b...
And I started writing it ages ago, then shelved in it a half-done state. And then it got that probably well-deserved review.
But its former self is barely recognizable since it has been heavily rewritten and expanded over the past 3 years, so I hope the iso-9899.info folks give me a regrade at some point in the future.
I don't think they do that anymore, but it's awfully hard to be certain.
Will check this out.
There are many ways, a codebase would be a better resource than a book.
That said, I try to make K&R-idiomatic examples when I can. They're just small-scale.
And until you're well along the path to guruhood yourself, you won't know if you're reading good code or bad code.
Static means different things in different languages, and in C in different places.
In what sense is a static C function static/unmoveable? or a static / class variable static in Java?
I find a lot of programming language guides tell you what without a coherent why.
C is one of the most simple languages there is, and it's YOUGE. There's a good reason other books skim over the library functions. The only saving grace is that C is relatively mature and (hopefully) doesn't change much every decade. Rust is changing so quickly it'd be a near-full-time job just to keep up.
Maybe I'll write some blog posts about Rust sometime as I improve with it. I think the language is all kinds of fun.
And the free Rust books and tutorials that are out there are already awesome.
But seriously, I'm not sure of any kind of material like this for Rust, but I believe that's because it's simply not needed since we already have "the book" [0], rustlings [1], and rust by example [2]. Honestly, learning Rust is just absurdly simple and straightforward if you have the motivation to do so, these guides are so good I don't see much of a good reason to develop different ones.
[0]: https://doc.rust-lang.org/book/