Why? the C standard library is full of gotchas and tricks and goofy shit that makes it unsuitable for new programmers. The comp.lang.c FAQ should be standard reading material for anyone taking a C class. Look at all the posts on StackOverflow about the stdio functions 'not working right' and asking how to do menial things like collect keypresses.
C works well as a first language if you teach it like assembly (use of which as a teaching language is another debate): flip some bits and do some loops. But not for collecting user input/output. Half of your students are going to get stuck trying to collect a list of names from the user, before they even get a chance to get stuck trying to sort the list.
The argument that one "should study other languages before they're fit to learn C" is terrible IMO and somewhat condescending. It's like saying one must learn "nedit/pico" before starting with editors like "emacs/vi". Please stop repeating that - especially if you have not done much C.
I am probably too old to understand the constant hate and criticism on why C shouldn't be taught to beginners. Not everything "has to be easy". Starting with C & Linux, I always had an advantage later in my career because it was these skills that enabled me to drill deeper than those who didn't know C & system programming.
C isn't just hard for beginners because of the language but the rest of the tool-chain. If you can get over this then learning C will also teach how to debug with gdb, strace, valgrind and learning about low level system calls, and a great deal about how a program can be loaded/structured e.g.: dynamic & static linking. Also where the resources are used and how to eliminate it’s bottlenecks. What is a heap, stack or static memory? How does memory management or garbage-collection work on a modern OS? What is IPC and how do the individual system calls function & require as input? How do you compile a program and optimize it for different target platforms? How do you write Makefiles or use autotools etc ...
Tell me again which other language forces you to learn these things? You could easily build a career just on C & Linux because of this "additional learning curve" and extra knowledge that a good C developer will have.
Sorry for all my bias (as I said my views might be very outdated :-))
The first language you should learn should be a doddle to learn, even if it isn't very powerful. This was BASIC (or sometimes Logo) back in the 80s, but you could probably substitute Python or even Scheme today. This language teaches you concepts like these:
* Computers follow instructions supplied by the programmer
* The instructions have to be carefully worded in order for a computer to follow them (i.e., intro to errors and debugging)
* Arithmetic and mathematical operations a computer can perform
* Computers can remember things (i.e., variables)
* Computers can interact with the world (input and output)
* Computers can make decisions (conditionals and program flow control)
These are the very fundamentals of programming and it's amazing how many people haven't internalized these concepts at a deep level.
You can actually get very far with BASIC -- entire companies have been run with BASIC code. (Software written in and for Business BASIC variants was a major industry back in the 1970s and 1980s.) You can get even farther with Python. But once the learner is ready to go deeper, it's time to begin Phase 2.
The second language you learn should be assembly, which familiarizes the programmer with how a computer actually works at a fundamental level, as well as important concepts like CPU word length, memory addresses (essential for understanding pointers), program call and return (and stacks), etc. You don't have to even complete a large project written in assembly to get the benefits of this low-level, bit-twiddling learning.
Finally, the last language you learn should be Pascal. C or C++ would be today's equivalent. In this phase you synthesize the knowledge of high-level programming, structure, and control flow that you learned during your "BASIC" phase with your low-level understanding of machine details that you learned during your assembly phase. Then, the details of how and why the language works the way it does make much more sense.
So yes, to me it makes total sense to teach C -- as (at least) a third programming language, perhaps after several years of proficiency are acquired in the other two (depending on the student).
"Learning C is not necessary for everyone, but there are many posts on Reddit and programming forums with authors who feel like they’re missing something, and I wonder if this is part of what they’re looking for."
I don't think C should be the first language for most new programmers. The article doesn't recommend that. Rather, my argument is that C provides the lowest level abstraction that (most) modern programmers could ever need. By understanding C, you give yourself the ability to break into the black box of any higher-level language or framework. You're not at their mercy if they provide odd abstraction layers or bugs in their code.
It seems to me, at least anecdotally, that beginner programmers using C feel like they can't do as much as other beginners using languages like Python or Java. Often this is because graphics are so much faster to get to when working with the libraries that those other languages offer. Without a standard graphics library, C feels like the weaker choice. How wrong that is, but I've heard this sentiment from several different beginners.
I think part of what draws me to the Handmade Dev community is that we are finding so many people who know how to code, but want to feel more in control of their programs. Many people following my Handmade Quake project have told me that they're considering learning C for the first time ever. Maybe that's a good position for C to take in 2016 - a language to learn once you've learned your first language and some simple logic but want to understand the hardware better, or figure out exactly what code is running when you turn on your Python interpreter. I hope the community is able to plant that seed in the minds of as many programmers as possible.
- C teaches you how computers work.
- Haskell teaches you how programming logic works.
But C has immense cultural value. Lots and lots of extant source code, important source code: Linux. The BSDs. PostgreSQL, SQLite, whatever.
C is the lingua franca. "Everybody" speaks at least a little C, so it can be used as pseudocode notation (without learning some specific, maybe even ad-hoc, pseudo language).
Except it doesn't. If any language "teaches you how computers work", its assembly. While a lot of the quirks of C's design compared to more "pure" structured programming languages (e.g., Pascal) reflect a desire to provide more direct access to common low-level features of systems of the time when C was introduced, it doesn't really teach you how computers work except insofar as trying to understand its quirks might motivate some inquiry into how computers work.
Maybe programming 101 should start with a higher level language and dive slowly on "how does that work?".
Though if you were to start with a language C might be a good one just to show you how sloppy thinking leads to incorrect behavior and poor performing programs.
I thought it was a horrible idea, and I had a pretty miserable experience at De Anza. If you're just starting out programming, you should simply get used to writing code, and not fighting low level details of the language. C is the kind of thing that is useful, perhaps only as an advanced undergraduate course. Most beginning programmers need to start writing code, not get bogged down in pointer hell. And obviously most beginning programmers aren't going to be hacking away at *nix kernels or anything advanced like that.
That said, you could do a lot worse than C for an introductory programming language.
No one should get bogged down in pointer hell. I program C for real work all the time, and I teach it to undergrads, and we use pointers carefully and with awareness. We don't do arithmetic or other 'clever' stuff. And we don't get bogged down in pointer hell.
Why do we teach it to undergrads? Among other reasons, because trying to teach OS or compilers to students who don't get pointers or any other low-level stuff is miserable.
Nothing compares to spending hours and hours grinding through thousands of lines of C and building your own passable OS from a skeleton kernel. It was the hardest course I've ever taken in college and it was my favorite.
I can honestly say I'm a better programmer and engineer after that experience and would recommend anyone to take a course in OS (hopefully in C), and you'll know what I mean.
I firmly believe that book will teach you every bad habit you should never use in actual practice. 30 years ago, many of those "tricks" made sense given the compilers and computers of the time, but today your much better just writing straight clean C (for example using array indexes instead of pointers). Not only because its more maintainable, but because it often generates more efficient code due to hardware support for base+displacement addressing and compilers abilities to understand the intent and optimize the resulting code. Furthermore, code like what you find in that book is what has given C a bad name due to buffer overflows and the dozens of other things people think are the fault of "C" rather than the programming environment around it. Not the least of which is a formatting scheme that hides bugs.
So, PLEASE, if you are learning C, stay as far away from the K&R book as you can until you have gained enough proficiency to understand the pitfalls of may of the techniques taught in that book.
Not sure exactly what you mean by the "hardware support" part, unless it is machine instruction support of some kind for C syntax. (But I thought that a basic level of support for that was present in most processors - things like base + offset addressing mode, indirect addressing via the value in a register, etc.)
But:
a[i] is equivalent to star(a + i) in C. (The K&R book says so somewhere, in either the original or the ANSI C edition.)
And star(a + i) is equivalent to star(i + a), by commutativity (a mathematical property) which is equivalent to i[a]. [1]
So you can write any of the above 4 expressions equivalently. I verified it by writing and compiling such code after reading this in the K&R book years ago; still remember being surprised until I saw the logic made sense.
[1] Or at least, it was so, some years ago, not sure about in C99 and such.
I used "star" instead of the asterisk symbol in code above because the actual asterisk turned following text to italics - HN markup.
However I don't think you should start writing all your programs in C! Use the right tool for the job. If that job is something C makes sense for then go for it but if you are writing a webapp I wouldn't recommend you use C (although I would if you needed a fast, native library to call from your webapp).
Honestly, at this point you're probably better of using Rust for that.
In this grand scheme of things, C is not better than Python. I don't mean the we should avoid going deeper. I mean that each level of abstraction has its own benefits.
BTW: How many of you started learning natural numbers not from counting fruits/blocks/candies, but from the ZF set theory axioms and the von Neumann construction?
Ha ha, good one. I don't even know what the ZF set theory is. Not putting down theory either in general or in this case, of course.
The march of technology (and knowledge in general) is one of constant abstraction. We figure out ways to do more with less so we have more capacity left over for further advancement. You don't know 10x10=100 because your brain derives it every single time, you just memorize the fact, hopefully with understanding. In software terms that means higher level languages with sophisticated libraries, and languages like C are relegated to more of an infrastructure role.
This isn't necessarily bad. If the technology is more capable then everybody wins at the end of the day. As the author points out there will always be a niche of enthusiasts/zealots who will lead the charge and further the field. The remaining 99% will learn what those people come up with and apply it. To the best of my knowledge that's how it's always been, and it's worked pretty well so far. So I'd say let people start off with high level languages if they want, but expose them to lower level languages and let them choose which demographic they end up joining.
Does someone brand new to programming, who’s excited about making changes in the HTML on a page or setting up a blog to later customize, or just wants a slow introduction to more complex topics, really need to worry about cache coherency? Or threading? Clearly that’s a problem for down the road. Maybe that’s a road that some developers will never need to walk down at all, depending on their focus.
His position is that, while C may not be the best language for everyone coming into programming it is beneficial to learn it eventually because it allow you to peel back the layers of abstraction underneath all of the goodies in your high-level language. This allows you to better understand and reason about even your higher-level code and opens the door to fixing and understanding a lot of things that would have constrained you previously.
I completely, whole-heartedly agree. We don't always need to be able to plumb the depths of our abstraction stack. I don't believe it is necessary for a beginner. But having the ability to go up or down in the stack is extremely valuable. C is worth learning.
Don't get me wrong: I use perl when it makes sense; I use Java when it makes sense. I quite like playing with new languages and seeing what I can do with them. There are many languages that are safer than C or easier to do particular tasks than with C, but I am very rarely happy with the trade offs. When I am trying to do serious work that is meant to stand the test of time, I'll use C.
You have no control over C's register allocator. (Well, very little, `register` doesn't mean much.) Assuming you use it, you have no control over how the standard library's malloc() and free() work. malloc() could be best-fit, first-fit, something else entirely, who knows. No control over fragmentation. You have no control over the CPU's branch predictor or cache eviction policy.
You don't control the OS's thread scheduler, virtual memory implementation, etc.
Everyone picks a spot where things below that level are details they don't want to care about. You may have picked a slightly lower level, but there's nothing fundamentally different between you liking C and someone else liking Java (which is GC but not interpreted), Forth (interpreted but not GC), or Ruby (GC and interpreted).
People do serious work in Ruby for the same exact same reason you do serious work in C, it lets them control the things they need to control and ignore the things they don't.
In my last big C project it wasn't uncommon at all to find memory leaks and out of scope references to stack variables. You'd add a line of code and the whole thing would blow up because it changed the state of the stack and unmasked a problem in another module. I would find the culprit and figure out if they were being sloppy or they just didn't understand, and it was usually the latter. This from people who had been programming professionally for a year or two.
But what surprised me more was that the courses became more institutionalized. Professors were bettering you for a cushy career as an employee at a bank or large corporation instead of showing you how to use the computer to solve a dizzying amount of technical, social and and financial problems. I could understand a technical college emphasizing a safe, risk-free entry into a nice career but I expected better from a university.