[HN Gopher] Beej's Guide to C Programming
       ___________________________________________________________________
        
       Beej's Guide to C Programming
        
       Author : qwertyuiop_
       Score  : 199 points
       Date   : 2023-02-26 16:35 UTC (6 hours ago)
        
 (HTM) web link (beej.us)
 (TXT) w3m dump (beej.us)
        
       | beej71 wrote:
       | Hi gang! I know there are some incorrectitudes in there around
       | the inline part of the guide... but if you find others, I'd
       | really appreciate emails or GitHub issues or PRs.
       | 
       | 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? ;)
        
         | throwaway71271 wrote:
         | you are legend!
         | 
         | thank you so much for your work!
         | 
         | the socket guide was my home page for like 10 years :)
        
         | BruceEel wrote:
         | while you're here, I'm seizing this opportunity to thank you
         | for that wchar_t section, it really is good work.
         | 
         | As for C23, quality takes time :)
        
           | beej71 wrote:
           | That was the second-hardest part to write (behind atomics).
           | Glad to hear it's useful, for sure.
           | 
           | I envy modern languages that were able to simple shrug their
           | shoulders of decades of history and declare "UTF-8
           | everywhere!"
        
             | ninepoints wrote:
             | Even those languages have to contend with win32 utf16
             | conventions anyways I suppose
        
         | vasco wrote:
         | Thanks a lot for your networking materials. 10 years ago it got
         | me through a hard networking course in university and it showed
         | me how good teaching materials could be. I'm not programming C
         | professionally but your stuff made me a better programmer.
        
         | fisf wrote:
         | Just wanted to throw out a thank you.
         | 
         | 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.
        
       | jamal-kumar wrote:
       | Is there something like a changelog to this? I remember years ago
       | people on freenode #C pointing out tons of mistakes in this [1]
       | and trashing it as a reference to learn from but it seems like
       | it's gone through a decent amount of revision at this point.
       | 
       | [1]
       | https://www.iso-9899.info/wiki/Main_Page#Stuff_that_should_b...
        
         | beej71 wrote:
         | Yeah, any history before git is lost aside from what you might
         | find on archive.org.
         | 
         | 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.
        
           | jamal-kumar wrote:
           | Cool to hear from you as the author, thanks for the
           | reference! I'm now pretty curious to check out your network
           | programming guide.
        
         | RicardoLuis0 wrote:
         | it seems to be on github, but the commit history doesn't go
         | beyond 2017: https://github.com/beejjorgensen/bgc
        
       | teknopaul wrote:
       | Re: "You can declare a function static if you only want it
       | visible in a single source file."
       | 
       | 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.
        
         | david2ndaccount wrote:
         | It's static as it will always resolve to the same function. An
         | extern function depends on whatever you link against (or users
         | link against if you provide a static library).
        
       | dang wrote:
       | Related:
       | 
       |  _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)
        
       | leourbina wrote:
       | Just wanted to say thank you. your content was crucial in getting
       | me to understand network programming and was crucial for my
       | masters thesis. Your content has always been crisp, to the point
       | and accesible. Keep it up!
        
       | anthomtb wrote:
       | Beej's site is a gem. I remember coming across it in 2010 or so,
       | thinking, yeah this stuff is great but what happens when this guy
       | gets kids or a "real job" and the bit rot sets in?
       | 
       | Here we are, closing in on 15 years later, and it's both still-
       | great and up to date.
        
       | fsociety wrote:
       | I started my career solely as a C dev and got pretty good at it.
       | This is by far the most useful resource whenever I have to dip my
       | toes back into it.
        
       | version_five wrote:
       | I was actually just looking for a C book. I already "know" (I use
       | the term loosely) C but have little insight into how to write
       | real code, and any best practices.
       | 
       | Will check this out.
        
         | bigdict wrote:
         | > how to write real code
         | 
         | There are many ways, a codebase would be a better resource than
         | a book.
        
           | beej71 wrote:
           | I agree. The Guide doesn't give many real-world examples
           | (there are zillions of those already out in the real world).
           | It's about how to swing a hammer and turn a screwdriver, not
           | how to build a house.
           | 
           | That said, I try to make K&R-idiomatic examples when I can.
           | They're just small-scale.
        
           | CamperBob2 wrote:
           | That's tricky, though. It would be a bad idea to learn C++
           | from _my_ code, even though it works well enough.
           | 
           | And until you're well along the path to guruhood yourself,
           | you won't know if you're reading good code or bad code.
        
         | c-c-c-c-c wrote:
         | Get a VM running NetBSD or OpenBSD and check out the source
         | code and play with it!
        
       | cwdegidio wrote:
       | I just have to say thanks to beej71 for making this guide. I'm
       | doing my Masters in CS right now (I'm predominately a web
       | developer... js, java, etc) and only used C++ one time in my
       | undergrad. Your guide was my go to resource when doing things
       | like an echo server/client and implementing a barebones http
       | protocol. Can't thank you enough.
        
       | ratherlongname wrote:
       | Your guide to network programming got me through a difficult
       | course in my Bachelors! Thank You!
        
       | thrwawy74 wrote:
       | Not to be pander, but I wonder if anyone has worked on a
       | translation of this to Rust.
        
         | doodlesdev wrote:
         | Tell me you use Hacker News without telling me you use it
         | hahahaha.
         | 
         | 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/
         | 
         | [1]: https://github.com/rust-lang/rustlings/
         | 
         | [2]: https://doc.rust-lang.org/stable/rust-by-example/
        
           | steveklabnik wrote:
           | As someone who helped write one of those guides... I would
           | encourage people to write even more. Different texts can help
           | different people. The more the merrier!
        
         | beej71 wrote:
         | I just want to go on record as saying there will never be a
         | *comprehensive* Beej's Guide to Rust. There will never be
         | another comprehensive Beej's Guide to Any Language, for that
         | matter. I've learned my lesson. :)
         | 
         | 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.
        
       | blonky wrote:
       | Beej is a great guy. He was my instructor at this boot camp I
       | attended. I learned a little C from him. I can't really do much
       | with it, but it was fun to learn.
        
       | incanus77 wrote:
       | The second volume reference is great too, and downloadable as
       | zipped HTML and PDF.
       | 
       | https://beej.us/guide/bgclr/
        
         | beej71 wrote:
         | This I modeled off The Turbo C Bible, one of my favorite all-
         | time reference books that still sits on my shelf. I thought its
         | killer feature was examples for everything.
        
       | jjgreen wrote:
       | Pfft, what a language, it doesn't even have telemetry.
        
         | CamperBob2 wrote:
         | For a while, you had to go out of your way to add
         | notelemetry.obj to your linker input to keep Microsoft from
         | adding _their_ telemetry to _your_ program.
         | 
         | I don't think they do that anymore, but it's awfully hard to be
         | certain.
        
       | simonw wrote:
       | Beej's guide to network programming is an absolute classic,
       | dating back to at least 2005 (according to the Internet Archive)
       | - very exiting to see them tackle C programming generally now.
       | 
       | https://beej.us/guide/bgnet/
        
         | elromulous wrote:
         | Beej is often on here and can confirm the actual inception
         | date, but iirc I used it as far back as at least 1999/2000ish
        
           | tesseract2 wrote:
           | Indeed! I remember first seeing it in late 2000 or early
           | 2001, and it wasn't brand new at that time.
           | 
           | I used it as a companion to Richard Stevens' classics on
           | TCP/IP.
        
             | pschw wrote:
             | From his bio:
             | 
             | > I'd started off with Beej's Guide to Network Programming
             | back in 1995 or so
        
               | beej71 wrote:
               | I don't remember if I said, but I got started learning
               | this stuff from a networking program someone had written
               | to allow you to play DOOM over the modem, IIRC. So thank
               | you to whomever that was!
        
               | moonshinefe wrote:
               | And thank you for writing the networking guide. I found
               | it in my early teens when I was interested in learning
               | programming and wanted to go beyond the basics to do some
               | more interesting stuff.
               | 
               | It helped me write some chatroom bots for things like IRC
               | and battle.net in the early 2000s, which fueled the hobby
               | even more and eventually turned it into a very good
               | career. :)
        
         | Accacin wrote:
         | Yeah! Last week I decided I'd like to write a (very) simple
         | HTTP server in C as a learning experiment. The network
         | programming guide was incredibly useful and the code examples
         | gave me a great base to learn from and extend.
        
       | tialaramex wrote:
       | > it is connected to the bare metal in a way that present-day
       | languages are not.
       | 
       | 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!
        
         | swatcoder wrote:
         | The key difference to me is where you use "probably". Rust and
         | even WASM reflect a lot of the system behavior that's become
         | the norm among modern hardware platforms.
         | 
         | 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.
        
           | saagarjha wrote:
           | Amusingly Rust is needing to revisit some of the earlier
           | choices it made that were "obvious" on modern hardware
           | architectures, because we have even newer ones that don't
           | behave that way.
        
         | i-use-nixos-btw wrote:
         | I'm no rustacean, but I agree - for vastly different reasons,
         | though.
         | 
         | 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.
        
       | waynesonfire wrote:
       | is there something like this for Common LISP?
        
       ___________________________________________________________________
       (page generated 2023-02-26 23:01 UTC)