263 points · by ingve · 8y ago · 211 comments
There are a lot more risks in C than more recent languages. Koenig's book "C Traps And Pitfalls" https://www.amazon.co.uk/C-Traps-Pitfalls-Andrew-Koenig/dp/0... was extremely useful to me when I was learning the language (available at my local library!). I don't know if there's a more modern version that takes into account recent changes to the C standard.
I note that a lot of the time in embedded work you don't get to use the latest version of the standard because you're using a weird or obsolete toolchain that doesn't support it. Or architectures like PIC, where your stack is hardware limited to 8 levels and indirect addressing is inefficient.
I think a lot of the "pure C" people are disguised as electronics engineers - doing PCB design and microcontroller programming together.
But to be fair, can't that be said that nearly everything is self-taught? From Physics, to mathematics, to music performance and theory, to learning to ride a bicycle or drive a car -- we can read but it takes some time to internalize the structure of each discipline and "own" it.
>>in embedded work you don't get to use the latest version I work in C77, and I'm reminded of how far C (and C++) has come each time I have to #define that fancy new-fangled "boolean" type.
>>I think a lot of the "pure C" people are disguised as electronics engineers... Agreed, this is exactly right. Most if not all of the pure-c world is due to embedded micros with strict limitations (<1k RAM, etc.)
Do you mean C89? I've never heard of C77 and a quick Google search came up with nothing.
Although ANSI C wasn't a thing until 1989, the language had been around since 1972.
The original K&R book was published in 1978, and if you read it, you'll see C that's noticeably different from C89 or newer. 'C77' was probably just a fun bit of shorthand pjc50 used to describe old, K&R style C. Or at least C that's missing some ANSI features.
Let's say that is true. The bottleneck is more about how much you can dedicate to the learning. You run out of time eventually.
I won't link to it directly; instead, the open directory (which lists a pile of other PDFs) is https://doc.lagout.org/programmation/C/.
If there's one thing my CS program did correctly, it was having a two semester course (sort of, officially they were two separate courses) that covered an intro to C programming but also C in terms of machine architecture, down to learning to disassemble C or assemble it from assembly, etc.
I think it was fairly valuable at the time to learn how and why C is important/forms the lowest layer for 'modern' languages.
So, in universities, you have either students who don't see the point, because they'll do java or PHP or JS at their daily jobs, or students who get a little too excited by it, almost specialize in it, and can hardly find a job that fulfills their excitment.
Consider that every electronic device you have has multiple microcontrollers inside running C code.
I would argue that if you want to get your code in the most peoples hands, you have to know C. Just looking around the (engineering) office I'm in at the devices using C:
- My PC.
- The router(s)
- The printer(s)
- the USB hubs.
- the mobiles and tablets
- the lights (yes).
- the A/C.
- The door entry system
- the microwave
- the desk phones.
- the monitors on my desk.
- The dishwasher
- The coffee machine
- The clock
The C world is much larger than most web people realise.
(the list goes on and on...)
Web stuff is maintenance heavy, so even a small webapp requires a lot of human work to keep working in the long run. Add this to the fact that, although most companies, even small ones, need a website that is more than a static single webpage, and/or a way to manage their customer / provider databases, there are rather few companies producing electronic appliances, and unless you live in the right place, most programming jobs you'll find are in high-level stuff.
Game consoles use mostly C++.
All of those do contain some C, but the ratio of it to all the js/java/python/swift etc. on top of that (because this router has web, ui, mobile app and so on) is growing.
> The C world is much larger than most web people realise.
Perhaps. But it was a lot larger 10 or 20 years go, and C was wiped out as "the language" from many domains it used to occupy.
But as somebody who mostly writes C for a living and specializes in embedded programming I can assure you that there's no shortage of employment offers.
Maybe I'm wrong, though.
There's also the fact that there's comparatively little innovation and hype in the C sphere of influence. C rarely makes the headlines these days while every other week you have a fancy new JS framework.
This seems true in any software engineering job - but at the same time, open source or personal projects are experience too. I would be very surprised to find a junior developer CV that didn't mention any projects like that.
I think it's important to use the language that best applies to the course material. For example we looked at UNIX in the OS course and it really made sense to use C there. But when we did Algorithms and data structures we used Java for that. Using C really takes away from the point and bogs you down in language specifics in this case.
But obviously the industries/domains which require C are not the same as those which require Java/PHP/JS.
In embedded software no-one is going to use Java or PHP, like in mobile/web apps software no-one is going to use C. That being said, Objective C is a pure superset of C (which C++ isn't).
Actually they will.
https://www.ptc.com/en/products/developer-tools/perc
https://www.aicas.com/cms/en/JamaicaVM
https://developer.android.com/things/
Just a small overview, as I don't feel like posting all links from everyone active in commercial Java compilers for embedded deployments.
Embedded isn't unaffected by Wirth's law.
For most embedded devices, this is not true.
When you're paying for every device the code runs on... and that code wastes 80% of your cycles in abstractions and indirections then forces you to have 200% more RAM than the equivalent C code, that changes your mind.
Saving a tiny amount on a processor adds up when the multiplier is (say) 1 million units.
The point being that the choice of language is strongly influenced by the domain the software is developed for.
I graduated 16 years ago and I have always got jobs which were pure C, on state-of-the-art products as well. That's still the way it is, plenty of jobs available.
With regard to that question, the OP seems pretty spot one. The identification of certain codebases as good or bad matches my own experience with most of them. The advice to use static analysis and to avoid preprocessor abuse is sound. There are only a few things I'd add.
* You can't really learn C without also learning to use a debugger - probably gdb. That must be part of the lesson plan.
* A more in-depth discussion of good error-handling and memory-management practices would be worthwhile, since C itself doesn't provide these.
* It can be very informative to implement some of the features found in other languages in C. Examples might include a simple form of object orientation, or a map/dict/hash, or a regex parser. Besides their value as a form of practice, this would expose students to some of the issues and decisions involved in making these a built-in part of a higher level language.
News to me.
I learned C in '85 from the K&R book and the project lead for our little startup's Fortran to C translator. No debuggers were involved as far as I can remember.
[p.s. as to "why" teach C. I know many languages at this point, but that initial exposure to C necessitated a deeper understanding of 'the machine', memory management, etc. The core of C is fairly straight forward and imho an excellent pedagogical vehicle for a 'substantial' understanding of "programming".]
Which is more fundamental, the machine, or its mathematical abstraction? Why should people learn C, and not Lisp?
It's usually used for a introductory systems programming course covering enough you can poke through a systems library and know what's going on but to actually teach C development would be a much different course I would imagine.
While widely used kernels are still written in C, new ones are often not, and vast majority of new databases isn't either. Its java (Hadoop, Solr), Go (InfluxDB), Rust (Tikv) among others. That's if you want to be prepared for today. If you look into few years ahead, its also TLA+, Idris or Coq. Same for compilers, its increasingly rare to see anyone choosing C for that domain, especially as a language for teaching them.
> Should schools be teaching C at all?
I'd change this question to "should schools have any say in deciding what to teach?". Wouldn't be better to offer a list of options and let students do the market research and decide on their own?
> It can be very informative to implement some of the features found in other languages in C
That's not how programming works today. You are given those as language primitives and don't think about it same way you don't think how the hardware implements "if" in C. You may be curious and find some use for this knowledge if you have it, but its not very useful when you want to "teach C" (and its also very ephemeral).
> Wouldn't be better to offer a list of options
I do believe I already said something like that. Yep, just checked. I did. The point is that, even if it's only an option, the question of how to teach it is still important.
> That's not how programming works today.
In your opposite-of-humble opinion, which is wrong. In fact, implementing things that are already pretty well known is exactly how programming is taught. Everywhere. In all languages. Students learn about pitfalls, and tradeoffs, and why "this" is easy/common but "that" is hard/rare. If you want to teach C, you have to teach C as it is used and why. A language is far more than just syntax you can learn from a book. Any student who has actually implemented a "basic language feature" in C is better equipped to understand how that implementation of that feature in a particular other language represents only one set of decisions within that space, and that others are possible. I'd say that's pretty important knowledge. Programmers who just settle for "it's magic" and "that's how it is" are never going to be very good programmers.
Yes, they will exist and be used somewhere, but when people design a new database or compiler today, C is not the language of choice anymore, so teaching it for that reason is not useful. Many of the things I am using for storing data today have no line of C in them, same for a lot of research papers in that subject.
> implementing things that are already pretty well known is exactly how programming is taught.
I agree with that, just discussing what those things should be. Implementing low-level language features has more to do with hardware architecture than any code above it, and C is not good for representing (modern) hardware. The way it is done today is that you come up with some mental model, and instruct compiler to generate native code for that, and none of those need to involve C. LLVM IR or some equivalent is more likely to be used if you need some "C-like language".
> Any student who has actually implemented a "basic language feature" in C is better equipped to understand how that implementation of that feature in a particular other language represents only one set of decisions within that space
You don't need C for explaining that, just show two sets of decisions without any code.
> Programmers who just settle for "it's magic" and "that's how it is" are never going to be very good programmers.
It's not magic, but how exactly one feature or another was implemented changed many times for languages I am using because hardware changed, or more research was done, and besides gaining some performance I wouldn't notice any difference. Knowing how to use those features is important, practical and useful for many years, implementation details are irrelevant and may become outdated tomorrow (and they literaly did in many cases).
C is still used, but nowhere as popular or relevant as it once was. It is not "the language of databases and compilers". If you want to teach it, focus on domains where it likely will be relevant, not the ones where it was relevant 20 years ago.
>The other day Neel Krishnaswami mentioned that he’s going to be teaching the C class at Cambridge in the fall, and asked if I had any advice about that topic.
Some googling found a C/C++ class by Neel Krishnaswami[1]. The description says 10 lectures so I'm guessing roughly ~10 hours of instruction. The prerequisites for the class are "none". You can also browse the pdfs of class lectures and past exams to get a feel for what the professor is teaching.
With that in mind, we then consider Regehr's advice:
>My main idea is that we need to teach C in a way that helps students understand why a very large fraction of critical software infrastructure, including almost all operating systems and embedded systems, is written in C, while also acknowledging the disastrously central role that it has played in our ongoing computer security nightmare.
The noble goal of "traps/pitfalls/disasters of C" isn't really possible to weave into an introductory course if a class is currently structured with ~10 hours. You have some hard time constraints there. You'd have to remove some topics ... e.g. maybe remove linkers?, STL?, metaprogramming? -- or -- make the class much longer ... e.g. more hours?, more homework?, more case studies?
An "Introduction to programming using C syntax" is one type of class that requires a baseline of time to each.
An "Introduction to industrial-grade software engineering using C best practices" is another type of class with its own heavy time commitments. Teaching the topics of correct/incorrect uses of C would take even more time than the learning the syntax of C.
Regehr's essay is well-intentioned ("hey, you should teach this very important topic <X-prime> related to topic <X>") -- but he doesn't actually tackle how to squeeze it all into a finite time-constrained class.
You can see the current version of the class at:
https://www.cl.cam.ac.uk/teaching/1819/ProgC
I've moved to a style where the lectures are recorded and the lecture hours have been replaced with lab sessions. I've also mostly[1] removed the C++ content and replaced it with more C. Both the videos and the labs should be available for everybody.[2]
This gives more time for tooling (eg, ASan/MSan/UBSan/valgrind), as well as for various low-level topics like memory management (eg, arenas, reference counting and mark-and-sweep) and data-structure layout and cache optimizations (ie, the reasons why people still use C). Also there's a heavy focus on the lack of safety and undefined behaviour -- beyond the lecture devoted to it, this comes up in basically every lecture.
[1] There are still 2 lectures on C++. If you think that it is impossible to learn C++ in 2 one-hour lectures, you are correct. The pedagogical theory at work is basically "Dumbo's magic feather": the goal is to give students enough self-confidence that the year after they won't be afraid to consider doing a final year project using C++ (eg, modifying a web browser), which is the point at which they will _actually_ learn C++.
[2] I'm not totally sure how I feel about this. On the one hand, obviously I'm happy anyone can access these resources.
But on the other, it's really easy to say subtly not-quite-right things when lecturing. If it's ephemeral, then this is harmless since the effect of lectures is just to cue students too remember what to look up when they are programming on their own, but if they can re-watch the video then there's a bigger risk of burning in the wrong idea.
I suppose the long-term thing is to pre-write a script for each lecture, maybe even turn it into a book.
Regarding the bit about "subtly-not-quite-right things when lecturing": IMHO, it's too tall an order to ask professors to get everything in their lectures exactly right. It seems more reasonable to take a page from good journalists - get as much right as possible the first time around, acknowledge if you discover something was wrong, and provide corrections with context.
Ultimately, I think it's up to students to remember that everyone can be wrong, even professors, and to trust but verify. :)
amazon.com/C-Programming-Modern-Approach-2nd/dp/0393979504
My TXR language is like that:
./configure cc=g++
make && make tests
The Lua people have done this too, and call this dialect "Clean C", I think.Compiles cleanly and tests pass. I usually work with it with a C compiler. Before releases I run it through the C++ compiler to see what breaks, and fix it. (Usually very little and often nothing at all.)
Teaching them at the same time? I'm not sure I would take programming newbies and try to teach them how to program in C such that it also means the same thing as C++. That is to say, I would be confident that I could teach this in a straightforward way; however, the problem would be the lack of supporting material. For instance, there is no reference manual for the "Clean C" language; what would we use as a text book?
One approach would be just to use C resources, and cover the C and C++ differences as one chapter of the course: basically, how to port C programs to the C++ dialect.
I'd definitely teach the students how to leverage the stricter C++ type conversions while keeping the code C, including the following trick:
#ifdef __cplusplus
#define strip_qual(TYPE, EXPR) (const_cast<TYPE>(EXPR))
#define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR))
#define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR))
#else
#define strip_qual(TYPE, EXPR) ((TYPE) (EXPR))
#define convert(TYPE, EXPR) ((TYPE) (EXPR))
#define coerce(TYPE, EXPR) ((TYPE) (EXPR))
#endif
This would be a nice way to cover the preprocessor.In some ways it's easier to interlink C and C++ code than it is to interlink Python2 and Python3 code.
There's more overlap wrt mental models between C#, Java and C++ than there is betwern C and C++. Being able to compile code in both languages means close to nothing if a developer can't understand how a project is structured and how a component is supposed to work.
You've mispelled C++ :)
First year students got Pascal in the first semester and C++ on the second one, followed by Caml Light, Lisp, Smalltalk, Prolog, MIPS and x86 Assembly, PL/SQL, while those taking compiler design classes would also dive into many other ones.
OS design, data structures and distributed computing classes assumed that having learned C++, any student would be able to downgrade themselves into C level on per-case basis.
C only became industry relevant thanks to the widespread of UNIX clones, and in platforms like ChromeOS, Windows, Android, its relevance is slowly fading away.
Still it is going to be around for as long as we need to target POSIX platforms, which is why I follow with interest any attempt to make it safer, as I would wish my computing tools not to be weakest link, if they happen to be written in C for whatever reason.
Given the examples that follow it seems more of an ideological stance that a practical one. C is dead so let's teach Pascal, Lisp, MIPS assembly and Smalltalk?
From a language theory standpoint C is pretty clunky but in terms of getting stuff done it's still one of the most mainstream languages out there.
>having learned C++, any student would be able to downgrade themselves into C level on per-case basis.
That's sort of how I learned C in the first place where I see where you're coming from but I don't think that's good general advice. "Don't learn C, learn C++ and figure it out from there" is sort of like saying "Don't learn Italian, learn Latin and figure it out from there". C++ is a beast of a language, if people are interested in C they should learn C.
>C only became industry relevant thanks to the widespread of UNIX clones, and in platforms like ChromeOS, Windows, Android, its relevance is slowly fading away.
I sort of agree but I'm more of a glass half full type of guy, I think C is less relevant not because it's fading away but because other languages are taking a lot more space nowadays. What used to be considered "scripting languages" are commonly used to build complex applications because we have a lot more RAM and processing power than we used to. Still, kernels and low level code are still commonly written in C or, sometimes, C++ because you need good performance and/or tight control over the application.
Have you missed the time reference?
Java still wasn't invented, Smalltalk had companies like IBM selling products written in it.
MIPS was being adopted across UNIX workstations.
People still though it was worthwhile to buy Common Lisp implementations.
Regarding language comparisons, actually C++ is Italian and C Latin.
C is C and C++ is C++. The analogy breaks down in a pretty major way because Latin has no native speakers, but important work is done in C. They didn't stop speaking Latin because it had problems as a language, they stopped because the cultural apparatus behind Latin collapsed after an exceptionally long period of dominance that Italian speakers never achieved.
I'd like to believe C has a stronger future in the academic world because it is simpler than C++, but also because C is a designed language and C++ is a cobbled-together language, and maybe that counts for something in a field where people call themselves engineers.
Anyone who sees C as a 'downgrade' has badly misunderstood what design means as an engineering concept. It doesn't mean 'does everything', it means that strategic choices were made early in the process after careful consideration.
* Serious hat *
C has a pretty well defended place in the world - it is an excellent choice for manipulating RAM and controlling the size of variables. C++ isn't as good, because its features add a lot of complexity that isn't really desirable when you have a problem that C can handle.
I wouldn't use it for string parsing. Or GUIs except as an afterthought on a project that used C for a good reason.
MIPS I concede, it was remained fairly popular for a long time.
I guess my overall point is that the subjective choices made by this university back then don't really mean much by now. The fact that they taught Pascal instead of C alone proves that they weren't particularly prescient.
>Regarding language comparisons, actually C++ is Italian and C Latin.
I thought about that while writing this bad analogy but I couldn't come up with a better one. I wasn't really talking about the historical relationship between C and C++ but rather about their relative complexities. Learning C++ is vastly more complicated than learning C. On top of that the coding style of a modern C++ application (which generally involves a lot of metaprogramming, OOP and even some functional constructs) is fairly different from a standard C codebase. You don't write C like you write C++ and vice-versa.
There's this good talk about how we should "stop teaching C" (if we want to teach C++): https://www.youtube.com/watch?v=YnWhqhNdYyk
I think the opposite holds true as well for very much the same reasons.
OTOH, C was just standardized a few years before, and linux was just a side project from an unknown student in a small university. And there was a new version of C, with objects, classes, inheritance and all those buzzwords, so why care about the old C anyway?
I don't know about you, but I haven't noticed any dramatic increase in performance - if anything - apps are slower and consume ridiculous amounts of RAM. At work I'm using PyCharm (written in Java) and somehow it's "normal" for it to consume 1Gb of memory. Now if every app would be free to use memory like that, then I wouldn't be able to run more than 8 apps on my computer.
For all the 'memory safety' and 'exception' features that Java has, in my experience - and that includes the time spent with PyCharm too - Java apps are more unstable. Sometimes the JVM downright crashes, or angrily throws a nasty exception, leaving me perplexed as to what happened and worried whether I should restart the app or not.
I don't see a reason why a program like PyCharm couldn't have been written in C. 'It's too low-level' is the common answer - I don't think so; the real reason is probably along the lines of 'I don't want to deal with manual memory management and pointers, and since there's plenty of languages that do that for me - I'll use that!'. C is bare-bones - and that's a virtue - because the programmer is in charge. Every single aspect of the program is a consequence of his decisions. It also removes the opaque 'middle layer' of cruft, like garbage collector and standard libraries.
C is the best language out there, in my opinion, sitting in the nice spot between assembly and higher-level languages. I wish there was a successor of C, which would have carried its philosophy into the 21st century (and no - I'm not talking about the abomination that is C++).
It has always been deeply important in scientifuc computing, and even now it’s very valuable to know C underpinnings of Python & Python scientific computing tools. On the scientific algorithm side I don’t see C diminishing in importance at all, and many critical systems and libraries use it and require young programmers to know it to really use them (zeromq, Postgres C api, CPython, various things written in Cython).
From the point of view of an ex-CERN, very few relevant packages are written in C.
However, my unscientific and unsupported observation is that in fields that have emerged after Fortran went out of vogue as a general-purpose language, this same process happens again, but just with a more modern language. SciPy etc. are quite common in fields like genomic analysis, and I suspect that in 20 years, this will still be the case even if almost nobody else is writing new Python code.
In parallel to this low-level thread of learning, you have to learn other high-level languages, of course.
This is true in x86, ARM, RISC-V, and just about any architecture I can think of, but it is not quite true in C, even when it is implemented on top of a 2's complement architecture. Signed integer overflow is undefined, you have to explicitly convert to unsigned before you do any overflow. Only then do you get the implementation defined behaviour your sought, instead of the usual nasal demons.
Also, assembly doesn't mean much with out of order cores. Data dependencies are likely more important than the generated assembly.
100% agree that this is a dangerous falsehood. I would add the word "always" in your sentence to clarify its falsehood.
But there is an even more dangerous, and more insiduous falsehood: "integers are encoded in some mysterious way that is useless to know".
It's already true in just about every system in the world today, and people with extremely obscure hardware can just hack a workaround into their compiler (or straight up ignore the standard because they're just a handful of people anyway).
I get that back in the day the K&R didn't want to impose on the hardware vendors and left the standard vague where they had different ideas, but that doesn't mean you can't evolve the standard as the hardware evolves over the decades.
Something similar happened to me in a general engineering curriculum where our introduction to programming was in C on some old unix machines that were already already very obsolete (as of end of 90s), with no more IDE or debugger than the equivalent of notepad and debug by printf. I thought it was interesting but too tedious for my taste (I had never heard of IDEs and debuggers).
It's only years later that I went back to programming, from higher level languages, and progressively making my way into more sophisticated / lower level languages.
You don't teach kids math by starting with topology. Most 1st year university students will have never programmed in their life (particularly if all they dealt in their life was smartphones and tablets).
IMHO that's a major part of the problem. Somehow Asm turned into something horrible to avoid, instead of being the crucial understanding that makes everything else fall into place.
I have an engineering textbook from the mid 60s. There is an entire chapter on how computers work and how to program them (in Asm!) to solve numerical problems. The audience of this book was not computer scientists/specialists, but just engineers in general.
Two decades later, computer magazines --- not "developer-oriented", just "user" or perhaps "power user" --- had Asm listings of simple (sub-512-byte binary) and useful utilities for readers to manually enter, use, and modify.
Another two decades later, and the programmers today barely understand binary nor Asm, despite the concepts being so ridiculously simple. A computer is a machine that executes instructions step-by-step, and nothing evokes than more clearly than an Asm listing. An HLL with all its nested structure doesn't convey the same "it's just a list of instructions" to the beginner.
I thought it was interesting but too tedious for my taste
"Genius is 1% inspiration and 99% perspiration."
You don't teach kids math by starting with topology. Most 1st year university students will have never programmed in their life (particularly if all they dealt in their life was smartphones and tablets)
Starting with Asm is like learning arithmetic. Starting with a HLL is like teaching calculus to students who don't know arithmetic by giving them calculators.
I've worked with a lot of both "new-school" and "old-school" developers over the years, and there's a large contrast between them: the former group is heavily reliant on tooling, tends to make many localised changes to "fix" things without considering the whole picture (causing more problems later), and get stuck easily when debugging. The latter tend to adopt a more methodical approach to problem-solving, and while they don't appear to be doing as much interaction with the computer, are overall more productive.
Python -> A restricted C subset ; standard ML -> asm
Teaching fun and power first is good for engagement. Teaching how to think and structure is good for later asm experience so people have an existing concept of how to turn problems into solutions in code.
The implicit context is that we are talking about fields that need to understand programming at a low level: Computer Science, Computer engineering, Software Engineering, Electrical engineering.
There is no point in a Chemical Engineer programming in asm. Just throw some python and R at him so he understands the basic concepts.
But for the fields that need to understand computers at a low level, I would expect enthusiasm from the student to understand how it all works.
What? That's completely untypical for me - I would expect that in a university with decent admission competition (don't know how these things work in US exactly) students that get in know at least a couple of high-level programming languages in addition to taking part in programming competition and general geekiness like brainfuck.
Universities with decent admission competition value well-rounded, high-achieving students. Such students are involved in a lot of co-curricular activities, may take many subjects, and have broad interests. It's perfectly reasonable to expect that they simply never had the time to learn multiple programming languages and compete in programming competitions, in addition to whatever else they do.
And many universities do follow books like "From NAND 2 Tetris".
But to understand semiconductors you first need to understand quantum mechanics. And to understand QM you first need to understand classical physics.
Oops, I think we just ran out of time for a masters program. ;)
That is a horrible, horrible idea. It gives people the misrepresentation that C is really "just" a thin veneer over assembly language, which it most certainly has not been for at least 20 years. Moreover, it feeds the cult of C, the idea that C is the only language capable of being close to the hardware and that other languages or attempts to build system-level languages need not apply.
C has lots of flaws and faults in the language, but we're not going to be able to make any progress in fixing the language, or more likely replacing it with a better language, if we continue acting as if it is the only way to get low-level access to the hardware.
C is a simple language that obviously maps to a programmer's idea of the assembly they'd write.
It biases towards lots of fixed-size stack frames and fixed-layout structs. Writing C code really does feel very close to the "bounded turing machine" metaphor that the CPU gives us.
Other languages have their advantages, but C's proximity to assembler, with all the faults included, really is a great pedagogical tool.
It's not. That's my point. You don't need undefined behavior to get to that point, just consider what assembly you get from this code:
unsigned prog_a(unsigned a) { return a * 2; }
unsigned prog_b(unsigned a) { return a / 3; }
What do you think you're going to get for that code? The "obvious mapping" principle for both codes suggest you're going to get a multiply and a division instruction, respectively. On x86, however, what you get is this: prog_a:
leal (%rdi, %rdi), %eax
ret
prog_b:
movl %edi, %eax
movl $-1431655765, %edx
mull %edx
shrl %edx
movl %edx, %eax
ret
In other words, the first program turned into a load-effective address instruction, and the latter turned into a multiply-hi instruction. Even something as simple as basic arithmetic gets turned into weird instructions, and which ones get this treatment is essentially a game of roulette based on compiler optimizations and particularly heuristics. We're not even bringing undefined behavior into play here, which is usually the major objection to C-obviously-maps-to-assembly; it's falling flat enough without it.The primary advantage C brings, what people are really touting when they tout its proximity to assembly, isn't that it's close to assembly but that it has a well-known, stable ABI for that mapping. Of course, it turns out that said ABI is actually a bad idea if you want performant modern code on most architectures (the infamous array-of-struct versus struct-of-array problem).
Programming without knowing anything about assembler is like driving without having ever opened the hood of your car and changed some oil. Of course, if you are a professional driver you will have specialist mechanicians that take care of all of it, but still it would be ridiculous if you could not do a few simple things.
Plus dialects like Object Pascal and Free Pascal provide a couple of niceties as well.
C isn't going anywhere, even in 10 years. People keep floating Rust and the like, but most targets for C have 16KB program space, and about that much in RAM budget. The C standard library itself is opportunistically linked on these platforms.
The dependencies required for Rust, or even C++, are larger than the code space of 90% of the microcontrollers in your car
Want to build a library that can be used in any of the high level languages? Write it in C and provide binding.
A library code written in PHP can't be used by Haskell. Not without a big overhead. Or vice versa. Write it in C and your are done.
These are where C still comes handy.
https://github.com/tildeio/helix https://github.com/PyO3/pyo3 https://github.com/kyren/rlua https://github.com/neon-bindings/neon
Because C is so raw bone, highly generic APIs from higher level languages can come off as rather lackluster when reduced to C's world view
This early quote filled me with dread. The article fulfilled that dread. IMHO K&R was a classic book but C has changed far too much for that to be of service to students, who would have to unlearn as much as they learn.
Then the author, a college teacher, offhandedly mentions another outdated book as if to recommend it but admits no firsthand knowledge of it. A serious programming student taking these recommendations has just spent a hundred bucks or so and a great deal of precious study time on material that will derail them from their studies with no benefit at all.
That statement could also have been made by someone teaching Javascript in 2018 :D
Kinda funny how we 'fix' high-level languages by augmenting them with tools.
What does he mean by "probably"? How could anyone in PL and teaching community ignore decades of research and development of teaching materials in Scheme and Standard ML?
Scheme (Racket) should be the first language. Standard ML or Haskell - the second. One will tap into two distinct whole sub-cultures (US and British respectively).
Also this way of teaching C as a specialized implementation language which must be used together with special tools is the right way. It is useless to teach C without or prior to machine architectures and assembler language basics.
C is by no means a general purpose language, even when the industry is using it this way and should be taught only as part of specialization in systems programming.
There's only been once case in which I really needed C (because I was programming for a system from the 90s and I only could get C running with the tools I had).
As others have said in this thread, it might be useful for you if you end up in embedded devices or other areas of development, but it won't help you for the bulk of applications written in Java / Python or some other higher-level languages.
No matter how you slice and dice C, its highest abstractions are functions, structs and pointers and all of them can fail catastrophically at each and every point of use.
One can't even add two numbers or read from a variable in C without the possibility of triggering undefined behaviour. And there's nothing that will rescue you from that possibility except paying attention, which is something us humans can be pretty bad at.
When using C, one is setting themselves up for failure, which already hints at the answer to the first big question - no. Go, Rust and C++ can do everything C does in a safer way.