Ask HN: How should I go about learning C?
I've mostly ever heard scary stories about C (or the "you need to get down to C for performance"). Since KRC was written in 1988 (2nd ed.) - how should I go about learning C? Is there a resource that captures all the gotchas, pitfalls that people have tripped over - over the past 2 decades? Should I even start with KRC or are there better resources to start with? I'm coming from a Ruby/Python background.
3 comments
You are asking a number of inter-related questions. I will try to scatter a few pointers (pun intended) for you to follow up on.
As far as learning the essential syntax of the C language, K&R 2ed is excellent. It was rewritten to cover the ANSI spec variations. The first edition uses the "traditional" conventions and these are generally flagged with warnings by most C compilers.
You mention performance. That claim is a red herring. In absolute terms, C allows you to write the fastest code if you know what you are doing. But it's like saying that a Ferrari is the better car to drive to the mall because it gets you there faster - no mention of speeding tickets, purchase cost, insurance, etc. Many other languages will let you write almost as performant code which takes less time to debug and is easier to maintain.
C is used for many different application areas, you don't mention what area you are working in. For example, C programming for Linux kernel modules is very different to writing Arduino code or networking code, etc. Each of these areas have a large body of knowledge based upon experiences, etc. I find Google / StackOverflow excellent resources.
If you really want to learn about the quirks of C language and C compilers, then I can recommend http://blog.regehr.org.
Finally, I recommend using gcc or clang on Linux or Mac with a vanilla text editor and command line tools. Using IDEs, including VS, introduces too much magic which can be extremely difficult to grok without having a solid grasp of the basics.
As far as learning the essential syntax of the C language, K&R 2ed is excellent. It was rewritten to cover the ANSI spec variations. The first edition uses the "traditional" conventions and these are generally flagged with warnings by most C compilers.
You mention performance. That claim is a red herring. In absolute terms, C allows you to write the fastest code if you know what you are doing. But it's like saying that a Ferrari is the better car to drive to the mall because it gets you there faster - no mention of speeding tickets, purchase cost, insurance, etc. Many other languages will let you write almost as performant code which takes less time to debug and is easier to maintain.
C is used for many different application areas, you don't mention what area you are working in. For example, C programming for Linux kernel modules is very different to writing Arduino code or networking code, etc. Each of these areas have a large body of knowledge based upon experiences, etc. I find Google / StackOverflow excellent resources.
If you really want to learn about the quirks of C language and C compilers, then I can recommend http://blog.regehr.org.
Finally, I recommend using gcc or clang on Linux or Mac with a vanilla text editor and command line tools. Using IDEs, including VS, introduces too much magic which can be extremely difficult to grok without having a solid grasp of the basics.
If I ever get around to going back to learning C my two starting resources would be:
Zed Shaw's Learn C the Hard Way because one weekend a couple of years ago and back when it was free online, I started working through it and it's very good for a beginner...which is Shaw's goal. The reason it's good for a beginner is that it does not get bogged down in trying to reach a broad audience (K&R was written for systems programmers using assembly language) and because it introduces key aspects of the C ecosystem/environment/workflow such as Valgrind and Make so that when you are done with the book, you're not going to be left wondering how to actually write a program.
The other resource is Head First: C which I picked up the time I decided to learn C before the last time I decided to learn C. I like the Head First books and have used some of them successfully...and apparently not used others unsuccessfully. Like Shaw's work it's also well structured from a pedagogical perspective (and this is rather unlike K&R).
Some other interesting C books I have on my shelf: The Standard C Library because it was about $3 used and I threw it into a Thriftbooks order to make the $10 minimum for free shipping on a whim. The prose and the code are both interesting. And the Amiga ROM Kernal manuals which date back to the first time I tried to learn C ... and at that time the real problem was that a C compiler and the hardware to run it was in the kilodollars and there was no internet and so that was kind of an impediment.
As for learning C for performance these days, probably not unless you're doing embedded systems and even then if it's not going to be manufactured in mass quantities you can probably throw hardware at it...a RPi Zero has is mid six figures of hardware from the time when K&R first appeared. But learning C for the purpose of learning to think about programming a different way is probably worth it.
Anyway, since I haven't learned C it might not be worth giving much weight to my advice.
Good luck.
https://learncodethehardway.org/c/
Zed Shaw's Learn C the Hard Way because one weekend a couple of years ago and back when it was free online, I started working through it and it's very good for a beginner...which is Shaw's goal. The reason it's good for a beginner is that it does not get bogged down in trying to reach a broad audience (K&R was written for systems programmers using assembly language) and because it introduces key aspects of the C ecosystem/environment/workflow such as Valgrind and Make so that when you are done with the book, you're not going to be left wondering how to actually write a program.
The other resource is Head First: C which I picked up the time I decided to learn C before the last time I decided to learn C. I like the Head First books and have used some of them successfully...and apparently not used others unsuccessfully. Like Shaw's work it's also well structured from a pedagogical perspective (and this is rather unlike K&R).
Some other interesting C books I have on my shelf: The Standard C Library because it was about $3 used and I threw it into a Thriftbooks order to make the $10 minimum for free shipping on a whim. The prose and the code are both interesting. And the Amiga ROM Kernal manuals which date back to the first time I tried to learn C ... and at that time the real problem was that a C compiler and the hardware to run it was in the kilodollars and there was no internet and so that was kind of an impediment.
As for learning C for performance these days, probably not unless you're doing embedded systems and even then if it's not going to be manufactured in mass quantities you can probably throw hardware at it...a RPi Zero has is mid six figures of hardware from the time when K&R first appeared. But learning C for the purpose of learning to think about programming a different way is probably worth it.
Anyway, since I haven't learned C it might not be worth giving much weight to my advice.
Good luck.
https://learncodethehardway.org/c/
Start with K&R like everyone else. It's the standard text and one of the finest programming books ever written. "Expert C Programming: Deep C Secrets" by Peter van der Linden is good. Most C programming books are just dumbed-down rehashes of K&R.