Abhi.

C in 2 Weeks

2025-08-14

For some time i was having a feeling that i am not enjoying programming as much i used to. When I began web development. I found HTML fun and easy, CSS was confusing at first and JS was frustrating beyond the basic. But there were those "click" moments that made it really fun. I created several projects using HTML,CSS and JS. Then i learnt React, Next, Node js and other web technologies. It was addicting at that time and i could go a full day just programming. But after some projects, it had became a little dull to me because creating website were not really a big deal. I didn't have any challenges that I used to have before.

So i decided that i want to learn something new. It was confusing at first, as there were so many areas to learn from. After getting some suggestion from internet, i decided to learn a fundamental language that has inspired most of the languages of today and used in most of the underlying tools, C language. And so i decided to challenge myself to learn C in 2 weeks.

C in 2 weeks

The syntax was itself a breeze since C syntax are very similar to that of JS. For the first 2 days i learnt about basics of C like structures, memory allocation, pointers, pointer arithmetic etc. Now i needed to implement these in a real project. For that I followed The kilo editor tutorial( again as per internet suggestion) which built a text editor in under 1000 lines. There were 7 chapter in total. Some of these chapters I completed in a day,some i took 2 days. But i made sure that i understood every individual code he wrote. I learnt many things from following this tutorial about working with the language itself like the file descriptors, bit masking, ANSI escape sequence, enums etc. But after following the tutorial for 2 weeks. I didn't understand the project as a whole even though i made sure to understand the code individually. Then i decided to create a simple basic program so that I get better grasp of the language and so began my journey to write a game of C in worst way possible.

Making RAM hungry snake game

Since i had little to no experience in writing program in C except from the Kilo editor itself. My mind only thought of doing this snake game in the way that antirez had implemented the kilo text editor and that made the game complex than it should have been.

I started with a appendBuff function just like in kilo text editor that would store the snake body, head , food and other escape sequence and write it all at once with write() function. I implemented the snake's turning by adding breakpoints to each individual body which would record all the coordinates where the head turned and turn exactly after reaching that point.

This made the game more complex than it needed to be. After running the game through valgrind and even after fixing all the memory leak issue that game had, the valgrind report gave total heap usage of 494,648,906 bytes allocated. A snake game having about 500 mb of total RAM usage was surely not a good sign. After reading some reddit responses, it was pretty clear to me that this kind of memory usage for such small program is really really really bad and a snake game didn't even need dynamic memory allocation. Then after reviewing my code, I found the main culprit of my memory usage was appendBuff which was called with every loop and multiple times under same loop which allocated large memory in the heap.

In reality, a snake game doesn't need to have a dynamically growing buffer like a text editor does, a snake game has fix area from which it won't ever grow. That was my mistake. Instead of creating a array that would store the snake game grid in 2 dimensional array, i was using a pointer that would start with size 0, then allocate memory for every row and column, everytime the loop ran so the memory usage was really high.

I initially thought i would create a game within few hours but surprise it took me like 2 days to create the game itself(with all those memory and segmentation fault error i kept running into) and another day to fix the memory leaks and high memory usage.


Nevertheless it was a learning experience to me on what not to do and learn when and how to manage memory in C. Most importantly it was fun making such mistakes, reviewing my code, getting frustrated because of my inability to pinpoint the problem and then solving it. That was the fun in programming that was missing. I guess i will stick to C for a while.



If you want to look at the source code. Here are the links:


Old Source Code: https://github.com/AbhishekTimalsina/snake/blob/master/v1Snake.c
New Source Code: https://github.com/AbhishekTimalsina/snake/blob/master/snake.c