October 21, 2022

Writing GUI apps with the c language

 

Around the year 1992 there was a drastic event in the domain of software programming. The former well standardized Ansi C language has felt of fashion and was replaced by object oriented languages and scripting languages. Until today the fact itself is seldom mentioned and the reason why remain unclear.
A possible hypothesis is, that it has to do with the creation of GUI apps. From a technical perspective a GUI app can created with a library. In the easiest case the curses.h library allows to create an interactive text based application. More advanced software can be realized with GTK+ and the Win32 API. For reason of better understanding let us first focus on the curses.h library.
In contrast to the established Win32 API the curses.h library works out of the box in linux and windows as well and is much smaller than graphical libraries. So it is a great choice for programming a modern user centric application in the Ansi C language. The problem is, that even simple hello world apps written with curses.h are hard to understand and difficult to improve. not because of the library itself or because of the C language but because the subject is complicated.
The typical curses.h application contains of event handlers, keystroke events, multiple windows and object oriented style message parsing. Non of these techniques are created by the programmer itself but they are the logical consequence if the attempt is to create a GUI app on the text console.
If the console.h library is replaced by graphical oriented GUI libraries like GTK+ the complexity will grow further. Before the programmer can write a simple hello world app he has to read a dozens of books with each 1300 pages and even then many new problems will become visible. The situation in Windows and Mac OS is the same.
The only working advice to avoid the complexity of a GUI app is to dismiss GUI libraries at all. Without the curses.h libary only non interactive software can be created which are command oriented but not menu oriented. The average command line tool in Linux like the cat or the grep command doesn't need the curses.h or the gtk library but they can be programmed in the C language style before 1992.
So we can say that no a certain programming language or library has caused the increased complexity but the problems have increases since the year 1992 with the advent of text GUIs and graphical GUIs. Such a proram type is easier to use by the end user but it is very complicated to program it in software. There is not a single programming language or library available for this purpose but lots of different approaches were created over the years. And all of them are very complicated to use. Easy self claimed lightweight GUI toolkits like FLTK which are reduced to a minimum of features will need 100 MB to install the package and the documentation contains hundred of different methods and classes.
The advent of object oriented languages like C++, RAD Tools like Visual studio and JIT languages like C# wasn't a coincidence but it was the natural answer to the problem of creating GUI programs. It is not possible to write a modern GUI app the same way like a c program before the year 1992. In theory it is possible to write a GUI app with the c language, for example with the win32api or with the gtk.h library. BUt the resulting program will look the same like a complex C++ app which was scripted with additional languages. It seems, that the requirement itself to write a gui app is the cause of the complexity.
Programming in the classical ”before 1992” style is only possible in the case of command line programs. It seems, that the growing complexity after this years is caused by the objective to write the mentioned GUI apps.
This thesis would explain why the amount of GUI apps for Linux is low or even zero. The cause is that for creating a GUI app a huge amount of manpower and additional tools is needed. The best design decision is to avoid GUI apps at all and program only command line tools. Such piece of software can be written in ansi C,, will need a short amount of codelines and is easy to understand.
Let us give a simple example. A state of the art method to create a GUI app in Linux would be to use the Java language in combination with additional libraries and RAD tools for creating the buttons and widgets. The programmer has to install around 10 GB of software before he can create a simple hello world app which is doing nothing. This kind of workflow isn't a sign of failure but it is the best practice method. The cause is, that programming a GUI app is a complicated task and a complicated workflow with lots of layers and bloatware languages is the only way to handle it. Even if the user prefers a lightweight programming environment like Forth he will run into the same problems if he likes to create GUI applications.
The cause why software engineering before the year 1992 was so much easier was is because during this period GUI programs were not available. The typical program in that time was a MS-DOS game in single user mode or a unix command line tool without any interaction. Writing such pre-1992 apps is much easier. A normal c compiler is fits perfect to the problem.
What all the GUI apps in all operating systems have in common is, that they are large scale projects. No matter if the program was realized in C++, java or ansi C and no matter if it was programmed for Linux or Windows and every single case the average gui app will need 100 MB and more on the harddrive, contains of 100k and more lines of code and depends on lots of libraries.

October 05, 2022

The remarkable success of the C programming language

 

With the advent of the C++ language and more recently the advent of Java, Python and other modern language, the good old ansic language has felt out of fashion. At least this is the public perception if the amount of github repositories and the amount of stackoverflow questions are counted. Newly created projects and newly asked questions are mostly about every language except Ansi C.
This situation stands in contrast to the importance of the ansi c language in the past. So there is a need to explain what the status of Ansi c was in the late 1980s. According to some older books from this time, the C language was some sort of standard language used for professional software development. It has emerged in the context of UNIX and then it was adapted on 16bit home computers like Amiga 500 and on MS-DOS PCs as well. 10 out of 10 randomly selected games and application software like databases from the 1980s were written in C.
It is important to know, that the C++ language wasn't used during this time. At least for the time until 1990 the only important language was Ansi-C. Ansi-C is a modular compiled language which runs highly efficient on all sorts of computers. It is known as a difficult to learn which has to do with the header files and the pointers, but it seems, that learning the language is only a question how often it was used.
The situation with C has changed dramatically after the year 1990. At least the published information in the books are explaining to the audience that C is outdated and that the future has to do with object oriented programming under newly created GUI frameworks like Windows 3.x The open question is what is the role of the C language from 1990 to 2000? There are two possible situation available. Either the ansi c language has become obsolete and was replaced by more powerful languages like C++, or the ansi c language was used continuously and only the public description has changed.
The main difference between Ansi c and C++ is, that only C++ is objected oriented. OOP means to use classes and inheritance. Another famous OOP language from the 1990s was Borland delphi and Java. There is some sort of similarity between C and C++ and as a consequence it is often described as C/C++. But from a programming perspective there is a fundamental difference between the languages available. And this difference isn't described very well in the existing literature.
The main cause why C++ has become popular is because it allows to create larger applications and works very well with GUI frameworks. But, in theory the Ansi c language fulfills the same purposes. The modularity allows to create larger software and the language fits great to GUI applications.
To recognize the difference between C and C++ it makes sense to analyze real software projects namely Debian Hamm which was published in 1998. During that time, the C++ available was available but it was seldom used in Debian. 74% of the source code was programmed in Ansi-C while 6% were created in C++ which was mostly Qt. Getting precise numbers for the Windows XP OS which was released in 2001 is a bit harder. According to online forums the core was written in Ansi C but it remains open how many lines of code exactly.
The working hypothesis is, that there is no need to switch from Ansi-C to C++ because Ansi-C is powerful enough to write all sort of software including GUI apps. The bottleneck is not the compiler but the available tutorials to teach the subject to programmers.
To verify if the thesis is correct or wrong we have to analyze the lifetime of programming books and compilers. Are the ansi C books from late 1980s outdated today, or is the information still relevant? The assumption is, that these books are still relevant. The knowledge written in a book from 1990 can be used 1:1 to understand current software. And this implies that the Ansi C language was never replaced by C++ but all the software is written in modular C while OOP isn't used in the reality.
Such a perspective contradicts the public awareness of programming languages. On major forums like Stackoverflow many questions are asked about creating classes and inheritance. Are these posts not relevant for writing software in the real world? The assumption is, that OOP languages like Java or Python are used by amateurs similar to Turbo pascal, but production software and commercial programs are written in a modular fashion with Ansi C.
[1] Debian programming languages https://sources.debian.org/stats/

C vs C++

 

C is a modular compiled language while C++ is an object oriented compiled language. The question to answer is which one is better?
Object orientation has become famous with the advent of Smalltalk which has later evolved in C++ and Java. Programming something without OOP features sounds a bit outdated. On the other hand, C programmers are convinced that there is no need to utilize object orientation. And perhaps this would explain why the debate between C vs. C++ remains unsolved.
Creating short hello world programs is possible in any language. The more serous problem is to create programs which have 1000 lines of code and more. The promise of OOP languages is to support this attempt. On the other hand it remains unclear how to write such programs with C. The interesting situation is, that very large projects like the Linux operating system are mostly written in ansi c so there is no need for object orientation.
On the other hand, this understanding contradicts most programming books published in the last 20 years who are explaining to the audience that OOP is here to stay.
Let us try to listen to the C community and how they are solving the complexity problem. The idea of a C program is to use modular programming. Modular programming is the same technique used in Pascal programs. The idea is that a file is a unit aka translation unit. Roughly spoken, a unit is some sort of class. The interesting but seldom mentioned fact is, that a unit can have variables which are accessible from many functions of the same file. So it seems, that modular programming and OOP programming have some similarities.
Perhaps it makes sense to compare modular programming with OOP programming in detail. The idea behind a class is, that the class contains of variables and methods. The methods are operating on the variables, and this is the strategy to reduce complexity. If a bug was found in the program only a single class has to be modified which is located in the best case in a single file and has not more than 100 lines of code. But, exactly this feature is provided by modular programming namely the C language and Pascal as well. There are files which contains of variables and functions and a single file can be tested and improved independently from the rest of the code.
One possible source of confusion is located in the documentation and tutorials around the C language. Even many books were written in the last 30 years it is a rare case that a book will explain how to create larger programs. Most books are assuming that the user has never programmed in any language and it is explained in detail what a function is, what an if statement is doing and how to use for loops. But the average program will know for sure about these things because the concepts are available in all the language. The for language in a c language is mostly the same like for languages in C++, Java, or C#. The real bottleneck in learning C is to master to use header files and modules. The books about the C++ language and also books about C# and Python are explaining very well how to use classes. The explanation makes sense and is easy to read and perhaps this is the cause why most programmers think that dedicated OOP languages are much easier to use than Ansi-C.
Using the header files and creating modules in C is bit more complicated than using a class but it is not outside of reach to understand the situation. The only thing is, that writing larger software in C is less common or at least it is less frequently explained in the literature. Perhaps the bottleneck is not located in the c compiler itself but in the tutorials around the language?
Most famous books for the C language like “Mike Banahan: The C Book second edition” or “K&R: The C Programming Language” are failing completely to explain header files.. The subject is either ignored or explained in a short chapter in the appendix. What we can say for sure is, that the average programmer who has read these books won't be able to program larger software in C. In contrast, after reading a random book about Java or Python it is very likely that the programmer is able to split the code over multiple classes stored in different files.
The similarity between modular programming languages and OOP languages is, that the program is split over files. A typical smaller program contains of 10 files which have each 100 lines of code. The ability to split the code over files is a fundamental building block in creating more complex applications. There is no workaround for this problem. The assumption is, that dedicated OOP languages are able to explain the modularization easier than low level languages like Pascal and Ansi C.
Somebody may argue that OOP programming contains of more elements than only splitting the code into files. A typical OOP feature is the ability to create many objects from the same class. Most games are working with lots of sprites. In an OOP language it is very easy to create an array of objects while in ansi C the compiler doesn't even know what an object is.
But this problem can be solved with a normal struct easily. The c language allows to create an array of struct. And then the single variable struct can be send to a function and manipulated or shown on the screen. There is no need to use OOP programming for displaying 100 and more sprites on the screen. That means Ansi-C is more than sufficient to create all sort of simulation programs similar to what C++ has to offer.
From a technical perspective the C language can be used as a replacement for object oriented languages. The bottleneck are the existing tutorials which are not able to explain how to split source code into files. This has frustrated programmers who are preferring dedicated OOP languages.

Creating header files in Ansi C

 



One of the serious problems within C programming is the art of creating a .h file. It is obvious that any programming projects needs to be split into multiple files and at the same time, most C newbies are not aware how to create these header files.
To make things more pleasant the first thing to know is, that a header file should contains only of the public visible functions. Let me give an example. The module contains three functions which are set(), get() and run(). The idea is that from the outside the set() function is executed first, then the run() function to activate the module and at the end the outside program is executing the get() function to retrieve the output of the program.
The include file consists only of these three prototypes but not all the additional functions and especially not of the variables from the module. All these things are declared private which means they are not mentioned in the header file but they are available in the .c file for the module.
The main advantage is, that things are easier to understand if they are reduced to its minimum. The outside program can include the .h file and it is only able to execute the mentioned functions but has no access to the modul's internal functions. Sure, the same functionality can be realized with the static statement which is often recommended in Online forum, but it is more easier to delete the function prototype from the header file at all. The cause is that by definition a header file has to obligation to communicate between a module and the outside program. And if no communication is needed about internal functions why should that are mentioned at all?
An interesting feature of the C language is, that the inner working of a module is very similar to a class known from object oriented languages. The idea is, that they are variables in this module which can be manipulated by the functions and that it not allowed to get access to these variables from the outside. A single module solves a clear defined problem and in the optimal case the code can be reused as a library.

Why has the C language evolved into C++?

 

It is not sufficient to talk only about programming languages, because in the reality a standalone C compiler is a rare sort of software What was used in the 1990s mostly were integrated development tools e.g. Borland Turbo C, Microsoft QuickC and Watcom C. What these mainstream programs have in common is a bundle which contains of an underlying C compiler with a frontend to type in the Code.
Around the year 1992 all these programs have disappeared. They were replaced by dedicated C++ IDEs because of the upraising of the Windows 3.1 operating system. The question was not how to program in C but the question was how to build GUI applications with a graphical editor, a help system, a debugger and some sort of compiler which includes make files.
The only non IDE C compiler is GCC which can be utilized with different text editors. GCC is a standalone C compiler which is focussed on the C language instead of the programming workflow. Most endusers belief that the C language has felt out of fashion because they are no products available around this language. If all the major software companies have replaced C with C++ then it is wise to assume that Ansi-C has become outdated.
The underlying question is if it makes sense to bundle a programming language with a certain Text editor. The resulting tool is a complex high price product which can't be shipped on 2 floppy discs but will need many CD ROMs and an enormous amount of manuals. It is not about the C++ language itself, but about the code completion, GUI designer, GUI libraries and so on. At least in the public perception the C++ has replaced the former Ansi c language since the year 1992. The new programming paradigm was called object orientation and the C++ language was a natural choice for doing so.

October 03, 2022

Programming exercises to understand robotics

 AI and robotics is mostly described as programming technique or as an algorithm. The question is which sort of program provides AI, or which sort of AI library is available? The surprising situation is, that AI is located in a different position. It has to do with with a programming exercise.

Some typical Non AI programming exercises are:
- "Write a software in python which prints out 6 randomly generated numbers"
- "Write a python program which plots a line on the screen"
- "write a java program which adds two numbers and prints the result on the screen"

These exercises are used to a teach programming and a certain programming language at the university. It depends on the student how to solve it. AI and robotics is some sort of advanced programming exercises which can be labeled as ultra-hard. The question is which sort of AI programming exercise is available? A possible challenge is given next.




There is a robot in a maze which can be controlled with the keyboard. The task is to write a grounded sensor for the robot which contains of 6 elements:
[xpos,ypos,distancefront, distanceleft,distanceright,distancetoenergy]

The sensor array should be printed to the screen all the time.

Such a programming exercise fits into the same category like "write a program which prints out all the prime numbers" because it formulates a problem which can be solved with an algorithm. The task for the student is to understand the problem, write a short program in Python or Java and then it can be determined if the software solves the task.

From an abstract point of view it is important to ask which sort of programming exercises are needed to explain the subject of robotics. What all these challenges have in common is, that not a certain algorithm is needed but a certain exercise. The formulated problem with the sensor array doesn't contain of program code nor an algorithm. But is a figure plus a text which formulates a problem. It is up to the opponent (the student) to provide an answer to the problem. The answer is written in a certain proramming language and will contain of an algorithm. In the concrete example, the typical answer will take the requested 6 elements from the underlying physics engine and in case of the distance value it has to be calculated from scratch. Then a print routine is needed to show the result on the screen.

Creating a GUI with Ansi-C


 

The dominant cause for the upraising of object oriented programming language was the introduction of GUI operating systems like Windows. Since the 1990, the amount of dedicated OOP languages like Java, C#, Python and Ruby have grown exponentially and many programmers are convinced that they need the ability to create classes.
But from a technical point it is possible to create a gui app with the imperative C language as well. There are some prerequisites needed for example a tutorial about creating the widgets itself. [1] Also the programmer has to make sure that all the needed libraries are installed and that the correct compiler settings where chosen.
/* 	
  gcc -ansi 2_ansic.c $(pkg-config --cflags --libs gtk+-3.0) && ./a.out

*/
#include <stdio.h>
#include <gtk/gtk.h>

void show_about(GtkWidget *widget, gpointer data) {
  GtkWidget *dialog = gtk_about_dialog_new();
  gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(dialog), "Sample program");
  gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "0.1");
  gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog),
     "The quick brown fox jumps over the lazy dog");
  gtk_dialog_run(GTK_DIALOG (dialog));
  gtk_widget_destroy(dialog);
}

void button_clicked(GtkWidget *widget, gpointer data) {
  printf("clicked\n");
}

void initwindow() {
  gtk_init(NULL, NULL);

  GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Hello World");
  gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);

  GtkWidget *fixed = gtk_fixed_new();
  gtk_container_add(GTK_CONTAINER(window), fixed);

  GtkWidget *btn1 = gtk_button_new_with_label("Button");
  gtk_fixed_put(GTK_FIXED(fixed), btn1, 150, 50);
  gtk_widget_set_size_request(btn1, 80, 30);

  GtkWidget *entry1 = gtk_entry_new();
  gtk_fixed_put(GTK_FIXED(fixed), entry1, 150, 5);

  /* events */
  /*g_signal_connect(G_OBJECT(btn1), "clicked", G_CALLBACK(button_clicked), NULL);*/
  g_signal_connect(G_OBJECT(btn1), "clicked", G_CALLBACK(show_about), NULL);
  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  gtk_widget_show_all(window);
  gtk_main();
}

int main() {
  initwindow();
  return 0;
}
Then it is possible to create a simple window on the screen which includes the ability to show a message box.
There are some differences available to a normal python script which is producing the same result. The main difference is, that the C language is working with pointers. The second difference is, that it is not possible to create any sort of classes. In theory there is a workaround available. C has a built in module mechanism, which means to define variables in files and use functions in the same file to get access to the variable. And the pointer arithmetic is some sort of extra effort which has to be done by the programmer to make sure that the source code is working.
The main advantage of the shown ansi-c code in contrast to a python script is, that the program can be executed in a production environment. The created binary file has a size of only 18kb and runs with the maximum performance.
The open question is what will happen if the programs becomes larger. The main cause why OOP languages were introduced is to manage the complexity. In theory the same purpose can be realized with the mentioned module concept of C, but the amount of literature about the topic is very little.

October 01, 2022

Programming languages in a cluster diagram




The amount of programming languages is endless. One suboptimal option to categorize them is by their history. The more elaborated attempt in building clusters is by a two variable model which contains of interpreted vs compiled and procedural vs. objectoriented. The interesting situation is, that all the existing languages can be located at a certain position in the 2d chart.

And secondly it is possible to name only the languages located on the four corner which are: Basic, Python, C++ and C. Each of these languages has in one variable the maximum value. For example, the Basic language is an interpreted language which is working with the procedural paradigm.

Let us determine the best programming languages for professional purposes. The interesting situation is, that even more than 200 different programming languages were invented, most of them are only used in an academic context. At the university lots of languages are taught, e.g. Pascal, Python and of course Prolog. But, most of these languages are not used for writing operating systems which includes applications for operating systems. The concern of expert prorammers against these academic languages is, that they are objectoriented which is equal to rreject them or they are interpreted which also implies to reject them. The only language which is not object oriented and not interpreted is C.

A closer look into the debian repository and into the anecdotal description of what is used for creating MS-Dos and Win16 bit application will show, that 100% of the software in the past was written in Ansi-C. This is especially the case for the early 1990s. According to the programming books of this time, the future is about object oriented programming namely C++, Delphi and Java, But at the same time, these languages were not used to write production ready sourcecode. But the programmers are prefering Ansi-C.

According to the picture it is very easy to explain the reason why. Professional programmers are prefering a language which is compiled and procedural at the same time. The only language available in this category is C. All the other languages like C++, Basic, Prolog, Lua and so on have a different self understanding.

The question which remains open is, why exactly professional software is written with compiled&procedural languages. It seems that only this combination ensures the maximum performance and the ability to maintain a larger amount of codelines. What the Linux sourcecode and the Windows sourcecode have in common is, that the project has over 1 million lines of code and that the binary file is executed very efficient.