Senin, 02 Juni 2025

C Programming from a High-Level Mindset

| Senin, 02 Juni 2025

Just finished up with Part 2 from Dr. Chuck's "C Learning For Everybody" and wanted to take a second to reflect. If you're interested about going to a low level language from a high level one, this article will give you some insight into some of things you might expect to learn. At the bare minimum, you'll come away with some great good for thought that'll continue to fuel your programming journey. Here are 8 insights I had throughout the past month and a half:
 

Base conversions in C

C, or at least some of the programming problems introduced in this course, will make you think about math (and really our entire world) differently. Writing a program that converts numbers between different bases (decimal, hex, octal, and binary) might very well throw your mind for a twist (it did mine). Converting between different bases manually is pretty straightforward, but writing a logical program get's a little tricky! When beginning to work with base 8 and 16, I honestly felt like I was working with an alien language. In a parallel hex-based universe, there's a $64 bill with Benjamin Franklin on the cover of it.

Using the -S flag

When compiling C code, you can add the -S flag to generate the assembly code. The output will vary depending on the CPU architecture you're targeting.

Understanding interpreters

I’ve heard it a million times, the difference between a compiled and interpreted language, but like anything in programming, you don’t really understand until you actually do it. You probably understand it conceptually: compiled languages do all the heavy lifting up front, while interpreted ones figure it out as they go, reading line by line. The lightbulb moment for me came when I wrote a function in C (not a real interpreter, but close enough) that read from stdin using fgets(), parsed each line with sscanf(), and executed operations based on that input. I essentially created a mini fake language and for the first time I could visualize how a language like C might go about reading and processing text input the same way an interpreter might handle JavaScript. If you understand compilation and interpretation at a high level, creating a pseudo-interpreter will give you a great low-level understanding of what's actually going on!
 

ASCII awareness

C really makes you understand ASCII. If you're a programmer, you've heard of it, but once you start writing C code, it becomes something you actually think about. You start to think like a computer. Letters aren’t just letters anymore - they’re numbers. For example, in C, the character 'W' isn’t just “W," it’s actually the number 87 in memory (its ASCII value). In C#, Java, Python, JavaScript, and other high level languages, you mostly think about characters by what they represent, not by how they’re stored. But in C, knowing what that 'W' really is under the hood becomes part of the way you write and reason about code. It’s definitely a shift in mindset when coming from a higher-level language.
 

No classes in C

No classes in C. If you're coming from C# or Python, this is going to be a bit of a system shock. If you want to group related data in C, you’ve got to use a struct. In C, data and behavior live in separate worlds: functions go in libraries, structs hold your data, and you manually keep it all wired together. In a typically OOP language, both the data and methods would be stored inside a class. I'll be learning more about this in the upcoming sections, so I'll keep you updated and hopefully provide some insight that will allow you to better differentiate and work between classes and structs.
 

Good documentation

This isn't really about C, but more about the documentation in the files of the C code written for the ESP-IDF that I came across. Maybe it's out there and I just haven't encountered it, but I really haven't seen anything as detailed. This isn't documentation on the logic but rather documentation on what different variables mean in plain English. I've been in many codebases where I look at the fields of data and have to jump through hoops to figure out what the hell their purpose is. Even in Microsoft .NET base libraries, the documentation of what the data fields represent isn't all that great. Regardless, the ESP-IDF in-code documentation was so good that I had to give it a shoutout. More codebase field declarations should be like this:

ESP-IDF Documentation
 

What a virtual machine really is

This might sound obvious, but a virtual machine isn't a machine at all. I feel like that needs to be said because computer science loves giving things names that sound way more physical than they actually are. A virtual machine is just a program, usually written in something like C, that simulates an entire computer. It's a fake computer running real programs on a real computer. Languages like C#, Java, and JavaScript all run on these virtual setups. C# uses the CLR. Java uses the JVM. JavaScript depends on the browser. Chrome uses V8. Firefox has SpiderMonkey. All of these are written in C and just sit there executing code under the hood. It’s easy to get caught up in all the “oohs” and “ahhs” of how clever everything sounds, but at the end of the day, it’s all just files and programs turning into processes. Nothing magical. Just code pretending to be a machine, running on an actual one. C is the backbone of it all.
 

Wrote a recursive function

Last but not least, I wrote my first recursive C function without any help from ChatGPT. While cool and all, recursion should rarely be used and, as stated by Dr. Chuck. It's really only necessary when dealing with problems that require some sort of tree.
 
Above are some highlights and realizations I made and hopefully they give you some insight into Dr. Chuck's course or just some of the revelations that you might encounter if you're coming from a high level language down to C. So far, I've completed up to chapter 4 (out of 8) in the original book. However, as stated by Dr. Chuck, things start to get powerful (and painful) at Chapter 5. I asked ChatGPT how much of C's power I've already gained to which it said that I've harnessed about 30-40% of C's power, but 60-70% of the "juicy" stuff is still left ahead. "The party is just getting started," ChatGPT said, "and you're at the front door. Chapter 5 is when the lights go down and the DJ shows up."


Related Posts

Tidak ada komentar:

Posting Komentar