TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: Learning C

62 pointsby FiveFiftyOneabout 16 years ago
Throughout my career, I've viewed C as the sleeping monster that mere mortals such as myself dare not waken. However, as I increasingly seek to achieve more complex goals in my projects, I find myself encountering C libraries (such as GMime) which will answer all my questions, if I could actually program in C. I have a copy of C Primer Plus Fifth Edition at hand, a development FreeBSD server as well as my Mac, but would like to ask of this community if they are aware of any other references, such as old CS course materials, which may help. Many thanks in advance. I realise that this is a long term goal, but feel that with my taste in server platform and operations software, C will prove a boon to me.

31 comments

rsheridan6about 16 years ago
Just about everybody who answers this will say that K&#38;R is good, and I agree, but in addition to this I found that learning the basics of assembly was helpful. You can really see what's going on with the stack, what a pointer really is (pointers are really the only tough part about learning C, everything else will be the same old same old if you already know how to program), and the difference between a system call and a regular function call, and it doesn't take much time to learn.<p>Books by W. Richard Stevens were old when I was learning, and older now, but they were the best guides to taking it to the next levels. You can pick up a lot of tricks and stylistic pointers from him while learning about UNIX features and networking. Maybe somebody will chime in with a more up-to-date resource that does the same thing.
评论 #580390 未加载
评论 #580431 未加载
评论 #580430 未加载
评论 #580648 未加载
tumultabout 16 years ago
C isn't hard, don't worry! It's actually a fairly simple language. But it's medium/low-level and there is a lot of power within its simplicity. Fast to learn, but a long time to master, as one might say.<p>I don't know what people are using to learn C these days, but the standard has always been the white book <a href="http://www.amazon.com/Programming-Language-Prentice-Hall-Software/dp/0131103628" rel="nofollow">http://www.amazon.com/Programming-Language-Prentice-Hall-Sof...</a><p>You will find yourself spending more time thinking about programming concepts rather than dealing with C syntax, since it's pretty concise. I don't know how difficult it is to learn about pointers if you have already been programming for a while but haven't had to use references directly, but it's very important to learn. C does not do too much for you on its own, so handling references yourself is an important skill. It will also help you understand how higher level languages and concepts work.<p>If you are working through it and find that you're enjoying yourself, congratulations, you're a programmer :] Then it's time to pick up the 1st volume of Knuth and start working through that.
评论 #580380 未加载
mahmudabout 16 years ago
Best beginner text after K&#38;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.)<p>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 it to Java!<p>To see a few Algorithm implementations in C, there is no better friend than Robert Sedgwich's polyglot tome; it's the applied Knuth volumes that everyone keeps on their desk (quickly swapped for the actual Knuth volumes when expecting hacker guests.)<p>Along with C materials you will also need Unix materials. The two are inseparable and fuel each other. You already have a decent Unix in your Mac, and your FreeBSD is the best of the Unix breed bearing the original blood. For that there are no better companions than the books by Richard Stevens, both for system and network programming.<p>For larger scale software engineering you will need a few small to mid projects of your own. You will deal with header file and Make dependency problems, a few unix and processor portability problems and a few other stuff. The solution to those problems along with a fat library of useful routines are found in "C Interfaces and Implementations" by Hanson. It's a literate programming text where source code is interwoven with prose documentation and it's very self evident.<p>Along with productive C programming you might need to look into the darker side of C and Unix and follow the papers of the hacking underground (yes, HACKING, dispute the terminology all you want, but I think some blackhats deserve the noble noun more than javascript and CSS jockeys.) Unix and C have plenty of Not-TODOs to motivate plenty of caution. Hoglun and McGraw's Exploiting Software is the canonical C-Do-Not text books.<p>If you want to read library source code, your FreeBSD source code should give you plenty to keep you busy. Start with the sources for the games; I spent an enjoyable summer porting Minix games to DOS and taught me allot. There is also "The Standard Function Library", or SFL, google it, it has the cleanest C sources you ever seen. Highly recommended.<p>And should you ever want to write a C compiler, like I attempted, here is some advice: implement Oberon instead. Everything you could possibly learn at 1/100th of the headache.<p>Just the 2 cents of a guy who will only write C code again if there was a gun to his head ;-)
评论 #580633 未加载
评论 #580555 未加载
neilcabout 16 years ago
You should absolutely learn C. A good knowledge of C is a very valuable asset for all sorts of work, especially on Unix.<p>Thankfully, C is straightforward to learn. The major concept you need to understand is pointers; once you've done that, there are some syntactic oddities to absorb (typedefs, function pointer syntax, arrays vs. pointers), and you should skim through the standard library (which is tiny compared to Java, or even C++), and you're most of the way there.<p>In addition to K&#38;R, I've hears that Steele and Harbison's "C: A Reference Manual" is very good, although I haven't read it personally.
评论 #580521 未加载
cschepabout 16 years ago
A great resource I have enjoyed while brushing up my C skills has been CS107 at Stanford University on the web.<p><a href="http://www.stanford.edu/class/cs107/" rel="nofollow">http://www.stanford.edu/class/cs107/</a><p>You can find the lectures on academicearth.org, or on iTunesU if you prefer to download them that way.<p>Jerry Cain is a great lecturer. I wish they'd post the assignments with the files they provide the students though. Maybe someday.
评论 #580525 未加载
评论 #580493 未加载
kmavmabout 16 years ago
First, congratulations. You're making the right decision. Programmers who are ignorant of C leave certain roles and problem domains closed off to them. Even if your primary language is something else, not being afraid to roll up your sleeves and fix performance and correctness problems in the compilers and runtimes themselves makes you more valuable, more confident, and better able to reason about how the runtime will map your input to the machine. And proficiency with system-building languages and tools is a prerequisite if you ever aspire to be a "head surgeon," "10x programmer", "ninja", or whatever the heck the kids are calling it these days :).<p>K&#38;R et al. are fine, but I'm a particular fan of two slightly oddball, but great, books about C:<p>1. van der Linden's Deep C Secrets. Short of implementing a C compiler yourself, reading this book is the surest path to completely understanding some aspects of the C linkage model, and the distinction between arrays and pointers. It's also entertainingly written, which always helps.<p>2. The Lions Book, an annotated source listing for the Unix v6 kernel. The subject matter is one of the most beautiful and influential programs ever written. This ~10k-line program is completely understandable in six weeks' study, a couple months' tops, yet it staked out almost all of the abstractions that still underly modern operating systems. It is also C's native environment: C was literally invented to write this program. And while it's dated in some of its peculiarities ("=+" where we'd write "+=", identifiers only being significant in the first 8 characters, no typedef yet, a lot more reliance on the register keyword, etc.), it's damned fine code, by any standard. Each page brims over with good, informed engineering trade-offs; the sweet spot may have shifted since the days of 128k core and 16-bit CPUs, but the thinking that goes into those trade-offs is mostly unchanged.
twopoint718about 16 years ago
This falls more into the "tips and tricks" category, but I wish someone had mentioned this to me earlier. Once you have a good book on C[1] install all the man pages for the standard library. Once you start writing code, being able to say something like "man strlen" is amazingly useful.<p>I made the most progress in C when I took a class on OSs. Mainly, we wrote systems programming sorts of things like a simple shell, our own version of malloc, a simple user-space thread library, and so on. This really gave me insight into how these things would be done for real. Also, writing something really linked-list-heavy gets you a feel for pointers <i>really</i> quickly.<p>[1]: I have K&#38;R ANSI C and I found it very concise and well-written. It is probably one step up from the "raw newbie" class of books in terms of difficulty, but should feel just right for someone already acquainted with (an) other language(s).
antirezabout 16 years ago
You should learn C. It is a small language but to master it requires a lot of work for a reason: you need to get good at design. When all you have in your hands are structures and pointers you absolutely need to develop the ability to design well organized programs in different layers of abstractions, otherwise to scale over the 1000 lines of code program is almost impossible. This is probably the most valuable thing C will teach to you.
tptacekabout 16 years ago
1. In Which I Join The Choir<p>You should definitely learn C:<p>* You'll learn the memory hierarchy, which is probably the most important thing to know about performant software.<p>* 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).<p>* You'll be able to bridge Python (or Perl) to almost any library or framework you ever need.<p>2. In Which I Express Reservations<p><i>Don't learn assembly.</i> 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.<p>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.<p>3. There Are Books<p>Everyone is going to have an opinion about K&#38;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.<p>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:<p>* Garbage collection<p>* Lists and hash tables<p>* Resizeable strings<p>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.<p>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).<p>The other book you want is "Advanced Programming In The Unix Environment" (or its moral equivalent, "Win32 Programming").<p>CII is "how". APUE is "what".<p>Finally, I like (for new programmers) "The Practice Of Programming".<p>4. What I Did To Get Started<p>I "knew" C for a while (maybe since I was 13) before I actually started coding in it. Two things got me unstuck.<p>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.<p>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.
评论 #581030 未加载
woobyabout 16 years ago
1. K&#38;R - an absolute essential. I found myself in your shoes about two years ago, picked this up, and worked through it.<p>2. "The Unix Programming Environment" (<a href="http://www.amazon.com/Unix-Programming-Environment-Prentice-Hall-Software/dp/013937681X" rel="nofollow">http://www.amazon.com/Unix-Programming-Environment-Prentice-...</a>) This is a recent find, but covers more application. It's almost a Part 2 of K&#38;R and covers building problems and the Unix signal and file APIs. The examples are in an ancient syntax, but part of the fun is coding them and getting them to compile.<p>3. "The C Puzzle Book" (<a href="http://www.amazon.com/Puzzle-Book-Alan-R-Feuer/dp/0201604612" rel="nofollow">http://www.amazon.com/Puzzle-Book-Alan-R-Feuer/dp/0201604612</a>) I found this one day at Fry's, and it's fucking awesome. You can effectively learn C using it without a compiler.<p>Good luck!
makecheckabout 16 years ago
K&#38;R <a href="http://en.wikipedia.org/wiki/The_C_Programming_Language_(book)" rel="nofollow">http://en.wikipedia.org/wiki/The_C_Programming_Language_(boo...</a>
astineabout 16 years ago
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.<p>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.<p>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.<p>One book I can suggest is <i>Advanced Programming in the UNIX Environment</i> 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.
avinashvabout 16 years ago
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:<p>* Everyone is going to recommend K&#38;R, which is fine. Find a copy of C Interfaces and Implementations as well--very good implementations with explanations of standard high-level structures.<p>* 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).<p>* 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.<p>* Assembly. This is where <i>I</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.
评论 #580791 未加载
评论 #582268 未加载
radu_floricicaabout 16 years ago
Chances are you already know C, but don't realize it. Java and PHP have mostly C syntax. All you have to do is substract objects and add pointers.<p>Like rsheridan6 said, assembly helps. It's not necessary though, as long as you realize a pointer is actually a memory address and not something abstract like a reference in Java or PHP.
评论 #580422 未加载
voberoiabout 16 years ago
If you've got the time, I <i>highly</i> recommend taking CS107 at Stanford (for free): <a href="http://see.stanford.edu/see/courseinfo.aspx?coll=2d712634-2bf1-4b55-9a3a-ca9d470755ee" rel="nofollow">http://see.stanford.edu/see/courseinfo.aspx?coll=2d712634-2b...</a>.<p>I became incredibly comfortable with C -- memory management, handling pointers, having a very clear picture of how types are represented in memory -- after taking that course. The lecturer (Jerry Cain) is fantastic as well.<p>The course changed this year so I don't have any thoughts on the current CS107 offering if you plan on taking it through their online course offerings for pay. The lectures, syllabus, and materials on Stanford Engineering Everywhere (OpenCourseWare at Stanford) are all from the course I took two years ago.
SwellJoeabout 16 years ago
C Primer Plus was completely useless to me. As I mentioned in another thread, I tried several books on C before K&#38;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.<p>My book recommendations would be K&#38;R, <i>Advanced Programming in the UNIX Environment</i> by Stevens, and <i>C: A Reference Manual</i> by Harbison and Steele. In that order. With liberal doses of web research and reading real world examples and experimentation.
milesabout 16 years ago
Learn C at Harvard (virtually) for free:<p><a href="http://tinyapps.org/weblog/misc/200904120715_learn_c_for_free_at_harvard.html" rel="nofollow">http://tinyapps.org/weblog/misc/200904120715_learn_c_for_fre...</a>
plinkplonkabout 16 years ago
Here is what worked for me.<p>Work through, in order, (1) K &#38; R (2) C interfaces and Implementations by David Hanson (3) Deep C Secrets by Peter Van der Linden (4) Advanced Programming in the Unix environment<p>Good Luck!
spc476about 16 years ago
I've only found two books on C worth the money. One is K&#38;R, the other one (which I haven't seen mentioned here) is "The Standard C Library" by P.J. Plauger. It not only presents the ISO standard for the library, but Plauger's commentary on the standard, how to implement the standard C library, along with his implementation. It explains some of the murkier aspects of the C library, along with how to go about writing portable code.
JeremyChaseabout 16 years ago
If you can program well in C, everything else seems like both a blessing and a compromise.<p>While K&#38;R is great, my copy is far less used than Pointers on C by Ken Reek. If you can find a cheap copy I highly recommend it. If you are working through K&#38;R make sure to do each of the sample problems. The text is just the support to let you figure out how to do them.
mavelikaraabout 16 years ago
I also wanted to learn C, not to learn specifics of the language per se, but to better understand the C/UNIX ecosystem. The book I chose to follow was Computer Systems: A Programmers Perspective. You can get a draft at: <a href="http://lsec.cc.ac.cn/~tcui/download/draft.pdf" rel="nofollow">http://lsec.cc.ac.cn/~tcui/download/draft.pdf</a> .
colonhyphenpabout 16 years ago
This course website from the University of Maryland has a very good introduction to C.<p><a href="http://www.cs.umd.edu/class/fall2005/cmsc212/" rel="nofollow">http://www.cs.umd.edu/class/fall2005/cmsc212/</a><p>It starts off with the fundamentals then gets into some more advanced principles. In particular, check out the "Jan Plane's Lecture Outlines" link.
rubentopoabout 16 years ago
Stanford's CS Ed library (particularly Nick Parlante's linked list and binary tree problem sets) is a fantastic resource for understanding the C language, it is the way i learned it.<p><a href="http://cslibrary.stanford.edu/" rel="nofollow">http://cslibrary.stanford.edu/</a>
FiveFiftyOneabout 16 years ago
Well, copies of K&#38;R and C Puzzle Book are on their way, with CII and Advanced Programming In The Unix Environment (2ed) wishlisted for later. Can't find a copy of Deep C Secrets on our local Amazon equivalent, but I'll keep searching.
pasbesoinabout 16 years ago
It may not be what you are looking for, but if you want a C based intro CS text, this classic is now freely available.<p>Foundations of Computer Science<p><a href="http://infolab.stanford.edu/~ullman/focs.html1" rel="nofollow">http://infolab.stanford.edu/~ullman/focs.html1</a>
mlLKabout 16 years ago
Some helpful reminders for learning any programming language, <a href="http://c2.com/cgi/wiki?LearningProgrammingLanguages" rel="nofollow">http://c2.com/cgi/wiki?LearningProgrammingLanguages</a>.
ohfourabout 16 years ago
I bought K&#38;R but in the end found <a href="http://www.cs.cf.ac.uk/Dave/C/" rel="nofollow">http://www.cs.cf.ac.uk/Dave/C/</a> to be much more useful and accessible - and free!
Zarathuabout 16 years ago
Sure! Knowing C can definitely help with your overall understanding of how things work. It's especially helpful when learning new languages.<p>I uploaded a copy of K&#38;R ANSI C for you. In my opinion, it's probably the best way to learn it.<p><a href="http://whitepaperclip.com/files/ansi_c.pdf" rel="nofollow">http://whitepaperclip.com/files/ansi_c.pdf</a><p>Have fun!
评论 #580631 未加载
评论 #580544 未加载
评论 #580372 未加载
anthony_barkerabout 16 years ago
amazon booklist I used to teach myself<p><a href="http://www.amazon.com/Master-C-Programming/lm/9PUKX2J9RNUO" rel="nofollow">http://www.amazon.com/Master-C-Programming/lm/9PUKX2J9RNUO</a>
FiveFiftyOneabout 16 years ago
Brilliant. Thank you all.
mromanabout 16 years ago
I will be trying this myself, from what I can see it would be very helpful for learning C:<p><a href="http://bellard.org/tcc/" rel="nofollow">http://bellard.org/tcc/</a>