tags:

views:

382

answers:

8
+4  Q: 

How do I learn C?

I'm interested in learning C better. I have read K & R, and I have even done some simple C extension work in R and Python. What's a worthwhile project idea for doing something more substantial with C? Any good online resources, similar to Dive Into Python? In particular, resources focused on programmers of newer languages who are trying to learn C would be helpful (that mention things like "Asking an array for its length is nonsense in C, you lazy Pythonista").

My background:

Math/stats, day to day programming in Python, R, mostly around natural-language-processing, algorithms, and the like.

+4  A: 

Have a look at http://stackoverflow.com/questions/133607/what-is-the-best-way-to-learn-c-what-next-after-kr

Bob King
Okay, now I'm totally embarrassed. SO's search engine totally failed me! Thanks for the smack with the clue stick.
Gregg Lind
A: 

Find or define a problem in your day-to-day work and force yourself to solve it using C instead of Python. That will force you to learn the language while keeping the problem relevent to what you normally do.

ctacke
I think this is good general advice, but for me, I think would be pretty counter-productive, since I use high-level languages for a reason in my day to day work :)
Gregg Lind
Yeah, this is the big problem with C nowadays :) It is still useful to understand and crucial to use for certain things... but most likely you will be more productive if you use something lese more of the time.
Dan
A: 

Implement a virtual machine (the JVM, for example).

JesperE
+8  A: 

Several years back, a friend of mine asked me that same question: "How do I learn C?" I told him to write a device driver.

Imagine my surprise when he actually did it.

Robert S.
+2  A: 

You could write an interpreter for a simple language. Use flex/bison. Make it multithreaded etc. This is fun and tends to exercise pointers a lot. I wrote something like that for a school project: A simple stack based language with two different garbage collectors, TwoSpace and a concurrent version. That was fun. And doable as a first ever c program bigger than "hello, world"!

Daren Thomas
+4  A: 

Somewhat off topic, but since you mention your background is in Math and Stats your should try your hand at Project Euler. There are over 200 math/stat related problems available to solve. In addition, once you arrive at a solution, you can view the problem forum to see how others solved the same solution. Very handy for seeing how others solve the problem... and fun to boot!

www.projecteuler.net

JTA
I finally started doing these, and they are fun! It would be nice if they had a solutions archive by language (for when you solve the problem), in addition to the forum.
Gregg Lind
I got hooked on it a while back. Although I am not even close to solving them all, it does present a great challange when I have some free time.
JTA
+4  A: 

Hi Gregg, I have a similar background to you. I use Python to do a lot of math and data analysis for my PhD research, and also for web programming. The difference is that I learned C first, way back in the 90s.

If you can write C extensions for Python, then I'd say you have a pretty good handle on what C is good for. In my opinion, C today is best-suited for two things:

  • Writing low-level software that interacts with hardware.
  • Writing code that does repetitive, tedious, CPU-intensive stuff (math, XML parsing, etc.)... perhaps as an extension for a higher-level language.

Of course a lot of higher-level applications are also written in C, especially under Linux I've found, but in large part these aren't really written in the "bare-bones" C of K&R or the standard library. Rather, they use frameworks like Glib, or wxWindows, or the Apache Portable Runtime, or others, which all put use some kind of object-oriented structure or conventions, and often abstract away some of the basic memory-management details of C.

So I think that making your C skills useful in today's programming language environment is largely about doing low-level work, or becoming familiar with one of these higher-level frameworks. I personally like the Glib and GTK libraries a lot, since they use a very dynamic object-oriented model (a lot like Python) without preventing you from using the low-level features of C.

Dan
Thanks for the advice Dan. I find my lack of C-fu hurts me worst when I have some program written in C that I'm trying to adapt into an extension. Maybe it's just scientific / academic programmers, but I find that those programs have the most confusing C around.
Gregg Lind
Yeah, a lot of programs written to solve a specific academic task are terribly written... they're very narrowly and hurriedly cobbled together, even if the algorithms and goals are brilliant. I know I'm as guilty as the next guy in that regard :-/
Dan
A: 

C interpreter.