Learning C by doing

Learning C by doing

This weekend I started to read the book "The C Programming Language", I wanted to learn C for a long time before, and finally I got time this weekend to start a mini project.


Before starting with C, I have to say that 15 years ago, during college, I learned C for the first time, but I haven't used it since. Then, I was a little bit bored with just doing frontend, which is my expertise and my current job position.


Besides that, in my free time, I used to create plugins for my Neovim configuration in Lua, or I just spent time on my homelab configuration (which is a lot of work).
This time, since I am done with my master's assignments and the workload is low right now, I just took a look at the books I have at home and saw the C one. Then I decided to start reading it.

What I have learned

As Brian and Dennis say in the book, it is not for people looking to learn how to code. The book implies you have experience with programming logic. By saying this, I'm not saying the book doesn't cover what a loop or an if statement is, but it is something you have to be familiar with.


Then, in the introduction, the book explains why they decided to write it and says that the best way to learn a programming language is by doing it, and I wholeheartedly agree with this. One of the things you have to do when you are learning something is to experiment to understand how everything works.


By learning C, I realized I have to learn about pointers, memory allocation, how to register variables, how to use a function between multiple files, this last is important, I was diging more about how C projects manage the architecture structure and I found that is most likely something every developer knows only by reading other projects, on C you don't follow structures like you do on React or Java, since is low level language you have to own everything.


Normally, when you are coding in high-level languages, you just think about the logic you have to implement within a function, without thinking about what a pointer is or how this is gonna break memory. Nowadays, frameworks are doing that for you.


Then I learned about ASCII, which, as a software engineer, I have to know, but I didn't remember the ASCII codes, which are important when you are doing C projects.

Why I choose the project

I was checking Reddit about what I should do to learn C. The book has some projects, but I wanted to learn by doing, which means I don't want to read the book and then do whatever it says to do. I wanted to experiment.


So I found a Reddit post that was saying that if you want to learn C, you have to replicate the following:

So I chose the first one, good to start, because by doing a replica of a cat command, I have to understand:

  1. whom to print each line of a file
  2. How to map every character in one line
  3. How to start an array of char, and what size should I use
  4. Structure a project to avoid having everything in one single file
  5. How I need to handle errors
  6. How do I need to print the help message
  7. How to define each flag and how to map each of the flags
  8. What happened when multiple flags were passed as arguments
  9. What happened in multiple files
  10. the buffer size of each file

Why I considered learn C

As I mentioned before, I just picked a book at home and decided to start learning C, but why C, not Rust, for example, that I also have some books about, well, I intend to start being efficient in C to start Rust projects.


I think that if I learn C before any other language, the quality of my code will improve over time, not only in the code I write manually, but also in the code I vibe-code with LLMs.


Understanding how the hardware and software work will liberate a world of implications that, if you just know one type of language, you are not aware of. With this, I think I will be able to detect bugs or introduce myself to a project more easily and quickly in the future.

Contribute

If you want to take a look at what I did or how the project is going, below is the link:

GitHub - CarGDev/mycat: New CAT project
New CAT project. Contribute to CarGDev/mycat development by creating an account on GitHub.