Can C be Used for Game Development And Is It a Good Idea?


Game Development is a challenging but also very fun task. Many games are developed in C++ and Game Engines like Unity or Unreal let you program in C# and Lua. Even Python has dedicated Game Development Libraries. But can you do it in pure C?

The C Programming Language can be used for Game Development. It is fast and provides great portability to a wide range of systems. As a general rule it is more complex to program a Game in C compared to other Programming Languages.

So should I use C for Game Development or better chose another Language? We will get into the pros and cons in the rest of this article.

Can C be Used for Game Development?

C can absolutely be used for Game Development and it has been used for it many times. You will find examples above all in the early 1990’s. Games like Sid Meier’s Civilization (Microprose, 1991) and Doom (id Software, 1993) come to mind. With the development of better compilers in the mid 1990’s however, the developers shifted more to C++.

Ingame Screenshot taken from Sid Meier's Civilization
Ingame Screenshot taken from Sid Meier’s Civilization (Microprose, 1991)

To this day, Libraries that are used for Game respectively Graphics Development are written in pure C. Some have C++ ports nevertheless. Examples are the OpenGL SDK and the Simple DirectMedia Layer (SDL).

C definitely has some advantages. It is fast because it is system-related and has some low level access. On the other hand it is also portable to a wide range of (operation) systems. Although created in the 1970’s it is still updated and extended. It is widely used and very well documented which gives you a lot of places to look for help if you have a problem.

Ingame Screenshot taken from Doom
Ingame Screenshot taken from Doom (id Software, 1993)

It can be more complex and cumbersome to program a Game in C.

But there is a reason why C is not heavily used for Game Development these days. The main reason is that C++ has many of the advantages of C but brings in many features that C does not provide. Object Orientation with classes, inheritance and polymorphism come to mind. Advanced Features like Templates and Smart Pointers (which provide some sort of Garbage Collection) are also not present in C.

Is it a Good Idea to Use C for Game Development?

If it is a good idea to use C for Game Development nowadays depends on the goals and expectations that you have. The last section made clear that it can be done but that there are more convenient options.

It takes a lot of effort to access all the hardware that is needed for Games like Graphics, Sound, Keyboards, etc. For the most part you would want to take libraries that allow you a more comfortable access to these peripherals by just calling some Interface Functions. (Fun Fact: A lot of these libraries are actually written in C).

The great advantage is that you can program in C++ and use the C libraries through its interfaces completely native in your code.

The decision also depends on the project you want to accomplish. If its a small indie game or you are just fiddling around, then C maybe excellent for you. If on the other hand you are working on the next Mass Effect or Uncharted then using C++ or a proprietary Game Engine like Unreal or CryEngine may be the better way to go.

Can You Make a Game Engine with C?

If you can make a Game with C you can of course make a Game Engine with C. It could be a great Idea to put the system-related code in your C Game Engine and provide Interfaces for other Languages like C# or Lua to access it in order to access the Features of your Engine. I covered the basics of a Game Engine in C in another article.

A Game Engine is a Framework that allows you to reuse parts of games like control, graphics and sound systems for more than one game. Once the programming of the Engine is finished you can use it for as many Games (of a certain type) as you like.

In fact it may be totally reasonable to write most parts or all of the Game Engine in C. This way you have system-related code that runs fast and uses memory as efficient as possible for the critical parts. Heavy physics calculation or rendering processes shouldn’t be written in Languages like Java or C# which run in Virtual Environments.

A Simple Game Written in C

At last here is a very simple example of a Game written in C. In order to keep this short there is no real Graphics involved other than output to the console. So don’t expect the next Doom or Halo here, but it’s a game nevertheless.

If you want to see it in action, copy and paste the code and run it with your favourite C compiler. Have Fun!

These two files contain all the game logic, input and output:

#pragma once

void game_setup();
void read_input();
void process_game();
void show_output();
int quit_requested();
#include "game.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

static int _quitGame = 0;
static int _number   = 0;
static int _guess    = 0; 
static char _input[2];

void game_setup() {
   time_t t;

    printf("Welcome to 'Guess My Number'\n");
    printf("I will think of a Number between 0 and 99 and you will have to guess it.\n");
    printf("You can quit the game at any time by typing q or Q.\n\n");
    printf("Guess My Number: ");
    
    srand((unsigned) time(&t));
    _number = rand() % (99 + 1 - 0) + 0;
}

void read_input() {
    if(_quitGame) return;
    scanf("%2s", _input);
    if(*_input == 'q' || *_input == 'Q') {
        _quitGame = 1;
    }
}

void process_game() {
    _guess = atoi(_input);
}

void show_output() {
    if(_guess < _number) {
        printf("Wrong! My number is higher.\n");
        printf("Guess My Number: ");
    } else if(_guess > _number) {
        printf("Wrong! My number is lower.\n");
        printf("Guess My Number: ");
    }
    else {
        printf("Right! You Won the Game!!\n");
        _quitGame = 1;
    }
}

int quit_requested() {
    return _quitGame;
}

This is the main file that calls the game relevant functions and provides the game loop:

#include "game.h"

int main() {
    int isRunning = 1;
    game_setup();
    
    while(isRunning) {
        read_input();
        if(quit_requested()) {
            isRunning = 0;
        } else {
            process_game();
            show_output();
        }
    }    
    return 0;
}

I admit that this is a rather simple game that won’t make fun for a long time, but feel free to extend it to something that actually is fun. Start, for example, with a maximum number of tries before you lose. You can do it, even with C 😉

Marco Lieblang

Professional Programmer since 2003, passionate Programmer since the mid 90's. Developing in many languages from C/C++ to Java, C#, Python and some more. And I also may know a bit about Assembly Languages and Retro Systems.

Recent Posts