292 points · by Halienja · 10y ago · 372 comments
Want minimal syntactic sugar and a language that's not prone to feature-of-the-week hipsterism? C
Want to make tiny binaries for some obscure system with virtually no storage or RAM? C.
Want to know where every last cycle goes and explicitly control everything? C
Want to trivially import any of the huge array of existing libraries? You guessed it...
Want to do something you really shouldn't, and any 'sensible' language might attempt to stop, but is called for right here and now...
It's not the safest language, it's not the most expressive, but the syntax is small enough to hold in your head and the body of existing, available work is HUGE. I've been working in C for 15 years and, while I enjoy forays into other languages, it will always be my love.
C++, D, Rust, ...
> Want to trivially import any of the huge array of existing libraries? You guessed it...
If C can talk to it, so can anything else. Granted, having the preprocessor means no bindings, but C++ has that too.
> Want to do something you really shouldn't, and any 'sensible' language might attempt to stop, but is called for right here and now...
C++, D.
Want undefined behaviour? C. Want segfaults? C. Want memory leaks? C. Want memory corruption? C. Want to write insecure code? C. Want to write your own containers? C. Want Go's approach to generics? C. Want to spend 3x as much time writing code as a comparable C++ solution (not to mention more productive languages)? C.
I've known C for 21 years now. I just quit a job as a C programmer for embedded systems. If I can help it, never again.
Want all of that and a whole bag of inconsistent design choices? C++. Because, yes, one day you will meet a C++ codebase which is a mixture of "forbidden C++" and "modern C++".
Perhaps so, but reading it back takes 5 times as long.
Coming from Python and dabbling in C for embedded software, this is the one that gets me the most. It's so frustrating to be spending hours doing something you can do with one line of the regex library in Python...
Overall though I think what you may be seeing is simply the stratification of software development; what began in the 70s and 80s has come to fruition. There are different planes of existence, like thermoclines in the oceans. A developer may (and is likely to) live in one forever, without crossing above or below. Each has its purpose. While I wrote my own database and rendering engines in the 80s as part of applications development, I wouldn't dream of it today unless there was a very, very good reason. Same goes for network stacks. Or math libraries. Memory managers. Etc.
Sure, coming from the Python ecosystem, you will feel as though there is a shortage of auto-pilot-like assistance, but you must realize you are in a very different world now - Embedded work is (frequently) focused on predictability, responsiveness, efficiency, and even moreso with realtime work. You are the engineer in charge, and all the safe boundaries and cushions of the interpreted language world are now missing. Want to REALLY feel naked? Drop down to assembly on any platform and try to emit a simple "hello world." For those who have never done so, it's a humbling experience but rewarding.
Remember that C was designed as a "universal assembly language", easily portable to different architectures and close (enough) to the metal so the programmer can have full control over the machine. Best analogy I've heard: C is like a chainsaw - used carelessly one can cause enormous damage and destruction, but in the hands of a skilled/experienced craftsman, very clever solutions are possible.
The Lua docs provide some examples of integrating Lua into C here - https://www.lua.org/pil/24.1.html.
However it doesn't give you the control or the performance of C without some serious work, some of which is usually work in C!
(I know there are some "tricks" you can use to make smaller Rust binaries by chopping things out, but of course the same applies to C. For example, I can take the C binary and just truncate the file down to 2230 bytes without breaking it)
C binaries are small because they're assuming you've installed what is ultimately the C runtime on your system (libc).
The tricks you describe are just changing the compile options to those that C uses by default.
on top of that you can apply the same tricks as C; e.g. by running strip, truncating the binary, writing a linker script, etc.
For more: https://lifthrasiir.github.io/rustlog/why-is-a-rust-executab...
Or if being verbose is desired, do you want to do a job for a computer that you are not suited to do but the computer is?
I want optimal syntactic sugar.
Can be solved by not doing stuff that are considered as undefined. Think of them as the standard's say of saying "don't do that". It's quite easy to do in most cases.
> Want segfaults? C > Want memory corruption? C > Want to write insecure code? C
There is absolutely nothing in C that makes it more prone to these in comparison to other languages. Moreover you can easily solve these by using an implementation that protects you. (such as one of the multiple C compilers that guarantee to produce safe code without UB or the sanitisers in gcc and clang)
> Want memory leaks? C
Can be easily avoided by avoiding dynamic memory allocation. Moreover nothing stops an implementation from using a GC.
> Want to write your own containers? C
This applies to all the languages as none of them happen to have an implementation of every container in their standard library. It is true that C lacks that in comparison to some other languages, however implementing the common ones (or using a library) is an extremely trivial thing to do.
> Want to spend 3x as much time writing code as a comparable C++ solution (not to mention more productive languages)? C
You must be quite the shitty C programmer to need 3 times less time to write something in C++.
I have a list of "don't do that" rules. I still do them, usually by accident.
> There is absolutely nothing in C that makes it more prone to these in comparison to other languages
If you believe that, then there's nothing I can say that will change your mind.
> Moreover nothing stops an implementation from using a GC
Many applications can't use a GC. For tiny systems, forget it.
> none of them happen to have an implementation of every container in their standard library
That completely sidesteps the fact that in pretty much every other language the containers one would like to use 99% of the time are implemented in the standard library or the language itself.
> You must be quite the shitty C programmer to need 3 times less time to write something in C++.
Only a shitty C++ programmer _doesn't_ write code 3 times as fast as they'd write C.
I've worked in numerous languages and platforms, even changed careers from programming (graphics) to film and television. But I'll always be a C programmer.
However, for me, C falls down on large projects. I know there a lot of hate on objects lately, but the complexity of a project decreases while the maintainability increases when you use properly designed interfaces. You “force” the new guy to do it right because the interface makes it obvious. I know there are ways to enforce encapsulation with C, but I’ve never worked on a (large) codebase where this has been successful.
I try to organize into specific components and piece them together into a sequential or threaded or both model, depending on what I do. Of course, it is specific to what I do. But there are large C projects that are successful - for example, Linux kernel.
C++11 really does put that to rest. Move semantics basically enable complete avoidance of manual memory management. There are many other powerful and convenient aspects, but in C++11 and on managing memory essentially goes away. I essentially never allocate memory explicitly.
Depends on what you do, I guess. I process huge image sequences and have them run (play at least) in real time. I cannot afford to not allocate memory manually. In fact, I have to and I have to manage heap myself. I also have to manage thread affinity, in order to have threads (producer/consumer) share L2 cache to get the speed I want, and all that on multiple processors.
Sometimes you must do that manually.
You know what you get with C and it just works as you expect always. I always feel refreshed after writing C.
There are perhaps a few very minor enhancements I'd suggest, but I'd be very reluctant to open the floodgates and ruin it.
C is one of the few languages, along with C++, that revels in undefined behaviour. It can be very hard to reliably know what a C program will do if it's not written very carefully because there are so many constructs that look benign but which are technically wrong, and the compiler will mercilessly exploit in order to optimise your program into nonsense.
Well, it works as defined in standards. It's just not every programmer knows what to expect. C is simple, yet powerful language and with powers comes responsibility. It's not like you can throw some libraries/modules/objects (or whatever it is in other, safe languages) together, upload to server and call it a day -- static testing, debugging, unit testing is a vital part of any semi serious C project.
Every language works just as you expect if you have the right expectations.
"C is memory with syntactic sugar." Dennis Kubes
So... you compile with optimisation switched off?
For instance, when writing a scalar-processing loop over a fixed length array with no dependency between iterations you can reasonably expect the compiler to convert to SIMD instructions and do some unrolling. It's not too difficult to check the binary that this happened, and if it doesn't happen you just write it yourself with intrinsics.
I often think of C + compiler's optimization passes as a "scripting language for assembly".
while (*++targ = *++src)
;Unless the data is user-generated, in which case you might have a security issue on your hands... :)
Kinda like with everything else related to C!
The problem is, it's also up to the programmers who worked on my codebase before I did. I would never write that atrocity, but I've come across that atrocity quite a few times in code I needed to modify.
(but usually generates compiler warning without an additional set of parentheses)
I particularly want to take issue with the claim in the original post that "C helps you think like a computer". C's machine model might have been a good model for the machines at the time at which it was developed, but it hasn't been an accurate model for a long time. A simple example is C's assumption that memory is flat and all locations have an address. With multiple layers of caching and nearly a couple hundred registers in current CPUs this is far from the truth. If you think programming in C is programming at the machine level you are deluding yourself.
Add to that the aggressive optimisations performed by modern compilers and that mental model breaks completely. It may be a good conceptual model, but it's not the model under which your program actually executes.
However, even programming in assembly keeps you very "far from the truth", since modern CPUs are very complex beasts internally and that complexity is mostly not visible in the ISA.
Few things have made me more nervous in my career than seeing a ISR making use of C++ standard library features. When I asked the engineer how they were sure this was safe and worked under the covers, I was immediately dismissed as "the compiler takes care of all that." The project only ever worked at -O0 and was still buggy. The engineer blamed the hardware.
I wonder when we'll get an instruction set that gives us fine-grained control over the different levels of caching, or even the ability to create FGPA-esque microcode functions, etc. Compiler writers could have the time of their lives with such an ISA.
I'm not very experienced with GPU programming, but my understanding is that the programmer has more control over memory locality there.
Another point to remember is that CPUs have coevolved with programming languages. I think if languages tried to expose more detail in their machine model, ISAs might evolve to match this.
Finally, hardware designers mostly aren't programming language people, and programming language people generally haven't been interested in languages in the same space as C (though that is changing.)
I think there are a couple of ways that it could be argued that "C helps you think like a computer": most CPUs have been designed around running C code quickly and the oddities of the language force you to think like the compiler (which has both positive and negative aspects :/ ). For that matter, things like UNIX and POSIX make more sense if you underderstand C (and vice versa) and there are multiple complete and fairly popular open source operating systems you can examine that are written in C (and the mailing lists of these operating systems can be excellent places to learn a bunch of low level stuff). On the negative side (as you mention), I think it is very common to think of C as a lower level language than it really is (I'm planning on learning Ada, which it sounds like has more low level features, although still not the same as learning CPU architecture).
C forces or encourages you to learn a lot more about everything than other languages, which not a great thing if you just want to write a reliable program but isn't such a horrible deal if learning such things is part of the point. D, Ada, and to a lesser extent Rust seem similar in some respects and with significant benefits but without the easily accessible operating system projects (so far, that I know of).
So I guess my addition to that excellent post would be that if you are going to learn C you should get involved with an OS project. The BSDs are particularly good for this due to being more integrated systems and slower moving with more design consideration given to new features than in Linux. I'd personally recommend NetBSD, where the tech-* mailing lists are high quality and fairly low volume, but any of them should work well.
There's also more serious efforts: https://www.redox-os.org/ has made tremendous progress in a relatively short period of time, https://robigalia.org/ is an extremely interesting effort for Rust on top of seL4.
In case it wasn't clear, my "to a lesser extent Rust" comment was just about my impression that Rust can more easily be used without worrying about optimization level details vs. D or Ada or C. Which is a good thing if true since Rust can also deal with the low level stuff, although possibly it is just my mistaken impression (I only actually know C out of these languages).
Further, C helps you think like a particular evolutionary path of computer, and the language and architecture developed mostly in parallel. It's like saying that giraffes have long necks because they need them to reach the tall branches.
Also, it can't be denied that there will always be areas where the exact control of memory layout that C gives you is not just nice-to-have for performance reasons, but a prerequisite for a good implementation or understanding of some algorithm.
Neither of those points, however, mean that C is the only suitable language imaginable for doing low-level work, or that the past forty years of programming language research are irrelevant to the spaces that C excels in. I think Rust shows quite succintly the immense value of the combination of an expressive type system and C-like zero-overhead abstractions.
Learning C, like learning some assembly languages, to some extent is probably necessary if you're serious about understanding computers, just to understand what's going on. But when it comes to actual use in production systems, I am glad that we are seeing serious alternatives to a language so prone to simple bugs as C. Think of all the time and money that have been lost to buffer overflows and segmentation faults.
Do you know which non technical advantage C has? Multiple implementations. Don't like gcc, use clang. The Keil compiler has a certain bug that screws your project? Use IAR. And so on.
That is one thing. Another thing (at least at the company i work for and the ones i know) is, companies that write code for infrastructure or machines that need to rung a looong time won't ever trust a language (and toolchain) that comes in versions and not in IEEE standards. That may or not be stupid, but it is the reality i've seen so far.
P.S.: Before someone says C only NOW has multiple implementations, yes, at some point in time that was true, but i was VERY easy for compiler vendors to implement a C compiler (speak ROI). Time will tell if this will be true for Rust as well.
The Rust team has a very high commitment to stability, and does way more to maintain stability than any other language/compiler I know of (by testing new versions and potentially-breaking-changes against the entire ecosystem)
> still don't (seem) to understand the non technical problems it has.
Just because multiple implementations of Rust don't exist (there are a couple but nothing 100% feature complete iirc) doesn't mean that the community doesn't understand the need for it. Alternate implementations are hard to write and take time. The language is still new, but work is being done towards speccing it (e.g. the unsafe code semantics work being done right now). Note that most languages have gone for years without getting a proper spec or alternate implementations. This is an advantage C has over Rust, agreed. It's usually not a dealbreaker, though.
Yes, but my experience is that they don't seem to manage generating the kind of trust needed to get my peers to give it a serious go. It's a purely social and subjective problem.
>Just because multiple implementations of Rust don't exist (there are a couple but nothing 100% feature complete iirc) doesn't mean that the community doesn't understand the need for it.
As i said, it SEEMS to be so. That means that it is a subjective impression my colleagues and me get. It is the job of the community to generate trust, not the (potential) user's.
>Alternate implementations are hard to write and take time.
Yes, see my P.S. Time will tell if it's worth it to compiler vendors. C made it easy for them right from the start.
>Note that most languages have gone for years without getting a proper spec or alternate implementations. This is an advantage C has over Rust, agreed. It's usually not a dealbreaker, though.
Not a dealbreaker for whom? In the infrastructure and big machine business you will see C, some C++ and Ada. All standardized.
If the Rust community wants to offer an alternative for C, C++ and Ada, AND want Rust to succeed as an alternative, they need to understand that trust (in the toolchain) is generated in different ways in different industries.
I'm all for this but I really don't see how Rust's stability can hold a candle to the international standards committees that govern C and C++ (not to mention the investment from the big tech companies in developing and implementing these standards)
> as much they still don't (seem) to understand the non
> technical problems it has.
What makes you think that? The Rust developers have had their eye on a written specification and a path to standardization for a while now. And I've seen nobody arguing that alternate implementations are undesirable, only that that's hardly a priority at the moment, especially in this day and age (the "you need multiple compilers" argument was more veracious before compilers were all FOSS and all grew retargetable backends, today multiple implementations are useful mainly to rule out reflections-on-trusting-trust attacks).Yes, for most developers this is true, but not all (see post above). Try looking at it from my perspective. If i suggest to my peers to give Rust a (serious) try and evaluate it for use in a project, this "hardly a priority" generates the impression of "we don't care right now, maybe someday". So they say "fine, i will give it a try in x years, when multiple implementations are available. It the meantime we will wait and C.".
So basically, chicken and egg. Again, the Rust community needs to understand what kind of impressions they generate vs. which impressions they want to they generate.
Update: Just to make that clear, i have to look at it from the perspective of an industry that produces infrastructure and complex, big machines.
Update 2: Fixed typo.
The syntax of the language is also designed to help the compiler writer and not the user – think of "declarations mirror use", the irrelevance of the order of type specifiers, etc.
Err, I would agree with the statement that the syntax of C (mostly) favors the compiler writer, but "declarations mirror use" is decidedly not a good example for a couple of reasons:
1. The grammar for type declarations is actually quite complicated[1]
2. Agol, which inspired C's type system, actually had a much more "compiler friendly" type syntax[2]. C's syntax is designed to be more programmer friendly.
3. Dennis Ritchie makes it pretty clear that "declarations mirror use" was intended to make use and declarations visually similar, which implies that it was done for the benefit for the programmer[3].
[1] Compare https://www.lysator.liu.se/c/ANSI-C-grammar-y.html to [2].[2] http://www.csci.csusb.edu/dick/samples/algol60.syntax.html#5...
[3] "Analogical reasoning led to a declaration syntax for names mirroring that of the expression syntax in which the names typically appear...In all these cases the declaration of a variable resembles its usage in an expression whose type is the one named at the head of the declaration." from http://www.jslint.com/chistory.html
In my view, a "user-oriented" declaration of an array of pointers would look like `pointer[10] of int`, and not `int *arr[10]`.
If you look at another old language, Ada, you'll find much more precise control.
In theory that's important, in practice you rely on implementation-defined behavior and put it behind a layer of macros. It's not perfect but it works - low level capabilities are not the problem in C.
Lack of high level abstractions make scaling it up and maintaining a PITA.
https://commandcenter.blogspot.com/2012/04/byte-order-fallac...
Software engineers these days are so lost in abstractions and design patterns that they forget that at the end of the day a program is just data, and a set of operations that transform that data. There’s huge value in writing code that does everything explicitly from top to bottom, both from a readability, and from a debugability standpoint. Instead, these days, software is designed using UML diagrams and OOP concepts while pretending that objects are ideas that float in some abstract disconnected space. All in the name of concepts envisioned by people who write books instead of programming, and who never saw even a glimpse of a problem you’re trying to solve.
Languages like C will never become obsolete. There’s always a new frontier of hardware that needs fast, tailored software. Few years ago that was phones, and watches, next it might be tiny implants. Anybody who thinks languages like C is obsolete should ask themselves “why was my Comodore64 more responsive than a PC i bought in 2016?”, or "why is my dual-core Android smartphone with 1GB of RAM barely usable after a year”. Modern languages and programming practices have this amazing ability to completely negate the massive progress hardware has made.
Not because of C; that's for sure. The two biggest reasons are:
1) Your Commodore 64 is doing at least a couple orders of magnitude less actual work. This has less to do with language choice than it does with a programmer who is fully in touch with the problem to be solved and can identify unnecessary work to eliminate.
2) Your Commodore 64 is effectively designed in a way that prioritizes latency over throughput. Pipelines and buffers are small-to-nonexistent. When you read the joystick port, you sample the state of that joystick at that very microsecond; you're not asking one microcontroller to ask another microcontroller to start thinking about sending the input it sampled 10 milliseconds ago. The worst-case latency from changing the video data to seeing it on screen is measured in lines, not frames (assuming a low-latency display like a dumb CRT). Virtually nothing is asynchronous. Systems aren't built that way anymore because we want to be able to push massive amounts of data through subsystems that can't all run in lockstep to one fast master clock, and that means lots of buffering and burst transfers.
I understand that computers worked differently back then, and that software usually had full control over the machine, but please, don't talk to me about buffering, etc. when we're talking 6502 versus an i7, or an A8. I understand that input and output aren’t instant these days, but that’s an order of tens to hundreds milliseconds of delay. It doesn’t explain why Visual Studio takes tens of seconds to compile “Hello, World!” on a 16 core processor. Or why your Chrome tab eats 600MB of RAM.
Since you want to talk about buffering and built-in input delay, here’s a comparison of text editor performance:
ObjC and Java
>>Anybody who thinks languages like C is obsolete should ask themselves “why was my Comodore64
Assembly and Basic.
ETA: it's probably worth put scare quotes around "resource constrained" when talking about Android and IOS but phones were dreta's example.
> ObjC and Java
To defend the parent's point, Objective-C is a pure superset of C. And there are quite a few frameworks on the Apple platforms that are pure C (not Obj-C), e.g. CoreGraphics, CoreAudio, ImageIO, OpenGL, etc.
Was pretty eye opening video to see that, that some of those complex looking instructions with a modern optimizing compiler can be surprisingly neatly optimized away.
BTW, if you want to run GEOS faster, get an REU and let the DMA transfer fun begin. This is 2016 after all; you must keep up with the times :)
C has three fundamental issues:
1) How big is it?
2) Who owns it?
3) Who locks it?
C's lack of help in dealing with all three is the main cause of most memory-related program bugs. I once tried to fix #1 in a backwards compatible way. See [1]. After much discussion on some mailing lists, there was general agreement that this was technically feasible but not politically feasible.
(I once had hope that Rust would replace C, but Rust has gotten too fancy, with too much input from the functional programming people, the template enthusiasts, and the type theorists. Rust isn't bad, but it's at least as complex as C++.)
[1] http://www.animats.com/papers/languages/safearraysforc43.pdf
Rust has gotten too fancy
Could you identify a simple Rust core that has all the good parts
while avoiding the complexity you mention? What is superfluous in Rust?By "templates" you mean macros? If so, how should a language implement the functionality that's currently delivered by macros?
I'm asking because I'm thinking about adding HKTs (higher-kinded types) to Rust.
> it's at least as complex as C++
You repeat this phrase like a broken record, but have yet to back it up every time that I have asked why you have this opinion. C++ is an order of magnitude more complex than Rust.- The functional control flow style, where everything involves closures, is much harder to understand than imperative control structures.
They're not opaque. Templates are more opaque because of the lack of self documenting bounds, perhaps (i still wouldn't call them opaque, just more so than Rust generics)
This is the millionth time you're repeating that garbage about imperative control flow. It boils down to a use of closures in a few stdlib functions. We've had this argument before, the essence of your point is that it is different from C. Of course there is a learning curve but that is there the other way too. Once you get used to it the control flow is pretty straightforward. And in most cases you can avoid the closures and write your if or loop manually.
Yeah, i remember using the Mac OS written in Pascal. Strangely enough, i remember it being buggy as hell and crashing pretty often (no memory protection at all).
But this is up to you to enforce.
2) & 3) are pretty much covered in undergrad CS programs. If it hurts, you're doing it wrong - you don't have to dip that deeply in to OO to find excellent patterns for solving it, of you even need to go that far.
On certain platforms like the Win32 API, 2) &3) are hopeless by, it seems, design. Just do your best then :)
The latter sentence sounds like a non sequitur.
Although technically a superset of C, C++ has constructs and paradigms which make you think in totally different ways.
In fact C++ introduces many language features which (should) protect programmers from 'C' mistakes in their C++ code.
For example, in modern C++ you shouldn't manage memory directly, but use smart pointers and containers. You can, but you shouldn't.
And there are dozens of such recommendations. Which is the right approach, imho. You can still do the low-level dangerous stuff if you feel brave, but officially you should stick to the 'safe' paradigms that the language now offers.
However, the paradigms that C enforces on you are the paradigms of how to do things fast. You wear no protection so you are much more nimble, but much more vulnerable at the same time. Which is good for some types of projects, but not for all.
`auto` has very different meanings in both languages, and you can't omit the return type of an int-returning function in C++
Also, C++ doesn't allow implicit casts from void* to any pointer type, which is a common C idiom.
Sure they are. C/C++ are often cobbled together in a sense that they are both low level, non-memory safe and non-type safe languages, as opposite to high level, safe, memory managed (aka "modern") languages.
I recently bought K&R, doing a bit of micro-controller stuff without the Arduino abstractions, and this I can agree with. I now have an idea how code really gets executed on a CPU, and how the contract between the bare metal and the assembler code for doing so looks like.
That the concept of a stack is built into x86, and why calling conventions are important, all of this stuff I had no idea doing Java development for all these years. What do I need a linker for? Object files, what?
I don't really like it, but there is something strangely beautiful about it - if you look hard enough, you can see the machine shining through the unforgiving cold blue ice that is C.
Curious what you mean by that, in what way learning C made you understand this better than Java or any other language did?
#[no_mangle]
pub extern "C" fn count_substrings(value: *const c_char, substr: *const c_char) -> i32
So I came at it from the opposite direction, and once you go down the rabbit hole of what "no_mangle" and "extern C" does, thats where it leads you.Along the way you inevitably pick up topics like alignment and struct packing.
> It is still one of the most commonly used languages outside of the Bay Area web/mobile startup echo chamber;
Fair.
> C’s influence can be seen in many modern languages;
Saying that that's a reason to learn C is a non sequitur. Why?
> C helps you think like a computer; and,
So does C++. And Rust. And D. All the stuff about databases can be done in any of these languages, C doesn't have a monopoly over letting you be close to the metal. It's one of the only languages that pretty much forces you to be close to the metal, but that's not a plus point.
> Most tools for writing software are written in C (or C++)
True. Hacking on your tools is great. But hacking on your tools rarely requires proficiency in a language (unless you're doing something major, which you're not), I've hacked on tools written in all kinds of languages that I don't know.
And the justification behind this point follows the same flimsy reasoning as the previous one, the reasoning that C is the only low level language out there. The post gives "browsers, operating systems and languages" as examples, but all three of these exist in Rust (Servo, redox/intermezzos/a bunch more, and rustc). I'm sure that D is up to the task for such software too, if it doesn't have them already. C++ of course has all of these.
Sure, you need to know some C when doing low level programming and/or FFI. But nobody's arguing against that. "Everyone “has been meaning to” learn Rust or Go or Clojure over a weekend, not C." is a false dichotomy. With Rust for example it's pretty easy to learn enough C to manage once you've learned Rust (there are tons of folks in the Rust community who have done this). You're not forsaking C, you're just focusing on something better. You can learn enough C to be able to read code and do FFI, and you're pretty much set for writing databases or operating systems or networking stacks or whatever. Reading C isn't too hard and if you speak another low level programming language you can pick this up over a weekend.
The only valid argument I see here is that C is is still used a lot in existing codebases, and you should be proficient if you want to be able to contribute significantly to them. Of course. But cut all the crap about C being the only language that can handle these use cases.
> The post gives "browsers, operating systems and
> languages" as examples
Actually, I can't think of any browsers that are written in C. Unless, wait, does Lynx count? :)> Saying that that's a reason to learn C is a non sequitur. Why?
To better understand how and why those modern languages work the way they do. The same as learning assembly helps you better understand how computers work (and what C really does under the hood). Learning about layrs under your abstraction layer typically leads to better understanding of your abstraction layer.
I understand the need for mechanical sympathy. This is not mechanical sympathy. At best, this is _historical_ sympathy.
As a programmer, one has no choice most of the time on what language a program should have been written, in my career, I have mostly been modifying and adding code than writing new code from scratch. This mostly means C, for anything close to hardware, as electrical/electronics engineers are familiar with it. Another way of saying this is that, "I program in whatever language, my team is programming in".
Embedded systems also have size limitations and the problem of bootstrapping on new hardware. C has time and again proven to be easy.
If you examine a typical GNU/Linux distro, you see all kinds of programs written in C that ought to have been written in a type safe language because most of the time, C's features were not required for the program.
I wish more and more "new" programs are written in all kinds of languages (not just Rust, but in Python or Haskell or OCaml ..) other than C and that we restrict C to the things we really need it for.
Overall, knowledge of C is only going to help. Whether you want to use it to use it for your next project is something that depends on the problem at hand.
Of course! To be clear, I'm not arguing against learning C in general. I'm very happy that I learned C.
I'm arguing against the point made in the post that you should learn C over newer languages. And like I said, "shit my codebase is in C" (i.e. point #1 from the post) is a totally valid reason to learn C. The other points are not.
The K&R is a classic, but it shows its age, especially as we have reached C11 by now. And C2x might be right around the corner. A third edition of the book won't be that bad.
That's probably a pretty good analogy - it's useful maybe for historical value, but it has to be understood that some of what's listed is no longer best practice and you can't, for instance, go around stoning adulterers and amputating thieves' hands.
To clarify, GP here is probably talking about UB security holes, not stuff like XSS. Most intro texts I've seen have "watch out!" notes all over the place.
So, C is a necessary evil if you want to understand or extend legacy systems whose authors preferred C. It’s also trendy in that people keep it mostly like it is while defending any problems it has. That’s a social thing. Like COBOL and PL/I are with the mainframe apps for companies using them. It’s not the best language for systems programming along a lot of metrics. One doing clean-slate work on a platform can use better languages to do better in the long-term. Short-term benefits matter, though, so such projects often compile to C or include C FFI to benefit from legacy stuff.
LISP is worth learning for a different reason — the profound enlightenment experience you will have when you finally get it. That experience will make you a better programmer for the rest of your days, even if you never actually use LISP itself a lot.
http://www.catb.org/~esr/faqs/hacker-howto.html
Functional language? Check. Stateless? Check. Reentrant? Check. Has a compiler which can generate machine code ELF binary executables? Check.
That's not to say one shouldn't learn C - after all, C is the contemporary portable assembler. But if one should also learn C, one should learn assembler too, preferably on a processor with an orthogonal instruction set architecture, like a Motorola 68000. Understanding both, we come back to the author's point - without understanding how the machine works, fast, small, efficient and elegant software will remain forever elusive.
The C philosophy "the programmer knows what they're doing" does not necessarily scale. It works when your team is strong or well supervised, but if that is not the case due to location, budget or whatever reason, unless you can afford a static analysis tool like Klocwork that can weed out mistakes while not sacrificing productivity, by going low level you have picked a hard battle.
C gives you a lot of power, but also a lot of rope to hang yourself with. I don't even want to imagine what kind of apocalypse I would see if you give multithreading and memory management powers to the caliber of people I've seen recently.
What many wrongly call the C ABI is actually the OS ABI.
Their OSes didn't had a C ABI, rather an ESPOL, NEWP, Modula, and so forth ABI.
Can you please elaborate on it please?
I wonder what if Kernighan, Ritchie et all decided to write UNIX in Pascal, not in C.
Hmmm.... :D
C is vastly preferable to writing assembler on multiple platforms, though.
The real tragedy is IT Managment(TM), which insisted on C++, and after that inevitably failed (for most things outside of video games), insisted on Java due to all the core dumping craziness.
Can't find the original source doc, but here goes: https://blog.acolyer.org/2016/09/07/the-emperors-old-clothes...
At least K&R took quicksort(), even if they didn't take the array subscript bounds checking part (etc) to heart.
Algol had it right, why screw it up?
I guess it doesn't matter if your (optional) type is word^H^H^H^H int, but this has descended into chaos in Java land.
Pascal was designed as a teaching language, then people realized it sucked as a language in the real world, so they extended it. There were quite a few Pascal variants in the late 70s / early 80s, and none of the useful code you could write in them was portable.
C had all the features you needed in an OS-level language, without resorting to variants (the X86 near/far bullshit notwithstanding, you used macros and typedefs to shift that stuff around). So Pascal more or less died in the industry.
I was at Apple when they converted from a Pascal shop to a C shop. There wasn't any big managerial push, just individual engineers or small teams realizing that the nightmare was over and they could use a language that sucked a lot less.
Since UNIX was written in C, academists/students began to learn C, and that was how the rise of C, now known as "the lingua franca of computing", started.
Personally, one valid reason for me to learn C today is to understand/hack stuffs written in it. A fine example is the Linux kernel.
I still don't get the "C is close to machine" mantra. If you want to understand the machine, why don't just learn assembly instead? :D
They're not hard things to do in assembly, but you'll need to write those every single time you need one. So then you start making macros to handle this for you. By that point the conceptual leap to C isn't very far.
it's almost an ideal hipster language.
Both items have diminished considerably lately, though, and that's something I can appreciate.
I'm surprised (and sorry) to hear that. The Rust community tries very hard to avoid language zealotry. If you see it on any of the Rust forums (can't speak for other sites), please report it.
I've seen lots of well-reasoned complaining about UB and other issues that C/++ are prone to, but not much juvenile hatred.
By now juvenile is something I surely am not.
I feel that both the OP and Dijkstra are right in their own respective way.
I do hope that Rust, for one, will turn out to be part of a path to better practise.
The OO nature of Python can also get in the way of certain things.
As mentioned in the article it is still a good way to learn about computers and it's the true "lingua franca" of our times - that said it needs to be put to rest for infrastructure software now.
I for one welcome our new Rust writing overlords.
Well, guess what? Most of the 'C' code I've written in the last ten years pretty much reflects the selfsame reality - next to nothing is done directly in 'C' but rather as posts of "transactions" to an interface. These may or may not be in text form but it's not much work to make 'em be in text form.
By the way - 'C' is excellent for text manipulation, even using the crufty old library calls.
If you do this and you do it carefully, all the horrors of 'C' become at least manageable. Done carefully enough, you don't even need all the new "safety" furniture.
I blame nobody for not wanting to do this, but can we at least self-identify as "will" or "won't" and behave accordingly? 'C' is no bugbear and your favorite interpreter can't save you from a bugbear that doesn't exist. When problems occur, it's because people didn't take the fifteen minutes or fifteen seconds to prevent them.
I just find myself gobsmacked that people feel so utterly powerless against the language.
I really hate the "C is how a computer really works" argument in favour of learning C.
We still start with the Bohr model in chemistry for the same reasons.
C is trivially easy to learn if you know Assembly Language plus any other imperative language.
My school taught assembly language as a full semester course (along with other low level concepts). That and an intro to computers course that used either Pascal or Fortran were requirements for the 200/300 level courses on operating systems etc. that used C but didn't actually teach it. They made seminars available for those who wanted to be taught C rather than having to learn it themselves. I believe that this is the right approach.
Of course, soon after I went through they fell victim to the hype wagon and switched to Java.
> Most engineers should take head of reason three, although our students also learn computer architecture and at least one assembly language, so have a firm mental model of how computers actually compute.
It is also useful in order to show how those "smart" things in higher level languages work, using standard library as examples.
There is no silver bullet for everything, obviously, but for me the above combination is a match made in heaven. Lua is small and adds only the necessary tools to take care of higher abstrations in many different paradigms.
Even if these two are different languages, they are amazingly complementary. I feel that a good introductory CS course could simply teach in one semester C and the next Lua. By the end of the year students will be able to tackle a huge amount of problems, as well as learned a useful systems and scripting language.
I don't quite see the point of C++ though. Can someone explain me why we should continue to bow into it?
If you don't need the C-level sharp knife, then there are a lot of options -- and a lot of reasons to choose them over C or C++.
Personally, I value that C++ is multi-paradigm (I can use classes when they are a good fit, generics when they are a good fit, asm when it's a good fit, etc.), also: trivial interop with C and assembler, no GC, runs directly on the metal. These are reasons to use C++, but not necessarily reasons to "continue to bow into it."
Did I mention generics and metaprogramming? Even if you don't use these directly, their availability makes for flexible and powerful libraries. The STL container library is the obvious example, but modern C++ metaprogramming goes way beyond that.[0]
[0] For example, see Hana: CppCon 2015: https://www.youtube.com/watch?v=cg1wOINjV9U https://github.com/boostorg/hana
Sure there are things (i.e. virtual classes) which cause an runtime overhead, but even when doing the same thing in C you'd need similar logic to what the C++ compiler produces for you.
Thing is, this exists. It's AWS Lambda. It allows running of arbitrary executables in the sandbox: https://aws.amazon.com/blogs/compute/running-executables-in-...
Is C all that commonly used these days? I rather suspect that it's an unforced error to start a new project in C, and that many wise organisations are replacing buggy C software with slightly-less-buggy software in safer languages.
> C’s influence can be seen in many modern languages
True, but … is this actually that important in a learning situation? Knowing Java will help learning Go (or vice-versa) about as much as learning C would, I think.
> C helps you think like a computer
Like a computer, sure, just not like the computer actually running on one's desk or in a data centre. Modern computers really aren't anything like the C model.
> Most tools for writing software are written in C (or C++)
I suppose that's true, because lots of people have used C, and that really is a good reason to know enough to get around in it, but … maybe one should teach students to use better tools in better languages (Lisp & SmallTalk come to mind).
Seriously, in 2016 it's obvious that C is just wrong.
It most definitely is. Outside of the fact that existing huge projects are written in C (Linux, many compilers/toolchains, databases, etc), it remains the default language for embedded systems. Most smaller embedded device (think Cortex-M class) is running code written in C. This represents an enormous amount of projects spread over dozens of industries and use cases. Someday that may change but right now even C++ has failed to gain traction in these environments.
> Seriously, in 2016 it's obvious that C is just wrong.
This is not true. It all depends on your use case. C is the most well-understood language on the planet - a sort of defacto default. Practices, processes, and tools have been developed over decades to help mitigate problems, often in systems that run for years without SW updates. This can't be discounted.
Is it because it doesn't fit your problem domain? That doesn't mean it's wrong for everything.
Every language has a strong and weak point. Knowing how to use their strong and weak points to elegantly solve any problem is important.
No language is "wrong" or "right"
I think it's obvious, or should be: manual memory management and poor types.
There's no problem domain for which I'd ever want to write C again: not for OSes, not for word processors, not for shell utilities, not for mathematics, not for graphics.
C's strong point is that Unix assumes it.
> No language is "wrong" or "right"
Counterpoints: JavaScript; PHP. Quod erat demonstrandum.
The world of software development is a lot bigger than PCs running high-level operating systems.
This is arguable.
There are cases where automatic memory management is not a viable option, and other cases where it is a poor choice. Just one example: consider all the trouble people go to with off-heap allocation in Java in order to get reliable low-latency operation.
In certain enterprise applications C are still the best choice. In finance, where I work, C and C++ still reign supreme. Why? There's already a huge multitude of great libraries developed over decades that are mature and battle-tested. Sure, you can write bindings for Rust/Go/D/whatever. But that's a ton of work for minimal benefit.
Fact is, when you get on a plane the avionics software was likely written in C. NASA extensively uses C. The automotive industry extensively uses C. Oh, and a lot of this C is embedded so forget easy software updates.
I think a lot of the hate for C comes from the fact that it lets you do bad things without alerting you. C is a language that mandates solid engineering practices. A good C codebase can be elegant, correct, and fully testable. The language doesn't force this, however. But a nice, well written C library will stand the test of time.
The Bible is useful certainly not just for historical value, but for making you wise for salvation. The Bible is God's master plan for forgiving you, and restoring you to friendship with himself, through Jesus' suffering, death and resurrection explicitly on your behalf. "Believing" in Jesus is not believing whether he exists, or whether God exists. "Believing" in Jesus means not claiming to be without sin, but rather acknowledging your sin, and trusting Jesus' work on the cross alone to save you from it.
I say unfortunately because this gives precedence to the Medina verses, which are marked by cruelty and violence.
This is an odd argument. C has hardly changed or evolved since 1988, so there's not really a need for a new edition.
"Modern C" is a good book on some of the new features.
http://icube-icps.unistra.fr/img_auth.php/d/db/ModernC.pdf