So I dabble with programming, and I know some C++, C# and a little about java/javascript and HTML, and while searching around I frequently found people that say that "real game programmers" use C++ for game programming. However from what I gather even if that seems to be true, there are a lot of other programming languages, perhaps even more suitable for game programming.
So then I happened upon somewhat of a dumb but meaningful question:
Why C++?
What makes C++ the favorite/mainstream language in game programming, when other languages such as C# or javascript are easier? Is it just because C++ has been around longer? Does it offer more control over some aspects of data-storage and such? Does it simply have more support?
Thank you in advance for answering.
Languages have "levels" which imply their "distance" from the hardware.
Programming in Assembly for example, you'd be really close to machine code, messing with how the hardware really works, so there's a lot more work to be done.
Higher level languages such as C# or Java have gone through tons of layers of code already, simplifying everything.
For example, creating a string in C# would be as simple as:
string myString = "someString";
In C, without the help of any libraries, you'd have to deal with an array of single characters:
char myString[20];
myString[0] = 's';
myString[1] = 'o';
myString[2] = 'm';
myString[3] = 'e';
//etc
(There's easier ways to initialize the variable, but the work is basically the above code)
So, the higher the language level, simpler the code management. But exactly because of this, you lose some "control" of what really happens.
For example videogames. Unlike desktop PCs, they have specific hardware set: Memory X, CPU Y.
When programming a game for a videogame you have to be aware of how much memory and CPU you have available and work with it.
In Javascript, you create variables, use them and that's it.
In C# and Java too, because they have something called "garbage collectors".
So what would happen if you keep constantly creating more and more variables and you reach the limit memory available? Crash. No more space for variables means no more variables, so expect something to crash.
In C++ and below, you can control when to delete a variable from memory, so you can make sure you'll have enough memory to continue your program.
"Garbage Collectors" in Java/C# free the memory when they detect that a variable isn't being used anymore. How do they detect it? This means there's another program running besides it checking for all variables you're creating and constantly checking them to see if they're okay to delete. This program is obviously consuming resources (CPU and Memory) as well, so that's why these languages are considered "heavier" on performance.
So, in general, the higher the language level, the heavier it is on performance because of all the things it is already doing to simplify the programmer's work.
When you want games to be as fast as possible, pushing the hardware to the limits, you don't want anything else doing (unnecessary) work for you.
Another thing about languages is that, the closer it is to the hardware (so the lower it's level), the easier it is to port to different machines.
All machines work in the same way in the lowest level possible: machine code.
The higher the level of the language, more layers it has been gone through, so more limitations it has.
For example Javascript, you must have a browser to run a javascript code (windows can work around it as well).
Or C#/Java, you must have their frameworks installed to run them (.NET and JVM), otherwise they just won't run.
So they're limited by whatever layers precede them. In comparison, an Assembly code could be run nearly anywhere - it's just a matter of how.
Taking all of these in consideration, you can figure why C/C++ is the language of choice for game programming.
It's not hard (harder than languages higher than it, obviously, but much much much easier than Assembly and lower languages), provides much control, and works nearly everywhere (where it doesn't is because the manufacturer is lazy), making your code much more portable.
But don't forget the famous "use the right tool for the job" quote. It'd be a waste of time to program in Assembly for a Windows Game just as it would be unproductive to program a game for PS3 using Javascript (not to mention the work it'd be just to have Javascript useable in the machine).