Recently, I have added a programming assignment to my college students’ Game Design & Development courses. I require the assignment as a way to simply introduce them to the C-language; but more for the logical thought process that the ensuing coding exercises are meant to accomplish. This is useful because the majority of my students have used a program called Game Maker, which implements a similar language style.
As such, I felt that it was important to put some of the information in writing. Although it is generally good to discuss topics with students during class, this particular topic is not nearly as condusive for comprehension as others because it requires practice on a computer instead of just general knowledge in order to get a program to work.
The Makers
C is a very small, but extremely powerful language. It was co-created by Brian Kernighan and Dennis Ritchie and if you are interested in purchasing a book specifically on this topic, I strongly recommend picking up the book sold here. If you know everything, inside and out, from this book, then you can consider yourself to know the entire language. Whether or not you ever get to the point of mastery depends on how you decide to use the language.
Applied Gaming
Nowadays, C++ seems to be the rage, but C is still very useful; especially on gaming consoles. Let’s take a quick look at a history of the Nintendo Gameboy handheld…
Handheld Console | Primary Language |
Gameboy | Assembly |
Gameboy Color | C |
Gameboy Advance | C |
Gameboy Dual Screen | C++ |
Although C++ seems to be the choice for the DS that is to come out next month, C is still a very viable alternative for many. Why do I say this? Well, really C is the predecessor to C++ and can actually do everything that C++ does for you automatically. For instance, when allocating memory in C, a programmer might call a function like “malloc()”, then check the contents of that new memory space manually; whereas using C++, that same programmer would simply call “new()” to get the same end result.
So, C requires more function calls to achieve one particular operation. But it actually will likely end up taking less processing power and/or less space because C++ is considered by many to be “bloated” for reasons that can be discussed later. What you want to realize is that a gaming console is generally very limited in ways of speed and memory; in order to accomodate it’s limitations, consideration should be given to which programming language to use.
How’s This Made Exactly?
When you double-click on an icon sitting on your desktop, you run what is called an EXE (executable) file. This file is generated from your code; but to do so, your code goes through several iterations…
From | Operation | To |
High-Level Code (C/C++) | Translation | Assembly (ASM) |
Assembly (ASM) | Compilation | Intermediary Files (OBJ) |
Intermediary Files (OBJ) | Linkage | Executable (EXE) |
Essentially, you write code using a programming language (in this case, C). Depending on your development environment, that code may be translated in-part or in-full to assembly language code. This code is then compiled into a specific intermediary format for whichever platform you are developing. From here, the object files are turned into human-illegible machine code (a.k.a. an EXE file).
The Environment
In order to create EXEs using the C language, you need to acquire a compiler. Although, it is termed a compiler, these tools also implicitly perform the linkage at a bare minimum…
For this article, we shall be using the Microsoft Visual C++ Integrated Development Environment, or MSVC IDE for short. Although this product is the most expensive from those listed, it is also the most widely used for PC titles in the gaming industry because it contains a plethora of features and plug-ins that the others simply do not have. Besides, the translation, compilation, and linkage are all done with a click of a single button instead of having to use a command-line interface (CLI).
Hello Who?
In most textbooks, you may find that your first program is referred to as “Hello World” or “Hello C”, for instance. Here, we will refer to it as “Hello Who?”. The whole point of this exercise is simply meant to familiarize yourself with a basic C-language program. First, open your compiler and start with a blank C file. From here, type in the following:
[sourcecode language=”cpp”]
#include <stdio.h>
void main()
{
printf( “Hello Dan!” );
}
[/sourcecode]
When you build (the process of translation, compilation, and linkage) and run the resulting EXE, you should see this:
Hello Dan!
Congratulations, you’ve made your first C program!
Let’s go through this line by line. The first line is the #include (say ‘pound include’). Inside of the angle brackets is a file name that end in H. This is termed a header file. Somewhere on your computer, when you installed your compiler, this file was copied. This is one of several standard header files that come with all C compilers.
This particular file stands for “standard input/output” and it contains several functions; one of which is the printf() function. Functions are pieces of code that you can use to perform a task. In this case, printf() outputs to your screen a sentence (or string).
We’ve covered two of the three areas in this program. Now let’s touch on “void main()”. This is actually a function definition. You wrote a function just like printf()! We will go more into depth on the options that you have in a later tutorial, but for now you should just know that this is the function that the computer calls when you double-click on an icon to run an EXE.
If you are interested in learning more about C, I highly recommend the K&R book mentioned in the beginning of this article. For many programmers, it is a great reference because the entire C language is compacted in just a small subset of pages in the back. Compared with C++, that’s just awesome and a great way to start off your knowledge of programming because everything that you learn using C can be directly applied to all of the new constructs in C++.