Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: What are some good resources for learning C?
13 points by arte on June 5, 2015 | hide | past | favorite | 21 comments
So far I've only come across scripting languages (Python, Node.js). I'd like to learn to program in C, for a number of reasons, but I'm not sure where to start.


K&R "The C Programming Language" is a classic. If you're serious about learning C this book should definitely be in your collection. It's the book most C programmers have cut their teeth on over the past several decades, written by the guys who wrote the language itself. (Though go for the most-current ANSI version). If you already know basics of programming methods from Python or JS, you'll be able to handle this book. Make sure to do the exercises!

Beyond that, it would depend what you're hoping to do with C. Eg, Linux kernel hacking? Optimized Python numpy routines?

There also are some recent books that focus on writing 'modern' and also safer C code, particularly with regard to usage of pointers. There are some links on Amazon and O'Reilly that may be helpful (I haven't read any so can't give recommendations)


> Linux kernel hacking?

An excellent resource here as a beginner would be- http://www.advancedlinuxprogramming.com/


Pretty sure that one's for userspace programming. In which case, I'd recommend The Linux Programming Interface instead, which is written by the same person who maintains the Linux man-pages.

There isn't much of a central source on kernel hacking that isn't outdated by now, though some reasonably good books on device drivers are still out there and relevant. Beyond that, reading the kernel's Documentation directory, LWN articles and the recent "Linux Insides" series being posted here is your best bet, along with stalking the LKML, of course.


I have seen a few people suggest: http://c.learncodethehardway.org/book/

I bookmarked this discussion a little while ago which has several good links: https://news.ycombinator.com/item?id=9333520

My suggestion is always to work on a real project but for that you need to know the basics of course.


I enjoyed Mastering Algorithms With C. After going through that book I was able to actually get things done. I recommend this because it walks through the implementation of basic data structures that you will be working with no matter what you're doing.

K&R is a good reference, but I only found it useful after I knew enough to actually get things done. I didn't find it good to learn with, and I was already proficient in a couple of scripting languages.

I also recommend Richard Stevens books. Advanced Unix Programming and Unix Network Programming are excellent. I'm not sure what you want to work on, so that might not be relevant.

For studying how non-trivial things are implemented I like TCP/IP Illustrated Volume 2, and The Design and Implementation of the Freebsd Operating System.

One of the things you're going to find about C that's different from higher level languages is that it's so minimal that you're going to have to build up a personal library of code to get things done.

For code samples, I like to look at OpenBSD. It's all in one place and there's less cruft than most of the alternatives. This is useful because while the key tasks of a particular program might be described well in one of the books I mentioned, it's not the same as a real modern program. I've found it really helpful to actually look at something that's stood the test of time for inspiration. Also, it's man pages are awesome. I look at them often even when writing code on other operating systems.

And remember, valgrind is your friend.


Thanks, this was very useful.


I am currently going through CS50 online. You can join through edX or here:

https://cs50.harvard.edu/

Pretty good first steps intro that eventually goes into algorithms and data structures.


As someone who has tried this myself, there are no solid 'tutorials' or 'guides'. Unless you are extremely versed in documentation-absorption it will be quite the uphill battle.

That said, if one of your reasons isn't 'iOS' (in which I would recommend Swift anyways), and you are instead moreso interested in learning a systems language period, I would heavily recommend diving into Rust.

The documentation/introduction book is wonderfully written and a breeze to go through. I have found the Rust team to be a wonderful group of guys that are extremely responsive on SO, IRC, etc.

HN users pcwalton and steveklabnik both have many wonderful comments in Rust threads that go into the minutiae of why it is so powerful. I had found steveklabnik's book "Rust for Rubyists" wonderful, but it is since outdated since the official Rust book has become concentration (www.rustforrubyists.com confirms this) but the book includes some 'ruby inside of rust' examples etc.

Good luck in your journey!


Thank you so much for the kind words <3

While I did take the site down, you can find the old source for Rust for Rubyists at https://github.com/steveklabnik/rust_for_rubyists still.


No problem. Keep up the wonderful work :)


I am looking to learn a systems language yes. I'm going to go ahead with C right now, but Rust is something I've been looking at, and I'll definitely be picking it up in the future.


Out of curiosity (not ragging on your decision at all) why are you going this route?


There are no good resources for learning C. All the good, accurate books on C do not serve as tutorials.

It is impossible to write a tutorial in C that:

- targets newbies (particularly programming newbies);

- is comprehensible to that target audience; and

- isn't chock full of inaccuracies and outright lies that have to be unlearned later.

Sorry, no space here to prove this, but anyway, since it is impossible, one doesn't exist. So, good news, you don't have to waste time looking for it.

Use a reliable reference instead and struggle along for a bit.

I would recommend Harbison and Steele's C: A Reference Manual: http://careferencemanual.com/

(Yes, it's that Guy Steele.)


What's wrong with K&R?


Firstly, the K&R2 is outdated. It was published in 1988, a year before ANSI C. It has not been updated since. As a reference, it serves only long-time programmers who steadfastly stick to ISO C 90.

The K&R and K&R2 can be likened to sales brochures for the C language, which promote the language to users of other systems programming languages and provide a mini-reference to get them started.

I started learning C circa 1990 from K&R2. A few years later I started reading the ISO C standard and realized that much of what I knew was wrong. I had a "wow, so that's* how it actually is"* moment on what seemed like every page. It's not that the K&R2 explicitly teaches wrong things---obviously not; rather, it misguides the inexperienced by omission. You're expected to be an expert programmer who reads between the lines, asks the right questions about the limitations of constructs and consults a reference manual or standard to have them answered. The book is also not a tutorial on programming, either.

Thus if you're a n00b, you will not learn programming from K&R2 and you will not learn C properly. A combination of K&R2 together with Andrew Koenig's C Traps and Pitfalls might be better. If you have those two books, plus the Harbison and Steele reference manual and a PDF of a draft standard, you're in very good shape as far as being well-informed. You still don't have a great tutorial though.


There's nothing wrong with it, but it was written for the sort of people who were already employed in places like Bell Labs, so it sort of assumes familiarity with the way assembly code works. It's not so much that a smart person can't figure out how to write C code, but they will still have to pass arguments to the compiler or write a simple Makefile to get an actual working program.


One approach: Pick a simple project that you completed in a language you know. Now try to do it in C.

C isn't good for CRUD webapp type stuff, though. For example, if you tried to write a C program that processes a GET request, does a database lookup, and then serves a webpage, that's going to be a big effort. (There are some libraries out there that make this easier.)

Some important points:

- learn about declaring the types of your variables, a big difference compared with scripting languages

- learn about compiling vs interpeting

- learn about memory management and pointers (Higher level languages use references. The implementation of references is in C with pointers.)

- learn the common C standard libraries (stdio, stdlib)


Head First C by O'Reilly was pretty good. Shows how to use make and explains linked libraries in addition to just C semantics.


I am currently writing a small "Intro to Programming" textbook that uses C. If you email me (username @ gmail.com), I can send you what I have right now, along with updates.


Perhaps this excellent looking free resource, which I haven't closely looked at: Modern C.

It was discussed here [1], and also more recently submitted [2].

[1] https://news.ycombinator.com/item?id=9018247

[2] https://news.ycombinator.com/item?id=9647257


Allow me to ask, any book unrelated to C that made you appreciate, understand and write better C in the end ? e.g a book on Pascal, or Forth, or even something more Mathematical ?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: