Why write Godot in C?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Joe0239

According to Wikipedia, Godot is written in C and C++. Why can’t Godot just be written in C++? What advantages does C have over C++?

:bust_in_silhouette: Reply From: Xrayez

I don’t think it’s about the choice but rather a byproduct and accumulation of using other third-party libraries that are written in C language like zlib.

The other reason might be simply because the C language provides a lower level access that allows to optimize critical parts of the engine.

Another reason might arise from the fact that writing in C language gives opportunity to support more hardware platforms.

I thought C++ can be just as low level as C?

Joe0239 | 2018-07-20 08:36

Both C and C++ are considered low-level languages. The need for creating C was arisen from making assembler more human-readable. One can clearly see how C and C++ differ from each other when it comes down to string manipulations, for instance:

char str1[16];
char str2[16];
strcpy(str1, str2);

vs

String str1, str2;
str1 = str2;

With the first example you need to make sure that str1’s size is long enough to contain str2, in C++ you can just use String class that does that for you ensuring you don’t do any stupid mistakes that would result in memory overflows (in cost of some decrease in performance). I think it boils down to how abstract these operations could go and how languages manage memory in general.

The distinction between C and C++ might be blurry because you can always you C functions while still benefiting from using C++ features like classes. This is another reason why the code statistics might show mixed usage result.

Xrayez | 2018-07-20 09:03

:bust_in_silhouette: Reply From: hungrymonkey

Many libraries expose themselves via C API because C++ ABI is not stable

Godot needs C code to interact with libraries.

It’s also the reason why GDNative is ship with a C ABI as its base.