10

I want to dig into C and normally when I want to learn a new language I think of a simple/attainable project goal that has some practical purpose (make use of an api, automate a task, etc...)

I'm having a harder time coming up with one for C mainly because everything I read points to the fact that C's primary use currently is in embedded systems and more complex software projects like operating systems.

Does this point me to the fact that I don't need to learn it or that I just need to be a bit more convoluted in my learning strategy?

References to practical learning resources? (tutorial/how to that produces an end result that has some purpose/benifit)

4
  • 3
    You'll get more sensible answers if you can tell us WHY do you want to learn C?
    – zvrba
    Commented Aug 29, 2011 at 7:20
  • 2
    I agree with zvrba. C is great for low-level, fast stuff and for driver code. If you're writing code for games, it's also great (although, you'd probably be working with C++ coders who'll complain at you for writing "non-standard" code). I do feel, though, that Assembly and C are similar to Latin (in spoken languages), they weren't the first, but most that came after them used the ideas and constructs from them to create their own languages. Thus, studying C MIGHT give you a better understanding of higher level languages Commented May 30, 2012 at 8:27
  • Have you checked out Zed Shaw's Learn C The Hard Way. About as practical as one can get, and quite free too. Commented May 30, 2012 at 16:34
  • the Python the Hard Way person now has books for other languages including an alpha version for C, I would start there. C is high level a big difference to asm, I hate the comparison to asm as it is false. Relatively sure it lets you do what other languages wont. It doesnt matter what you want to make any application you can write in C, no reason to target the application to the language.
    – old_timer
    Commented May 30, 2012 at 19:42

6 Answers 6

27

I would just recommend picking up a copy of Kernighan and Ritchie's The C Programming Language (2nd Edition). It has a number of exercises at the end of each chapter that you can do, along with providing a very good introduction to the C language. It won't make you a master of the language, but you should have a good enough grasp at the end of the book and exercises in order to perhaps find an open source project written in C that you could read the source to and perhaps contribute to.

If you want to see actual real-world projects with C code in them. It is getting harder - many projects use a "higher level" language, but C is still alive and kicking. If you want to learn C, don't be afraid to reinvent the wheel to achieve learning. It's not a good idea to reinvent solutions on the clock, but it's perfectly fine to do so as an academic exercise.

12
  • 1
    Add the FAQ from comp.lang.c to that. c-faq.com Commented Aug 28, 2011 at 23:08
  • 3
    +1 for don't mind reinventing solutions in order to learn.
    – user
    Commented Aug 29, 2011 at 9:15
  • 4
    K&R is horrible for learning good programming style in general, and horrible at teaching safe/sane C programming practices in particular. All code snippets inside it are obfuscated at best, most of the time they teach outright dangerous habits. It is an outdated book, I wouldn't recommend it for anything beyond nostalgia. It is especially unsuitable for beginner programmers.
    – user29079
    Commented Aug 31, 2011 at 14:24
  • 2
    @Thomas Yes the 2nd edition. You can cite pretty much any code snippet from it and I can likely point out 2-3 dangerous/poor practice cases in it. I'll open the book right now and pick some random example. Ok here we go, p119: assignment inside conditions, use of native primitive data types, use of implicit int (unsigned), typecasting the result of malloc (very bad!), typecasting the parameter to free to void* (why???), multiple returns (aka spaghetti), using NULL without proper #include, if without {}.
    – user29079
    Commented Aug 31, 2011 at 14:46
  • 2
    +1for KnR C. It's the best (and most concise) introduction to programming in a specific language I've ever read. As other's have pointed out, it can be a little dubious in places - the one piece of advice I'd give is this: if there ever was a programming manual that required you to read the surrounding blurb, this is it. Other's you can, usually, figure out from the code block, but this book requires that you read it cover-to-cover, at least, once. Commented May 30, 2012 at 8:23
1

You're going to have to really understand memory allocation. You sound like you've used other languages before, but the chances are that those other languages hid the gory details of memory management from you. In C, the memory management is always explicit. There are some advantages to this, but it means that you have to be careful to get it right or all Hell will break loose.

Use whatever memory debugging facilities are available for the platform you use. For Linux there is Electric Fence, on Mac OS X there is Guard Malloc. Also there is Valgrind which supports a number of operating systems.

If you fail to free memory that should be freed, you'll have a memory leak. Keep doing that and you'll run out of memory. Even with backing store (what most people call virtual memory), you can cause the system to page excessively or even run out of swap space.

If you free memory that has already been freed, you'll corrupt the heap and crash, but probably not right away. You'll also corrupt the heap if you overrun a buffer. The memory debugging tools will help with all of these.

Unit testing also helps.

0

I agree with most people here that today languages are determined by the task at hand . There was a time when you had limited choice in this regard . Today C is mainly used in Embedded/System programming domain where there is a need for code optimization . You need to write precise codes to get the maximum because if ever you start to use that abstraction , then the underlying stuff has to very robust . I would say you try to get around some open source GNU projects like Linux itself or something that needs C developers . Try your hand in those projects instead of writing something out of scratch in C . This is probably the better way to write codes and learn a language .

3
  • Actually, I doubt jumping head first into an established, even if minor, project is a good way to get started learning a language from scratch. You're likely to get in way over your head, and will also have the additional burden of simply trying to navigate the code, even if all you set out to do is understanding how the code works. An operating system kernel in particular is an incredibly complex piece of software.
    – user
    Commented Aug 29, 2011 at 9:14
  • Yes , but I am assuming the questioner has actually gone through K&R book or something like that . I was recommended this approach when I asked in Python channel . Would it be differnt in C ?
    – Nishant
    Commented Aug 30, 2011 at 17:42
  • Strictly speaking I don't know if it would be that much different, but in C, you do need to pay a lot more attention to the nitty-gritty details than in a high-level language such as Python. Memory management, to mention just one thing. Jumping into an established, potentially large project when you're just getting started with a language might not be the best way to do it.
    – user
    Commented Aug 31, 2011 at 7:46
0

You're going to want to do something that can work from the command line. GUI programming in C is a major pain in the ass, even with a good GUI framework.

My usual suggestion for a practical C learning experience has been a contact list or other simple data management app. That will cover most of the important bits (I/O, memory management, text processing, etc.), and you won't have to use anything outside of the standard C library to accomplish it. C doesn't have any built-in support for graphics, networking, sound, or much else1, so for your first foray into C programming it's best to avoid anything that relies on those sorts of things.

C can be used for general-purpose programming; you just have to ratchet down your expectations somewhat (imagine it's 1983 and you're sitting in front of a VT52 attached to a VAX 11/750, and you'll be in the right mindset).

Just remember, C has no blade guards, and won't prevent you from doing something stupid. The language definition is a little loose in places, and there are some operations that aren't illegal as such, but aren't well-defined either.


1 - The latest (2011) revision of the language standard has added built-in threading support

0

I recommend Project Euler. I used it to improve my C++ and it is awesome. It's great because each problem is a small task that might take less than an hour to complete (at least the first ones).

You'll need a profiler and a book about optimizing code for speed. Be sure to stick to the 1 minute rule: You have to get the answer within 1 minute.

0

If you are ALREADY a programmer, and JUST need to learn C, and don't mind learning an older version of the language (the core is still pretty much the same), then Leendert Ammeraal's "C for Programmers" can't be beat. Work through it, beginning to end.

Not the answer you're looking for? Browse other questions tagged or ask your own question.