tags:

views:

1642

answers:

19

I am able to program in Lisp, Python, Ruby, JavaScript and Prolog and I have been willing to learn how to program in C for a very long time now. I know the syntax and basic concepts (I've actually read Kernigham & Ritchie), but I've never been able to pass through the writing “Hello world!” stage.

So, my problem is related not to the language itself, but rather to everything else that you need to do sensible development in C. I never quite figured out how to use libraries, what are the capabilities of the Standard Library, what is the good way of getting Unicode (I hope there is one), how do I get hashes and so on. Oh, not to mention that stuff like the linker, what should be the compile flags and so on are black magic to me. I have problems with the pragmatics of programming in C.

What would you recommend me to do in order to finally learn C?

  • Are there any really good tutorials or books?
  • Are there any libraries known to be easy to interact with that would allow me to learn what the best practices are?
  • Are there any Open Source projects to which I should be able to contribute quite fast and which present a good programming style?
  • Writing what pet projects would be optimal from the learning point of view? In other languages I usually write a Sudoku solver or a Red-Back trees implementation to get the feeling.

I'd like to be able to:

  • write programs interacting with libraries,
  • write a Python module in C,
  • create the CFFI bindings for Common Lisp for a C library
  • write extensions to SQLite or PostgreSQL

So as you can see I am not really interested in kernel hacking or hardcore algorithms at this point.

I am a Linux and Emacs user.

Important

I do have Kernigham and Ritchie. Thus the subquestion What next after K&R?

+2  A: 

Read K & R's The C Programming Language and do all the exercises. It really is the source of all C programming knowledge.

Get an open source compiler (I like gcc, and there's also gcw) and do exercises on the command line. I know all those big shiny IDEs are tempting, but if you just want to grok C, they're more distracting than anything else.

Bill the Lizard
dulaneyb
You didn't read the original question carefully, did you?
simon
The source of _all_ C programming knowledge? Gimme a break.
postfuturist
He changed the original question after 6 people had answered. Also, that book was co-written by the creator of the language, so yes, it literally is the source of _all_ C programming knowledge.
Bill the Lizard
+1  A: 

I would sit down with K&R (since it sounds like you have it) and do the exercises. Perhaps skipping over some of the trivial ones (like Hello, World).

Thomas Owens
+4  A: 

I'm really only answering a subset here, so forgive me for the brevity.

You indicated that you have read K&R, that is good.

To learn how the linker works, I'd recommend having a look at the man pages. In fact make sure you also have the manpages-dev (I think the package was, in Ubuntu that is) and it will shed a lot more light on certain functions.

I'd also recommend you do something lower-level than you would in say Lisp or Python. Choose something that requires you to use pointers and bitwise manipulation. It will help you understand (a little bit) how compilers optimise for certain platforms and It's certainly what I prefer doing, so I suggest you at least give it a try. You could try something like file encryption. XOR is very easy to implement and its something practical as well. If you want to progress from there, grab a free crypto API and experiment with that (or something else that interests you). Check out how it's put together, and if it's open source and it interests you, lend a hand!

mdec
+1  A: 

Take on a non-trivial programming project that requires you to do things with memory, pointers, function pointers, file i/o etc. Many years ago I taught myself C largely by writing a paint application - this was in the days before Windows, so the UI, menus etc were all hand crafted too.

Do something that is fun and interesting to you. Graphics of one kind or another has always been my interest, but I'm sure there are other areas that you really like where writing an app - even a mini-clone of something so you don't have to think of the functionality - would give you a buzz.

Greg Whitfield
+8  A: 

I strongly recommend you to pick something huge to build, like an FTP Client and start banging your head to your keyboard. Instead of reading a programming book, I think what you need is to start making mistakes.

If you need to learn a new concept, search engines are there to help you. If you have a C question, there will be Stackoverflow.com

Probably you will end up with a terrible-but-working FTP client and again probably you will trash the code you would have written by then. But this process will teach you a lot more than any book and/or course can teach to a human being.

+4  A: 

Back in the day, the first edition of the Waite Group's C Primer helped me way more than K&R. Sacrilege, I know, but true. Walk first, then run.

marc
A: 

It sounds like you already know the basics of the language, so there are 3 more things you need to know to get hacking:

  • To learn how the linker works and how to use it read the gcc man page (replace with whatever your compiler is)
  • Learn how to write Makefiles and how to use make
  • Read the Python documentation (or Perl or...) on linking to C libraries
  • dsm
    +2  A: 

    Re-read the K&R chapters on pointers and make sure you get it right. Then read them again nd make sure you really understand them. Then download the source code for some simple, small open source application (for example, you can check the code for classic tools like cp, echo, etc, or some commandline calculator app or whatever). Try to make little, incrementally harder changes to those sources. Make it crash, badly. In the process, you'll also learn how to use the standard linux compile toolchain (gcc, ld, automake, autoconf...).

    axel_c
    +1  A: 

    Learn through coding, using the K&R book as a reference.

    If you want someone to lecture to you Stanford has some videos on you tube. 2 series Programming Abstractions, Begining C++ (which is extended C), and Programming Paradigms, which is for the first half very involved C programming(pointer arithmetic, and memory management).

    J.J.
    +2  A: 

    Reading K&R doesn't really do much unless you have done the equivalent of taking a course on machine structures. For instance, learning C is completely useless if you don't know what a stack frame is.

    And the C you see in K&R is quite different from the C in, say, the kernel. Read some production C code to get a feel of "modern" idiomatic C.

    +2  A: 

    Many suggested you to start building something from scratch, I'm going in the opposite direction.

    Take an existing project and try to add something to it. It may be a bug fix, a feature you would like to see, support for another file format, the integration with another project, ...

    It is somehow harder because it will require you to go through someone else code, so try to pick a project with a clear coding style, made by good programmers but the very act of studying someoe else code will teach you a lot.

    Remo.D
    A: 

    I believe K&R is good book but you cant say novice in C to start the programming with it. However I think you should take some reference book like Deital & Deital OR C Complete Reference etc and start working on some existing or small project. As this will let you use your existing skills against some practical code and you can always look into the reference books whenever you stuck. Now when you able to work through one or two projects. Then i would recomend that you go through the K&R book again and i hope you will understand it loads better.

    Gripsoft
    +2  A: 

    This site is really thorough, and may cover some things that you already know, but working through the exercises is really helpful.

    ajbl
    +1  A: 

    K & R is makes an excellent reference but poor textbook, IMHO. A much better option is K. N. King's C Programming: A Modern Approach (2nd edition).

    Michael Carman
    +1  A: 

    After you've completed K&R exercises, read Abelson and Sussman's Structure and Interpretation of Computer Programs and complete their exercises, but do it in C instead of Scheme. This should get you busy for another 10-20 years.

    Ok, just joking :)

    Constantin
    A: 
    A: 

    You can only learn so much from reading programming books. While it is essential to have handy references you have to start being a programmer, not a librarian.

    Start doing!

    Mike Thompson
    +3  A: 

    I guess you should try to code something, before going back to new books. Perhaps something you already wrote up in another language (so you're familiar with the problem), or something that you need, that you would have donne better in another language, but should try on C instead.

    I started with C (I studied physics, not computers science).

    To warm myself up when I read the K&R, I needed something to understand how allocations and pointers worked. Despite reading and re-reading the chapter, something did not fit in my brain. So I wrote my own "string.h" library, and then, rewrote it to have each function do the reallocation (which the C string API does not do). This took me two weeks of work, but at the end of the two weeks, I had some solid base of C bravery (it took me some more time to fine tune it into real experience).

    The secondth thing I worked on was what I called a "site compiler". It took HTML files with custom tags, and did something like the C preprocessor #ifdef and #include. This used the library I had written before, and was of some use to me because I add to play with 3 versions of the same site, and needed them functional with only a browser (i.e. no dependancy on a server).

    The third was a zero+ expert system. But by then, I was learning C++ (i.e. more accurately, C plus some C++ facilities like classes), so it goes beyond the scope of your learning.

    I guess a parser would be interesting, too. Perhaps an XML parser... Or an interpreter for a limited-scope language, like polish-notation console calculator, or even some kind of script engine.

    All in all, I guess you have experience on computer languages, so C won't be really difficult. In fact, makefiles will be more a problem, I guess... You'll have to learn how to create an executable, and then a lib, and then link with a lib, and then the same with a DLL/Shared Library. Learn about the mysteries of C "headers" and C "sources". C "compilation units". The C pre-processor is a must, too. Understanding its use (and its abuse) will help you a lot. :-p

    But you'll find yourself very limited by C: Everything's done by hand, and the compiler will let you do stupid things, and won't always be there to catch you up. You'll discover your program leaks memory, or resources, and you'll have to learn to handle this kind of problems. Contrary to Bjarne Stroustrup's viewpoint, I believe C is a fitting introduction to C++... But should you try C++ after C, do it with humility, but this is another story.

    paercebal
    A: 

    I'd suggest writing an interpreter for a simple programming language.

    It doesn't have to be anything complex, try a calculator language with user defined functions and variables. Make it work properly with recursion and other language features and you should have a fun project that allows you to experiment with various concepts of C code.

    Doug