Blog‎ > ‎

Fun with C - III

posted 8 Jun 2012, 09:50 by olnf Admin   [ updated 8 Jun 2012, 09:52 ]
Author: Sudeep Jaiswal

Most of you probably remember the ‘Matrix’ films. Right now, I have almost no recollection of them but I do remember the falling characters/numbers sequence. It looked really good. I got to thinking, and I decided that I could make something that at least resembled it in C. If you don’t know what I’m talking about, click here to see a picture. Looking at more pictures, I had a clearer idea of what to do. It was not possible to match the actual characters, but I wanted to make something that resembled it in general.


C has two modes, text and graphics modes. I wrote the program in text mode, which has a size of 50×80 characters on my monitor. I made an array of characters of the size 50×80, intending to fill it using random characters, so that once printed, it would occupy the entire screen. I created two functions, one to generate the characters and fill the array and the second to print it.

Since filling every position in the array with a character would just fill the screen, I decided that I needed a lot of spaces. So I filled every position in an odd numbered column with spaces. Array counting in C starts with zero, so the second column would be the first. You can make this out in the images below, every alternate column is filled with spaces. Now, every character/number/special symbol on the keyboard has its unique ASCII value/code. If you don’t know what that is, it’s not important. Understand this. The number ‘1’ has a special ASCII code, the letter ‘a’ in small and upper cases both have their own unique codes, and so on. ‘Space’ has the ASCII code 32 and there are 256 such codes in all. Randomly filling the array with 256 characters meant that there were hardly any spaces and the screen still looked very constricted. Also, things like ‘enter’, ‘backspace’ and even ‘beep’ have their own ASCII codes, so it was messing with the look (and sound) that I had in mind.

Looking at the ASCII table, I came to know that all those problematic things came before ‘space’, so I decided that if the random character that was generated had the ASCII code less than 32, it would get treated as 32, i.e.: a space. Though, I still thought the screen looked constricted. I realized that out of 256 characters, those generated with an ASCII code less than 32 might also be comparatively fewer, so I wrote in a simple logic that put in more spaces and I was done. Finally, I made the sequence green. Now, I am printing the array again and again till the user presses some key. That's the reason the characters appear to be moving upwards. As new characters start getting printed, the old ones go away, one by one. Also, the first time the array is printed on the screen, it's printed in black colour, that's why it seems the green characters are starting to come up when the program is started.


And lastly, the download. I came to know that DOS applications are not working properly on different operating systems. I use Windows XP (32 bit). So this time, I have included a video, apart from the code and ‘exe’. There is also a shortcut to the ‘exe’, which was generated automatically when I selected the option of running the program in full screen. I found that if I delete the shortcut, full screen goes away, and I can’t run the program through the shortcut.

Download: (747 kb)

PS: Does anyone know how I can make this a screensaver? And I know the sequence in ‘The Matrix’ goes from top to bottom.