55 points · by lukeqsee · 16y ago · 94 comments
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!
Now 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.)
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.
Or your first cryptic compiler error. I'm pretty sure mine was "numeric constant contains digits beyond the radix".
http://www.amazon.com/Reference-Manual-Samuel-P-Harbison/dp/...
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...)
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.
+ 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.
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...
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.
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.
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.
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.
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.
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.
Of course, it will also help if you start working on the following things, as they will show up in your classes. Don't worry if you don't understand it all right away, that's why you're going to college, right? :)
- How to debug (go read the book by Agans if you haven't already. It's short and fun to read.)
- How to do base conversions (eg. how do you convert decimal numbers to binary? decimal to hexadecimal?)
- How to work with sets (If you know how to work with list comprehensions or the set objects in python, or have played with SQL, that's quite handy)
- How to write a proof. (Here are some handy words to google for: "modus ponens", "modus tolens", "propositional logic", "truth table", "logical fallacies" and, once you get a feel for propositional logic, go look for "first order logic".)
- How a computer works at the level of logic gates. (Go play with the java applet "xLogicCircuits": http://math.hws.edu/TMCM/java/xLogicCircuits/ and see if you can figure out how to design a CPU. It can be pretty fun, although the applet's a little clunky.)
- What state machines are, and how they relate to regular expressions.
- While you're learning C, go look at assembly languages and see if you can understand the relation between C and assembly, and also between assembly and logic circuits.
- How to study and manage your time (Yeah, I'm still working on that one myself...) :)
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.
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".
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.
If you're going to read K&R, make sure you visit the Errata page. There are a bunch of examples that have typos (some of them are not on the Errata page though).
But reading one C book isn't going to teach you everything about C. A lot of the C code out there makes heavy use of macros and multiple pointer indirections, which isn't covered in much detail in K&R. And if you're interested in the newer C99 standard, The C Primer is a great book (but it's quite thorough, which imo, is a good thing).
If you're going to try out the examples from K&R, pass these arguments to gcc: -ansi -pedantic -Wall -Wextra -g
The -g flag is for debugging symbols. I think I still have most of the fixed examples from K&R that will compile, if you're interested in these. Also, make sure to bookmark these two links:
http://www.eskimo.com/~scs/cclass/krnotes/top.html
http://users.powernet.co.uk/eton/kandr2/
The first one gives you some better explanations of what has been said in the K&R book (divided by chapters). The second one has most answers to the exercises in the book.
As for C, yes, you should learn C. In several industry segments (such as embedded systems), you cannot even get an interview if you do not know C.
A nice way to do something in C is to write a text editor using ncurses for the GUI. This was something I did in my second year as a Comp Sci major in school, and it was a great experience (with a friend).
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.
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.
After you understand pointers and structs and have futzed around in the Unix command line for a while, you probably have enough to go on to something like Linux Programming by Example. This book walks through GNU implementations of Unix utilities and the system calls that make them possible.
You don't just want to run ls, you want to know enough that you can write ls by yourself. You want to learn how to manipulate the operating system with it. You will learn a mental model for how operating systems work in a deep, detailed way, which will serve you well no matter where you go.
http://www.amazon.com/Data-Structures-Using-Aaron-Tenenbaum/...
Have much success in your degree!
There is no doubt that C programming is a very good skill and if you see yourself programming a lot in the future, it is more or less a no-brainer. BUT it is still a trade-off. It will cost you time and this is time that you could have spent having fun, making money or learning some other skill instead. You could ask people in any field if X-basic-skill in their field is worth studying, and you would get similar answers.
But yeah, you should learn C :-)
http://www.amazon.com/Code-Language-Computer-Hardware-Softwa...
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.
Oh and see something of Forth if you like the looks of it and some assembly.
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!
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.
If you want to understand how computers work, of course you will need to know assembler and c. This will give you the option to do things that other people can't.
If you want short term profit, you should choose other options.
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.
If you ever have to do low-level programming or Mac/iOS development it will definitely be of great use.
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++
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.)
On the educational side, you will learn about hardware works. Some will argue that there are better languages to learn these things and I'll agree: Forth could be better for learning about raw memory addresses and systems programming. Scheme could better for learning about data abstraction. However, C has several salient things going for it: C is UNIX (for good or bad, majority of serious non-embedded -- and even great deal of embedded -- programming outside of Microsoft or of Windows Desktop Application development targets UNIX) and C is the lingua franca of programming languages.
From the practical side, you will have an advantage too. Higher level languages generally still have pointers (Quiz: is Java pass by value or pass by reference? Answer: It's pass by value, just the value of anything other than the primitive types is a pointer). Higher level languages have built abstractions around I/O and VM but they typically fall under two categories: thin wrappers around C and POSIX APIS (Perl, Python, Ruby) or leaky abstractions that occasionally force you to use FFI to C (JVM based languages). In either case, if you are able to program C you have a competitive advantage against coders brought up purely on those languages.
C is also a good language to learn about data structures: partly because learning data structures in C is really about the data structures, abstract data types and algorithms and not (as C++ and Java centric courses often make it out to be) about OO. This is the book I recommend for data structures in C: http://www.amazon.com/Data-Structures-Pseudocode-Approach-C/... (I'm biased as I learned from it myself and have had classes taught by Professor Forouzan).
Note, I never said anything about performance. Just like it's nearly impossible to hand-write assembly that beats "gcc -O3" (not to say "gcc -O4" or icc) performance wise on modern hardware (out of order execution, NUMA, specialized instruction sets), I am pretty confident that languages will emerge that will beat C even for things like inner numeric loops by being able to reason more about mutability, side effects or lack thereof, exploiting parallelism and caching available in hardware. You won't be writing in C to make computation faster, you'll be writing in C to deal with I/O, memory and the like more efficiently: many times that will mean your code will run faster, but it won't specifically due to being written in C (to be blunt, it will be due to implements of runtimes and programming languages sheltering the languages/runtimes from the hardware/OS; that may not always be the wrong thing to do). See http://pl.atyp.us/wordpress/?p=2947 for a great discussion on this.
Finally in terms of C++: don't study it before knowing C and a non-C++ object oriented language. I was lucky enough to learn C before learning C++, but it was the first object oriented language I tried to learn. Because of that, I thought of OO as a black art. Oddly enough I truly learned about OO in Perl: I was writing Perl 5 (loosely adhering to Damian Conway's "Perl Best Practices") and despite OO in Perl being an ugly hack (it's similar to Python's, but without even the syntactic sugar that hides __dict__ away) the Perl code I was writing was far more OO than any C++ I wrote up until then. I then learned Python and Java; when I came back to writing C++, I was much better at it. Josh Bloch (along with Doug Lea, the author of Java's collections) in "Coders at Work" spoke of a similar experience (in his case, he didn't do any OO programming before Java) and likewise speaks very favourably about C.
I strongly recommend you do the same: learn C and do a non-trivial project in it (writing a compiler for subset of C in C did the trick for me) to truly grok it; continue building projects in Python (or another OO language e.g., Ruby, Java, Scala, Smalltalk, ObjC, Common Lisp+CLOS; pick as many of these as you'd like) to make sure you have a firm grasp of OO before you go after C++.
I'm sure there are many reasons to know C/Assembler. But I felt like learning things like why:
obj.getField().setFirst("random"); obj.getField().setSecond("random");
is worse than
Field field = obj.getField(); field.setFirst("random"); field.setSecond("random");
Another reason I used to like C was that it always made me want to make my code run as fast as it was possible to run. When I write code in Java, I'm looking to make the code work. I'm not saying either is better, they just each have their plus points, but I enjoyed C more due it's challenges and feel I'm a much better programmer for knowing it.
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.