March 11, 2020

Object oriented programming in C

Instead of using a dedicated OOP language like Python or Java, it's possible to create even in C an object oriented project. Some features are missing but in general it's possible for doing so. The only difference is, that the fucntion call doesn't need the full path to the module, but it can be called direct.

// file: main.c
// -------------------------------
#include "stdio.h"
#include "point.c"

int main()
{
  printf("main\n");
  point_show();
}


// file: point.c
// -------------------------------
typedef struct {
  int x;
  int y;
} Point;

void set(Point* p, int x, int y) {
  printf("set\n");
  p->x=x;
  p->y=y;
};
void show(Point* p) {
  printf("show %d %d\n",p->x,p->y);
};
void point_show() {
  Point p;
  set(&p,10,5);
  show(&p);  
}

The interesting question is, if such a programming style works for practical application. In the github archive there are many examples for games written not in C++ but in normal C. Many of them are using this style. The subparts of the game are stored in dedicated files which contains of a struct definition for the data and a list of functions for the C code. What these modules are doing is to to call their own functions, similar to the concept of classes in other programming languages.

Understanding the inner working is a bit harder than mastering Python OOP, because in a C project, pointers are needed for everything. But suppose the idea is not to use Python, C++, C# and Java, then this kind of programming style is a here to stay. It allows to write semi-object oriented software which scales very well. It's not a coincidence that most of real programs which are available out of the box in Unix and Linux operating systems were written in the C language but not in Java and not in C++. The reason is, that the advantage of dedicated OOP languages over C is small.

Let us take a closer look into the sourcecode. In the main file, only a simple call to a module is available. All the details of the Point module are hidden in the external file. In the point.c file, the datastructure is stored together with the functions in the same file. If the programmer takes care, that the maximum length of the file remains under 100 lines of code, it's not very complicated to maintain and bugfix the code. If the C code is rewritten in Python or C++ it will look nearly the same. That means, the overall project is divided into classes which are responsible for subparts of the project.

The assumption is, that writing larger projects in C can be realized with the same productivity like in Java or Python. That means, the C programmer won't miss OOP features, because most of them can be replicated with the C language. This makes it hard to convince a C programmer to convert the existing code into a different language. Basically spoken, the C language has a bright future and will be used very often.

Programming language statistics

The well known Tiobe index doesn't reflect programming languages in reality. There is a gap available that in computer education, Java and C++ is very important but in reality nobody is using these languages. A more realistic picture is counting the lines of code, https://www.openhub.net/languages?query=&sort=code

According to the Openhub directory the most used languages in the wild are:

1. C with 9.4 billion Lines of code
2. Javascript 4 billion LoC
3. XML 2.9
4. C++ 2.6
5. Java 2.4
6. HTML 1.4
7. PHP 0.9 billion
8. Go 0.6 billion loc
9. Python 0.7 billion
10. CSS 0.5 billion

In another older statistics, the C language outperforms also C++ easily in amount of written codelines. The sourcecode of the Debian operating system was measured in the year 2005 which contains of 105 million lines of code overall. 65% of them were written in C and only 12% were written in C++.[1]

There are some points against the C language. Most AAA videogames are not writtein pure C but the normal C++ language is used. And many developers are explaining that C is dead and they are prefereing C++ because videogames need object-oriented features. But, a closer look into the C++ sourcecode will show, that nearly all the game engines and the games written on top of the engines are using pointers in the C++ classes. It's not possible to avoid pointers in C++ because this ensures the maximum performance. What the programmers are writing in the code is not C++ but they are programming in C with pointers and using only the class statement and sometimes the templates of the C++ compiler.

Let us make a thought experiment. What will happen, if a larger computergame is reprogrammed in C? THe sourcecode will look nearly the same. That means, lots of pointers are needed to draw the sprites and call foreign modules. The difference between C a program which is using static functions to get access to structs and a C++ program which is using dedicated classes is low.

[1] Amor, Juan José, Gregorio Robles, and Jesus Gonzalez-Barahona. "Measuring woody: The size of debian 3.0." arXiv preprint cs/0506067 (2005).

No comments:

Post a Comment