C Fundamentals

Why C is Popular

C Fundamentals

Lesson Description

The "Why C is Popular" Lesson is part of the full, C Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Richard describes what makes C so popular. The language provides the maximum possible performance while giving developers better ergonomics than assembly-style languages. It's also simpler to learn than other C-based languages like C++. Some alternatives to C are also discussed in this lesson.

Preview
Close

Transcript from the "Why C is Popular" Lesson

[00:00:00]
>> Richard Feldman: So why is C still popular? So, I mean, the main number one reason is just, it gives you access to the maximum possible performance. This is really because it's really zero-overhead programming. It's the ability to get down as close to the metal as the operating system will let you, just shy of micromanaging registers and Assembly.

[00:00:20]
And not because it does amazing optimizations, although it does lots of those two. It is much more ergonomic than Assembly, if you've ever tried writing an Assembly program by hand, I don't recommend it [LAUGH]. It's an exercise in, well, it's a learning experience, I'll say that. And of course, although C++ was designed to be in many ways an improvement over C++.

[00:00:42]
C++ is extraordinarily complicated and the compile times are massively longer than they are for C. So, C is still popular even in a world where C++ exists, even though C++ was designed to be the improvement over C. Because C++ added so much complexity and so much longer compile times that a lot of people are like, yeah, I know C and C++ and I prefer C.

[00:01:00]
One really direct example of what C gets you in terms of performance is what I'm gonna call the Local Static HTTP Server Perf Olympics. This is not a real competition. This is very unscientific, and it was done by me very quickly by measuring some different static HTTP servers and just loading up a download of the FrontendMaster's website.

[00:01:19]
And seeing how fast they were able to load it into the browser. Very unscientific, but also a little bit of fun. So in third place with the bronze, we had Node.js. So this is Node's HTTP server package. This has about 2.3 million weekly downloads, it's extremely popular. It's the most popular way to do a static HTTP server in the Node.js ecosystem.

[00:01:39]
In second place with the silver medal, we have Rust, Rust's most popular alternative is simple HTTP server. Rust's much smaller ecosystem than Node.js, only 52,000 all-time downloads, let alone weekly. And then in first place with the gold medal, the fastest local HTTP server among the ones that I tried is written in C.

[00:01:57]
And it is the about 300 lines of C code we write in this workshop with only the standard library and no third party dependencies whatsoever. So this is kind of an example of why C is still popular. I mean, granted, we're not talking about blisteringly [LAUGH] fast differences here.

[00:02:12]
It's like, all of these are about under ten milliseconds of response time to like load all the different assets statically. But nevertheless, it is the case that pretty consistently, you can see that even though we're not really like, I didn't sit down and benchmark this, other than this silly little performance Olympics thing.

[00:02:28]
I didn't sit down and performance optimize the C thing. These are very mature, well written, well established, designed to be high performance HTTP servers written in Rust. Which has a reputation for Being a very fast language, and Node js, which, granted, has a lot more overhead than rust or C.

[00:02:44]
And still, just this example that I just made for this workshop, just by doing some basic bread and butter, just do things the way that kind of C encourages. We're able to get really, really fast performance just because C is just really, really not adding anything, even compared to Rust, yeah.

[00:02:59]
>> Speaker 2: Is it Node relying on C++ anyway as well to do that?
>> Richard Feldman: Yeah, so Node, under the hood, certainly the Node runtime is gonna be V8, and V8 is written in C++. It's multiple millions of lines [LAUGH] of C++ code, and billions, it takes forever. But yes, so when you're running your Node HTTP server, you are definitely executing a lot of code that was written in C++, nearly the JavaScript Virtual Machine, V8.

[00:03:24]
Yeah, [LAUGH] and nevertheless, [LAUGH] here we are. Cool, I do wanna mention there are some low overhead C alternatives. We're not gonna get into them in this course, but I do wanna at least mention them. So C++, of course, is like definitely the most popular of these, very, very complex.

[00:03:41]
It's absolutely huge, especially in the game dev space, but not just in game dev. But if you wanna be a professional game developer, there is an extremely high percentage chance that you're either going to be writing C++ or at least building on a large C++ code base. Rust, definitely a very popular alternative that's increasing in popularity a lot.

[00:03:58]
Nowhere near C++ levels of popularity, but Zed, like the code editor that I work on at work, Biome, which is the successor to Roam written in Rust. That's like a JavaScript ecosystem thing. Ripgrep, really popular, like grep alternative at the command line. I have a course on FrontendMasters about that if you wanna learn how to do Rust.

[00:04:18]
Zig, also very nice language, you might go see the terminal emulator bond in the JavaScript ecosystem, you might've heard of tiger beetle rock. The programming language I'm making is in the process as we speak of rewriting our compiler from rust to Zig. And there's plenty of other, this is like D, Odin, JAI, Carbon, lots of different languages are trying to say, hey, C has so many nice properties, but the ergonomics, there's just some problems with it.

[00:04:43]
It doesn't feel like a modern language because it's like half a century old. And people are trying to modernize it, hopefully without sacrificing the things that make it popular in the first place. Okay, all of these low overhead C alternatives definitely support C interop. So, not just languages that are trying to be C alternatives, but also like we said, Node.js and others, really, C interop is almost universal, because C is like the language that other languages use to talk to each other.

[00:05:09]
All things considered, I would say that, today, in this day and age, C is the most universal language that you can know. If you only wanted to learn one programming language and you wanted to know one that will give you the ability to write code and have it interact with every other programming language out there, C is that one language.

[00:05:25]
And today, we're gonna learn it. I also wanted to mention very briefly, so you might see in the exercises we talk about, it says, hey, please use either MacOS or Linux, or if you're on Windows, use Windows Subsystem for Linux. I wanted to just briefly mention why we're doing it that way.

[00:05:41]
Essentially, we are using in this course some OS-specific APIs, and Windows basically just has a totally different set of APIs for things, like MacOS and Linux and BSD, BSD, sorry, MacOS being a flavor of BSD. These are all different UNIX derivatives and they all use POSIX, which is sort of the standard for UNIX APIs.

[00:06:00]
Which means that, we can have the code in the exercises work across all of these. If we wanted the code in these exercises to work across Windows, I would have to do two separate versions of every exercise and explain the differences between every single one, and it would just make everything take way longer.

[00:06:14]
Fortunately, Windows subsystem for Linux also supports these POSIX APIs because it's just a Linux build. So, basically, I was like, okay, the windows specific APIs which I have used are often very, very similar. So if you go through this course and learn all this stuff, and then you're like, but I want to do dedicated Windows programming, maybe for game development or something like that.

[00:06:32]
And I wanna learn the Windows APIs, I think you'll find that the names are different, the arguments are slightly different, but fundamentally, the concepts here are gonna translate very, very well to Windows. It's just gonna be the names and stuff are just a little bit different. So you're not missing out on a lot because we're not doing it in Windows, but mainly it's gonna be a big time savings.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now