Ask HN: Should I learn C?
I'm a recent high school grad going into college as a comp sci major. I have 2 years experience in Python and JS.
Would it be worth my time to learn C (C++ possibly?)? (Or maybe you have another language that would be more profitable now.) I'm aware I will have to learn at least C for college, but is it something I should learn now?
I'm looking for your opinion because a lot of you have either gone through a comp sci degree or have done some programming in C.
Thanks HN!
97 comments
[ 3.0 ms ] story [ 164 ms ] threadNow is a great time to start. Go find K&R, fire up Linux and write and compile "hello, world". Then keep going until you experience your first segmentation fault. ;)
(Do not study C++ at this point. Study C. These things turn out to be very very different.)
Or your first cryptic compiler error. I'm pretty sure mine was "numeric constant contains digits beyond the radix".
I've found that even though I now spend most of my time programming in higher level languages, my understanding of C and assembly, compilers and debuggers, the virtual memory model and system memory hierarchy, and CPU architecture are still very valuable in helping me reason about my code and how it interacts with the rest of the system. I also still have developers at work on the C development team occasionally come ask me code questions, and often the answers to their questions come down to understanding one of the above better. If you can master multiple levels of abstraction you will be a much more effective developer.
http://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/...
K&R refers to: The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie
http://www.amazon.com/exec/obidos/ASIN/0131103628/thedinosau...
In other words: Don't waste your time trying to learn from anything other than K&R, at first. It's just the most concise text available...C is a concise language, and it is not well-served by big fat books.
I'm not against learning C in general. I think everyone should. But his question isn't should he learn C ever it's should he learn it in his spare time between High School and College
The best thing I did for myself during college was to program on my own time in languages that people use out in the world. Learning C, Assembly, Data Structures and all the rest is great but when you get out into the world it helps to have been programming in Python or Ruby for 4 years and be experienced in that.
So I say "No". Learn C in school but in your free time I suggest you learn Python (Here's a great starter book I used when I taught a class on it: http://www.amazon.com/Python-Programming-Absolute-Beginner-3...)
C is a good language to learn, and a lot of interesting, well-paying jobs use C, and a lot of interesting open-source projects use C -- but if you just want to make steady cash, you can do so much easier with Java or PHP.
That said, if you go about your programming self-education correctly, you won't have a problem using Java or PHP (well, you won't have a problem in the sense that it won't be difficult for you to design programs in those languages).
It's good to have a handle on how the architecture of your machine works. C gets you much closer to that, and you will learn about a lot of things that are hidden from you in HLLs like Python, but that are still relevant to non-trivial programs.
So yeah. Learn C if you're passionate about comp-sci, or software engineering. If you're learning programming only for a career, it may not be as beneficial for you. Learning C before college is definitely a good thing.
Also, if you learn C, the last chapter of SICP is much easier to do, and you should go through SICP at some point ;)
Don't touch C++ until you need to touch C++. You'll know when that time is, if ever.
I was meaning the in overall sense; "profitable" as in good for me. Sorry for the misunderstanding.
Edit: Also, if you use linux and end up going down the rabbit hole a bit, C is a prerequisite.
The stack-frame comment is spot-freaking-on. Knowing this level of detail will make you a way better programmer in every other language because you won't make stupid memory hogging decisions or mistakes. You also understand the complexity of doing things one way and the tradeoffs of another.
I best learnt C by re-writing large portions of the Pintos operating system. It's an academic OS and will be interesting to look at the source. Systems programming is where C packs the most punch, but all of those concepts can be re-applied in user land for some amazing effects.
I don't believe that Linus ever asked (someone else) if he should learn C. Or that Damien Katz (couchdb creator) ever asked if he should learn Erlang. Or if Brad Fitzpatrick (Livejournal & Danga) ever asked if he should learn Perl!
Just learn what you (from looking around) believe will make you a better or smarter, what is even more important use what you learn. Don't just learn to learn!
Spolsky and Atwood argued this out in Stackoverflow Podcast #2. http://blog.stackoverflow.com/2008/04/podcast-2/
Just get a comfort level with it (C), and move on. It will pay dividends.
From a strictly pedagogic perspective, it would also be good to spend some time studying vanilla C and the POSIX APIs. When we say the phrase "learning C" in a thread like this one, we tend to actually mean "learning to work at a lower level of abstraction, closer to the OS, to assembly language, or even to the hardware itself". And I believe that most of iPhone development consists of telling Cocoa to do things, and Cocoa is presumably not a very low level API -- thank god.
However, any C is better than no C, and iPhone development is fun and useful, so if it inspires you, go for it.
+ From the point of view of Python or Java (say), it's close enough to assembly language to let you understand how things might work;
+ If you need speed or control over hardware, sometimes it's your best route to extend whatever you're working in;
+ In many jobs you'll be the only one who has the knowledge to do unusual things, hence you'll be more valuable;
+ It gives you another point of view when designing systems;
+ You say you'll need it for college - learn it now;
+ A spread of languages is critical if you want to master programming.
You can carve out a perfectly good programming career without knowing C, but it's always of benefit and value to get a wide range of skills. Some recommend:
+ Pure OOP: perhaps smalltalk or Self
+ Pure Functional: perhaps Haskell (non-strict) or ML (strict)
+ Pure Imperative: perhaps C
+ Pure Logic: perhaps Prolog
Other hybrid languages are useful and perhaps more productive, but it's the ideas that really matter while you're still learning.
Nothing wrong with the pure languages as options, just I think impure languages can fit the categories too.
I think that there's probably more (practical) benefit to be had with Ruby than Smalltalk or Self.
http://mvanier.livejournal.com/998.html
http://matt.might.net/articles/best-programming-languages/
http://www.info.ucl.ac.be/~pvr/VanRoyChapter.pdf
http://www.info.ucl.ac.be/~pvr/paradigmsDIAGRAMeng101.pdf
http://www.slideshare.net/brweber2/functional-concepts-for-o...
-----------------
Oh, SICP and books like PL pragmatics
http://www.amazon.com/Programming-Language-Pragmatics-Michae...
----------------
and i love "Traps/pitfalls" from 1989!)
http://www.amazon.com/C-Traps-Pitfalls-Andrew-Koenig/dp/0201...
Other than that, I can't speak for anyone but myself.
For me, C has taught me so much since I was 14 that I couldn't possibly be where I am if I hadn't spent years writing C and I couldn't possibly recommend anyone not to learn it. But it's a different world for everyone and I can't say it's a bad thing if you don't know C. It would be a bad thing in my line of business but not everyone does the same things I do.
One thing about C is that it's a nice forced marriage with the von Neumann machines we're using these days. If you really want to know C, you really want to know the current hardware architecture as well. It will change but it won't do that at least in the next couple of decades.
On the other hand, mastering high-level languages first and dropping down when necessary might be a wise choice, if you only know you can do it. Like I said, architectures and paradigms will change in the future, and higher-level languages can embody abstractions that are very hard if impossible to contain in a von Neumann machine and it is the revolution of some new architecture when C will eventually be obsoleted.
You'd want to learn C to get a feeling for how the computer works at the core, but more important than that you should be seeking a rock-solid foundation in proper coding methods and structures that has potential off-branches into your courses and your career.
From Java, you could pursue Android development or even Objective-C for iOS development. From C, that transition would be frustrating to say the least.
(Other points behind pursuing Java is you'd find it quicker to start testing on your local box without needing to seek help, it'd give you great insight into what an IDE is and why it is your best friend, and there are more community projects surrounding Java than C that you could start to participate in when you're ready.)
In the end, any language you want to learn would be a good language to learn (even Fortran), but you should go after a language that you can go from 0-to-novice on your own in a short time where you'll end having built a nice repertoire, and I think Java would best suit that need for you.
Make sure you're using a C compiler, not a C++ compiler
Here is why not C++: http://news.ycombinator.com/item?id=1463592 and why not java
Edit: Was there something wrong with my comment? Or were you downvoting merely because you disagree?
In general, very little of the program needs to be written in C to get great performance gains.
Additionally, python is vastly different from LUA. Lua is still a scripting language, ruby and python have moved past that.
For someone at age 18 or so, I'd say C is a much much better level of complexity to be tackling that C++
There's no downside in learning C, and there's not much fluff in the language core to complicate things, or slow things down.
I'd suggest you get a copy of K&R: The C Programming Language and just work through the text and examples. You might also want to pick an existing C-based project and work on bugs in it, maintenance programming being one of the best ways to get to grips with using the key tools, gcc and gdb.
This way you'll get a feel for the language, and also for what you'll need to debug and fix code.
Any language you learn will make learning additional languages easier and faster. But, the boost you get from learning C is arguably greater than for other languages. It's also the single most powerful backup language you can know.
I don't write C for a living at all, but it is great to know how everything is working under the hood, and APUE will teach you that.
To be honest, I wouldn't bother with Java. I think in ten years Java will look like Ada does now: we will all be asking ourselves "What were we thinking?"
I also wouldn't bother working on anything but Unix, but that is just bias...
http://www.amazon.com/gp/offer-listing/0201563177/ref=dp_olp...
1st ed. APUE won't cover e.g. epoll/kqueue at all. Get the 2nd. Fantastic book, though.
This, for the same reason you should learn some provisional level of assembler on an older 8 or 16 bit processor with a simple instruction set (intel 8080 or 8086, and then maybe something RISC just for kicks). You don't need to be an expert, just dabble enough that you "get" how it works.
Also learn how to implemenet a few other progrmaming patterns in C - you can do OO in C, you can do functional in C, you can do concurrent in C. You likely won't have to in real life, but understanding those concepts at the level of C will really help you understand the benefits of higher level languages.
I'd say the same for any of the Lisps - learn one, even if you'll never use it - the insights it gives you are worth it and will make your other code better.
On second thought, learn assembly language for a platform you can run it on. If you only have x86, learn 8086.
One thing you might want to look at is cython[1]. It allows you to write C extensions in a Python-like language. Could be useful as a "bridge".
[1] http://cython.org/
If you ever have to do low-level programming or Mac/iOS development it will definitely be of great use.
Think of it this way: Why not learning it?
1) I had already started learning C. It is low-level, and I was getting disinterested. I wanted to know if it'd really be worth the work to learn it. (The answer is absolutely.)
2) So that other people, when wondering if they should learn C, would have a great list of hacker recommendations. (That has happened.)
I assume that you want to be a good hacker. A good hacker has a healthy mix of knowledge that will allow him to start work quickly on almost anything. Without a solid grounding in C, (and a perspective of JS and Python), there are a whole class of things that you won't be comfortable working with unless you have some lower level experience.
With your current knowledge, how easy would it be for you to start hacking on an IP stack? Not that you would ever need to, but with a good knowledge of C such a thing becomes natural to you. All of those RFCs begin to make much more sense.
Another big benefit to C is how simple it is. You begin to see how possible it would be to create a simple C compiler, and this is a good spot for this intuition. It has a simple syntax, is somewhat feature-poor, and is very close to the hardware.
Plus, you'd be able to hack on CPython if you want ;)
Practical aspects:
Your knowledge seems to say that you are headed for web development, which I applaud, because all things exciting are happening on the web.
Web development is changing. It used to be that web developers were laughed at, considered to be inferior to other programmers. It has been a few years since I've seen that opinion, and I think it has been thoroughly shown to be wrong.
Web development has become, essentially, a two part system where the pretty web front end sits as an interface to a super hardcore back end. Web companies now face scaling issues that are relatively new to web development. While you may never program in C, having a strong foundation on what the underlying issues are.
Either way you look at it, learn C is to get a better understanding of technology. It will certainly do that for you very well.
Long answer maybe you also want to learn some assembler to see how the machine really work (I would recommend programming a simple microcontroller or the lego NXT things).
If you manage to learn C. It will be easier later to learn an object oriented language later. (Java, C# or C++)
The problem (and the frustrating thing) will be that you first probably wont achieve much by using pure c. Therefore you can also start with toolkits like processing.org which can teach you basics about algorithmic thinking.
Consider proper memory management, et al. There are other languages that implement similar features that C does, but ANSI C is a macro-assembler in the truest sense of the word.
Pick up a copy of "The C Programming Language, by Kernighan and Ritchie" and do the exercises.
Good luck.