tags:

views:

604

answers:

11

I'm a C# programmer and I'm sold on the benefits of learning C. I want to deepen my knowledge of the underlying OS and CPU, understand the pain of memory management that garbage collection encapsulates away and generally improve my high-level programs thanks to an appreciation of the low-level issues that the compiler is dealing with on my behalf.

My question is how long can I expect to spend learning the C language in order to gain these benefits?

Is a couple of weekends spent reading the K&R book from cover to cover sufficient, or do I need to schedule time to cut some code? Do I need to spend time delving into any libraries, or is an understanding of the first-order concepts in the language enough to improve my C# code?

To be clear, I don't intend to write any significant programs in C. My goal is more to learn from the language than to become an expert in the language.

+16  A: 

Reading a K&R book and not writing code is like reading a book on weapons and never actually shooting. Yes, you've read in a book, that it works this way, but you have never encountered the typical problems that arise while doing this. Without practice such "knowlegde" is worth very little.

Plan to spend 2-3 years slowly writing small programs for solving different tasks in C. This will count as real experince. C provides delayed gratification for your effort.

sharptooth
to which I'll add: The key here is to find a little project you want to do and do it all from scratch, learning the whole way through. Don't run through someone else's sample code. And don't make it so simple that you don't have to use many pointers ;-)
Dave Markle
@Dave: That's better than nothing, but this will only help you master a very small portion of C. To gain really worthy experience you need to ssolve a handful of different tasks.
sharptooth
anon
@Neil. 6 months is a long period too compared to the few days for reading the book. In my estimate I deliberately accounted for slow learning instead of intensively solving problems how it happens in a workplace.
sharptooth
Yes, but he doesn't want to become an expert, just to get a better understanding of memory use etc. 2-3 years is way, way too long for that!
anon
@Neil: To really get better understanding of memory management in C he will need to explore the ways into udefined behaviour arising from doing thing incorrectly (unintended dangling pointers and other such stuff). Since UB is different in different environments this will require significant time.
sharptooth
I've never taken any time to understand how to get UB - I have spent a little time learning how to avoid it.
anon
you know, they say a virgin wrote the Kama Sutra...there's a difference between knowing and doing, but knowledge is power.
Jeff
+5  A: 

I'm not sure how long it takes to learn a language - it probably comes down to the individual. But I'm pretty confident you can't learn one without writing and debugging code in it.

dave
+1  A: 

I read the K&R book cover to cover and would not say I have any great understanding of C. Some time doing the exercises in K&R would be hugely beneficial.

I'm sure C libraries would make you more productive writing programs, but if it is simply learning C you are interested in, then you can implement anything yourself that you need. www.projecteuler.net is a good source of problems (although slightly mathematical in general) for you to get started on, if you fancy trying some coding outside of the K^R exercises.

Patrick McDonald
+2  A: 

If you can read K&R and understand it all, that's pretty good, as K&R covers pretty much all of the language.

However, reading it and understanding it all are very different. You should probably take a few passes through K&R and do all the associated exercises to ensure you really know it.

Even after reading through all of that, you will spend more months learning pointers the hard way. Expect lots of seg faults. On the plus side though, you'll get really good at reading hex!

There are a few caveats that the language has that you'll find out as well. One that used to give me trouble is that all pointers are the same size (4 bytes on x86), regardless of what they point at. A char* is the same size as a void* and an int*.

samoz
Thomas Owens
Gian Paolo Ghilardi
@Giani Amyone can say stuff like that, but one wonders how much credence to give to it when they use terms like "exit codes" - C has no such concept.
anon
Gian Paolo Ghilardi
I thought you were talking about strtol() et al?
anon
Yes, I'm so used to those macros (and other languages) that I used the wrong name (sorry, it happens)
Gian Paolo Ghilardi
s/exit codes/return values/g
Gian Paolo Ghilardi
I really don't know what you are talking about strtod() (for example) returns a double which is the the result of conversion. It will certainly never return EXIT_SUCCESS or EXIT_FAILURE.
anon
I used improperly the term "exit codes" instead of "return values" because of a little confusion in terminology. StrtoX() and EXIT_* macros are of course unrelated. Ok?
Gian Paolo Ghilardi
+21  A: 

C will take a week to learn, and a lifetime to master.

Larry Watanabe
That's true, but only because they keep changing it!
anon
@Neil Butterworth, How is C being changed?
Liran Orevi
anon
Almost certainly true. But there must be a continuum of knowledge that can be learned on the journey to mastery. In the early stages it will be general concepts that I am hopeful will be useful to me elsewhere, in the later stages I expect the knowledge will be in the realms of esoteric arcana. There's no doubt that I will embark on this journey - my question pertains to where I get off!
wildcard
You can get off with C .. like the Hotel California, you can check out anytime you like but you can never leave.
Larry Watanabe
Isn't that the Roach Motel?
anon
Almost certainly not true.
James
+1  A: 

In a couple of weekends, you will obtain mainly two results:

  1. hello world
  2. a lot of segmentation faults

C is not easy, in particular if you are not used to its hardcore concept. You will have to invest weeks, even months in tinkering with it, to grasp the most obscure (but still not too much) essence.

Stefano Borini
C is easy. It's probably the easiest language I've ever learned, apart from old Dartmouth-style BASIC. Languages like Python, Java etc. are much more complex.
anon
Yes and no. The syntax is indeed easy. it's procedural and you have a limited amount of datatypes. However, when you delve into pointers, and pointers of pointers and typedefs, and start passing arrays around, then it gets messy. That is what I am referring to
Stefano Borini
Pointers are really, really easy. I have trained hundreds of programmers in C and pointers. A handful (perhaps 2?) had a problem with pointers, but they also had problems with the concept of loops, functions, etc.
anon
I too am baffled as to why people think pointers are a difficult concept.
Don't know, but every time I had to debug someone else's code, I always found issues with pointer handling, and pass-by-value/pass-by-reference issues (scanf being the most famous case). I must however state that I mainly worked with future chemists or hobbyists, not future computer scientists.
Stefano Borini
There is a simple rule about scanf - never use it.
anon
Thanks Stefano. I think that what we’re talking about here is the difference between learning to read a language and learning to write a language.
@Neil: yep! :) well, I could produce a dissertation on bad coding practices that are answered with "so what?" when you point them out. ;)
Stefano Borini
+4  A: 

Ten Years

Joachim Sauer
+2  A: 

You definitely need to write some code - I don't believe you can learn any language without doing that. K&R has lots of exercises you can practice on. It's difficult to know how long in terms of elapsed time it will take to get a good working knowledge - I used to teach pretty much the whole language in 4.5 days, but that is quite intensive. I'd suggest about a month, if you are doing an hour or so a day.

Edit: I must admit, I find it a bit depressing that so many people think C is so difficult. K&R is 272 pages long, in my copy, and covers basically everything you need to know, including the standard library. Is there book in ANY other programming language that covers the whole shebang so concisely? I don't think so, and the reason is not that K&R is compressed in some way (Brian Kernighan is THE greatest techical writer, IMHO) but that the language is simple and easy to describe.

anon
A: 

40 days and 40 nights.

If you can't do the days and nights sequentially, then it will be 42 weekends.

But seriously, without putting any context on how fast you learn other topics, nobody can give you a real answer that is relevant to you. We can say how long it took us to learn it to a satisfying level, but that has zero correlation to how long it should take you to learn it.

If you said it took you 6 months to be good at C#, then maybe we can say it should take you 6 months * X (where X is still a guess, but a better guess than now).

We can all agree, however, that just reading the book is not enough. Of course you will have to write code. That is how we best learn anything - read it, write it, teach it. If you really want to learn something, teach it.

Erich Mirabal
I considered including some additional personal background in the question for exactly this reason. I decided against it because the answers would then be specific to me and less generally applicable. By omitting the context, I think most respondents are assuming a reasonably competent developer and answering accordingly.
wildcard
Yeah, but then we assume about the average case and give you no real answer. Basically, it boils down to us answering "what should I do to learn it" instead of "how long will it take *me* to learn it." I mean, it is such a broad answer that it is a non-answer.
Erich Mirabal
A: 

It will take a lot lot longer if you just sit around asking abstract questions and not actually diving in and doing it. Do you have a deadline or something? How long will it take me to learn the piano? Who cares, I just wanna make some noise. That's how kids learn so fast. They don't care about becoming an expert, or even good. They just like to play.

In any case, if you want to learn some interesting things, try some assembler as well. A lot of people really hate it, but that's just because they don't like spending countless hours not accomplishing much. I like it just fine.

Breton
"they don't like spending countless hours not accomplishing much. I like it just fine." Great quote.
samoz
A: 

To understand the pain of memory management just being writing sample programs with stacks, linked lists, binary trees, etc. You'll see what your getting into.

In school i was taught C as the introductory language and as pointers got introduced a whole slew of individuals dropped the class because frankly its a hard concept to grasp.

As many of the other answers have stated... Plan to not only read but practice. Theres no doubt that you haven't learned alot from C# by just making mistakes while coding and having 'aha!' moments.

IMO: 3 to 4 years to really understand the majority of concepts. A book will help you realize what the capabilities of the language are.