Live data from Hacker News

Ask HN: Learning C

news.ycombinator.com

31–40 of 66 posts

Re: Ask HN: Learning C

#31
post #3

Sure! Knowing C can definitely help with your overall understanding of how things work. It's especially helpful when learning new languages. I uploaded a copy of K&R ANSI C for you. In my opinion, it's probably the best way to learn it. http://whitepaperclip.com/files/ansi_c.pdf Have fun!

Is this book free now? If not, you should kill that link.

Re: Ask HN: Learning C

#33
post #25

Best beginner text after K&R is Kelly and Pohl's "A Book on C"; very newbie friendly and at the same time uses traditional C examples (i.e. memory management, serializing and reading-back record-oriented data stored in structs, file and directory manipulation, string manipulation, etc.) Most other C books will just treat it as a stepping stone for C++ which is a different beast, and a few awful offenders will stretch…

I actually picked up and then put down a good looking book on Unix systems programming either by Stevens, or by someone who worked very closely with him and wrote an updated version. I think I'll scream off to the book store and track it down.

Re: Ask HN: Learning C

#35
1. In Which I Join The Choir

You should definitely learn C:

* You'll learn the memory hierarchy, which is probably the most important thing to know about performant software.

* You'll get 100x better at debugging --- both because you'll have practice, and because most of the tools you use to debug C code work for higher-level languages (gdb is still a better Ruby debugger than Ruby's own debugger).

* You'll be able to bridge Python (or Perl) to almost any library or framework you ever need.

2. In Which I Express Reservations

Don't learn assembly. In my (C and assembly-heavy) career, I've found it works in exactly the opposite direction: you won't really understand assembly until you understand C code. There are mainstream architectures (SPARC and MIPS) that are literally designed as drivers for C compilers.

You'll eventually want to be conversant in assembly so that you can debug faster (when it comes to the runtime, the C language is a flimsy abstraction indeed). But you should let your needs and interests drag you into the machine, just like you should probably learn "Stairway" and "Smoke On The Water" before mastering barre chords and sight reading --- you can have a pretty excellent punk band without ever knowing how to read music.

3. There Are Books

Everyone is going to have an opinion about K&R. What I think you need to know is, it's short and it's dry. I re-read it 6 years into my career and I was surprised at how rich it was; there's a lot of design and data structures material, beyond the core language.

There's a C book that changed my life. It's David Hanson's "C Interfaces And Implementations" (CII). I will now make a case for why it's the first and only book you'll need. You're coming to C from a high-level language. That language is giving you a couple key things you don't realize you depend on:

* Garbage collection

* Lists and hash tables

* Resizeable strings

Almost every major C project contains a site-specific reimplementation of these concepts. CII gives you one --- an internally consistent one, relatively well implemented, and extremely well documented. In doing so, it also teaches you the single most important thing about writing good C programs, which is how to build abstractions around your gnarly code.

The difference between people who know C as a language and people who can actually deliver software in it is resizeable containers (linked lists, dynamic memory allocation, high-level strings, doesn't really matter what the specific is).

The other book you want is "Advanced Programming In The Unix Environment" (or its moral equivalent, "Win32 Programming").

CII is "how". APUE is "what".

Finally, I like (for new programmers) "The Practice Of Programming".

4. What I Did To Get Started

I "knew" C for a while (maybe since I was 13) before I actually started coding in it. Two things got me unstuck.

First, I picked a couple basic Unix APIs and wrote little toy programs around them. If I remember correctly, the first thing I wrote was "who", from "getpwnam(3)", which forced me to do some basic strings and structure pointers.

Second, I wrote network code from a socket tutorial. Sockets are a great thing to get started on; just write a scraper for a service you like, and there's a zillion little problems you'll have to solve that are the same problems you solve in every other program you write.

Re: Ask HN: Learning C

#36
post #25

Best beginner text after K&R is Kelly and Pohl's "A Book on C"; very newbie friendly and at the same time uses traditional C examples (i.e. memory management, serializing and reading-back record-oriented data stored in structs, file and directory manipulation, string manipulation, etc.) Most other C books will just treat it as a stepping stone for C++ which is a different beast, and a few awful offenders will stretch…

I actually picked up and then put down a good looking book on Unix systems programming either by Stevens, or by someone who worked very closely with him and wrote an updated version. I think I'll scream off to the book store and track it down.

It's the updated version of APUE done by Rago (the guy who did Unix System V Network Programming; a fairly good book but with a narrow and almost obselete focus; sysv STREAMS -- a very good idea whose time hasn't come.)

Re: Ask HN: Learning C

#37
I learned C a while back, and don't regret the time spent doing so at all. This is how I'd recommend going about it:

* Everyone is going to recommend K&R, which is fine. Find a copy of C Interfaces and Implementations as well--very good implementations with explanations of standard high-level structures.

* Read source. SQLite's source, for example, is very well written and documented, and they actually have a version to download recommended for reading (everything is in one file).

* Implement. You're going to want to try implement standard Unix commands yourself. Try writing an interpreter/compiler as well. These things give you a good grip on a wide variety of C basics and will make sure you can work well with memory management and all that.

* Assembly. This is where I would learn assembly (i.e., where I did). I don't think you need to go too far in depth, but a basic working knowledge will make debugging much, much easier.

Re: Ask HN: Learning C

#38
post #11

Earlier quoted context omitted.

I'd second this. In fact, Steve Yegge once said this about K&R ( http://steve.yegge.googlepages.com/ten-great-books ): This is an odd little book. It's frequently mistaken for an introductory programming book, which inevitably leads to frustration. It's not a good way to learn how to program; it expects that you're already familiar with machine architecture, assembly language, compilers, and at least one other high-l…

I know the plural of anecdote is not data, but my personal experience was the opposite. C was the first programming I learned and I learned it by reading K&R in high school. I certainly didn't know anything about assembly, compilers, or another high-level language. From there I learned by contributing to open source projects. Not saying K&R is the best way, but it's not so opaque that a beginner with zero knowledge o…

K&R was the only C book that ever allowed me to pierce the veil of mystery surrounding C and understand it. I'd tried at least a half dozen other books and was either bored out of my mind at the painfully slow pace, or utterly baffled around chapter 2 or 3 where it leapt into pointers and seemed to just assume it was obvious.

For whatever reason K&R was a perfect storm of comprehension for me, and I came away actually knowing how to read and even sort of write C (and knowing how to search the Internet, pre-Google, or ask reasonably smart questions on the mailing list or IRC channel of the software I was working with, for answers to the harder questions). I failed miserably with every other book on the subject.

Re: Ask HN: Learning C

#39
C Primer Plus was completely useless to me. As I mentioned in another thread, I tried several books on C before K&R and none of them "took". I knew (by some definition of "knew") other languages (several BASIC variants, Pascal, REXX, Amiga shell, a little 6502 and 68000 assembler, etc.) when I started learning C...but it took years, and several failed attempts before I was actually able to make sense of real C code.

My book recommendations would be K&R, Advanced Programming in the UNIX Environment by Stevens, and C: A Reference Manual by Harbison and Steele. In that order. With liberal doses of web research and reading real world examples and experimentation.

Re: Ask HN: Learning C

#40
I taught myself C/C++ when I was around 13. I already knew most of the concepts through my experience with Pascal, including pointers, which are harder to use than to understand. What I did have trouble with was Windows as I just couldn't figure out the API. What solved it for me was one of those old 'Game Developers' series of beginner's books. It gave detailed instructions on for a 'hello world' Windows application. After that, it was pretty easy to get the hang of it.

I don't use C much anymore (haven't since Highschool.) When I do, it's with GCC and I and the environment is still the hardest part.

Moral of the story being that C is pretty easy. I learned it with some very basic references. The hard part, supposedly, is pointers, and those are much harder to use than to understand. Basically, everything is done through them, without the usual abstractions that you would get with say, Java. You mainly just have to make sure you always have the right number of asterisks and ampersands.

One book I can suggest is Advanced Programming in the UNIX Environment by W. Richard Stevens, (revised version) which gives a very through introduction to UNIX from a C perspective. C is interesting in that it is almost a different language depending on which system you write it on and you'll spend more time learning system calls and UNIX paradigms than C syntax and pointers.

Post reply on HN