Comparison: C vs. C++ for Game Development


Many people choose C++ almost immediately when they start developing games, and often with good reason. But wouldn’t good, old, plain C do the trick for this job?

Taken as a whole, both C and C++ can be used for game development. With plain C, however, you have to do without advanced features such as object orientation, generics and templates.

Let’s take a deeper dive into the similarities and differences between C and C++ in the context of game development.

C vs. C++

Comparison between C and C++ for Game Development

In order for us to understand what differences there are between C and C++ in game development, we first have to understand what differences there are in general between the two programming languages.

C is a pure structural or procedural (aka imperative) language where C++ is mainly object oriented. Because C++ is essentially a superset of C it also can be used as procedural language. Thanks to the extended features, C++ can also be used for other paradigms such as functional programming.

C is good for low level applications like drivers, kernels, etc. It comes close to the performance of assembler and lets a project meet industry guidelines better because they are good to prove and test in the C Language.

On the other hand, C++ is also still very low level and the marginal performance disadvantages compared to C are outweighed by the extended features. C++ makes it easier to write reusable code and, unlike C, allows you to encapsulate, overload, and more.

Example: Overloading in C++

In C++ you have the possibility to overload functions which gives the opportunity to use the same signature with different parameters:

void my_function(const int& firstParam);                             // Declaration
void my_function(const int& firstParam, const char& secondParam);    // First Overloading 
void my_function(const char& secondParam);                           // Second Overloading

int main()
{
    my_function(7);
    my_function(7, 'a');
    my_function('a');

    return 0; 
}

The same code in C would need three different function declarations:

void my_function_i(const int& firstParam);                             /* First Declaration */
void my_function_ic(const int& firstParam, const char& secondParam);    /* Second Declaration */
void my_function_c(const char& secondParam);                           /* Third Declaration */

int main()
{
    my_function_i(7);
    my_function_ic(7, 'a');
    my_function_c('a');

    return 0; 
}

Reasons to use C over C++ in Game Development

So if most of the points are in favor of C++ and the game is not so low level that C is absolutely necessary, why should you still develop your game in C?

Many libraries are written in C, like the SDL for example, and using these with C++ requires the use of the C subset of the language anyway.

There is also a direction in game development that is moving away from encapsulation and object orientation towards more data-driven development. Performance is particularly important when it comes to games, and organizing data in a way that the processor processes it best can bring about decisive improvements.

As part of this development, so-called Entity Component Systems (ECS) are increasingly being used. But if data and functions are separated, their encapsulation is also prohibited. This makes the object-oriented approach obsolete and thus robs C++ of one of its strengths over C.

The Switch from C to C++ in the 1990s Game Development

There were several reasons that many Studios and Developers switched from C to C++ in the mid 1990s.

First, C++ became faster and the compilers became better. In the early 1990s compiling in C++ was slow and sluggish, but this eventually wasn’t the case anymore.

Then Java became popular and the whole Object Oriented Programming paradigm was more widely accepted and used.

Also there was no C99 Standard yet and therefore C Developers were restricted to the old C89 with the 90 and 94 updates of the language. This had a few disadvantages as the following example demonstrates:

/* C89 with updates C90, C94 */
int main()
{
    /* Only Multiline Comments */
    int i; /* for loop needs variable declaration outside of loop */
    FILE* file; /* all variables have to be declared at top of scope */

    for(i=0;i<10;i++){
        /* do something */
    }

    file = fopen();
}

// C99 
int main()
{
    // Inline Comments
    for(int i=0;i<10;i++) {   // conter variable declaration in loop
        // do something
    }
    FILE *file = fopen();  // cariables are only declared where they are needed
}

Also a lot of developers came from University where they learned how to program in C++ and Java and brought this to the companies that hired them.

C Game Development Engines

There are some Game Development Engines that use exclusively C. The most famous would be the id Tech Engine that went through several iterations and was used for Games like Doom and Quake. I summarized those iterations in another article about writing a Game Engine in C.

Other notable Game Engines in C are Allegro and Build EngineOpens in a new tab., but both are not used for so called Triple A Games to this day. You should still take a look at them, if only for the sake of learning and demonstration.

C++ Game Development Engines

If you are looking for a C++ game engine, there are almost as many as a dime a dozen. Today, however, three of them stick out because they are used by many developers around the world for projects of various sizes, from Triple A to Indie and back.

The three most well known Engines are:

There are also propreritary engines like FrostbiteOpens in a new tab. from E.A and lots of Open Source Engines, like OGREOpens in a new tab. (graphics only) or GodotOpens in a new tab..

Summary

There are good reasons to program in C++ instead of C if you want to develop a game. However, as an individual developer with a smaller project, or as a small team that wants to pursue the data-driven approach, it is worth taking a look at C and programming a game in this language.

You could start programming your graphics in C with the help of the SDL library. If you want to load Shaders for your OpenGL application you can also do it in plain C. And playing a .wav-File in C also isn’t that hard. Each link above brings you to an article about the topic where I show you how it is done.

Now you can consider using C in your next game project.

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