October 21, 2022

Tribute to Ansi C

 

The C90 Ansi C standard is a great choice for programming. The language was invented 40 years ago and outperforms every modern language easily. C compilers are available for all computers and all operating systems and allow to write any sort of software. No matter if C is used to create videogames, webapplications, desktop apps or even operating systems libraries — c is always a great tool.
Every criticism against the c language is a sign for a misunderstand what programming is about. Programming is not about drag and drop GUI widgets into a frame and press a run button. Programming is not about importing a python library without understanding what the inner working is. But software engineering is about understanding from a low level perspective what the computer is doing internally. This includes to use raw pointers with direct memory access to compare two strings, and write sorting algorithms from scratch because they are missing in the standard C library. This is not problem, but is ensures that programming makes fun.
So called high level languages like Java or PHP are created by inexperienced programmers for non programmers and are a sign of failure. The best recommendation is to rewrite all these existing nonsons programs with Ansi C and create new software always in C as well. Only a compiled language can be executed fast enough and it is simply not true that the productivity in scripting languages is higher than in C. Most libraries are not needed in the reality. For example the matplot library which allows python programmers to draw charts can be simply emulated with a c program which draws the graphics into the video ram of the graphics ram. The underlying algorithm can be written from scratch in a short amount of time and is more cpu efficient than any precompiled library.
Object orientation which has become famous since the 1990s is a dead end because Ansi C has a more powerful feature called modules which allows to interconnect different programs into larger systems. There is no need to create complex C++ compilers or even invent jit compilers to execute PHP, but the same task can be realized with a vanilla C compiler much easier. Basically spoken, all these java programmers are wasting their time, if they are writing the software in so called high level languages. They have simply not understood what a linked list is or what the C preprocessor is doing internally.
With the c language everything is great, but the ecosystem around C has made major mistakes. In the last 20 years lots of new programming languages were invented with the promise to make programming easier to learn. Non of these attempts was able to replace the c language. Buzzwords like agile programming and object orientation were mostly invented by c programmers and can't be replicated in other programming languages. a well trained C programmer is able to write any sort of applications within a week. All what he needs is a VIM editor plus the gcc compiler and the c internal features are producing error free code by itself. The resulting binary file will run on a server and a desktop very well and it is unlikely that the code needs to be improved. And if another programmer will find an error, he can easily locate the error because the program is divided into smaller chunks. Ansi C has a natural tendency to evolve into stable software projects which are written once and then used as production ready code for decades. The C language stands for high software quality and maximum coding productivity.
One of the strength of C is string handling. There are more than 12 different ways available how to concat two strings together.[1] it is recommended to decide for one of them and the program will be protected against all sort of security issues.
[1] https://stackoverflow.com/questions/8465006/how-do-i-concatenate-two-strings-in-c

Which programming language is the best?

 

There are many attempts available to compare different programming languages each other. In contrast to a famous myth it is possible to determine which language is the best if some prerequisites are fulfilled. First thing to do is to define what “best” means. A possible definition is, that best= high level programming language. The assumption is, that programmers are preferring high level over low level languages. Then it should be defined what a high level language is about. High level means that no pointers are needed, that object orientation is available, that the language is interpreted and very important that a huge libraries with predefined functions is available.
The reason why high level language are preferred over low level language is because it allows to create the same app in a shorter amount of time Writing a hello world gui app will take in pure assembly language around 1 month, in the c language it will take 1 week, in the c# it will take 1 day and in python it can be realized in 10 minutes.
The interesting situation is, that the sorting order from low level to high level language can be measured on an objective basis. Around 99% of the programmer will agree that the following sorting order is correct:
1. assembly language
2. Ansi C
3. C++
4. Java, C#
6. Python, PHP
Low level language are compiled and are using pointers. Midlevel langages like C# doesn't use pointers and there is need to define the datatypes, while high level languages like Python doesn't need anything but the hello world Qt app is created with 5 lines of code and executed on any device and any operating system.
The only question left open is, if high level language are a here to stay or if python has some sort of disadvantages. In most cases the concern is, that high level languages are not great for production systems. Python is known to run slowly, while C# and Java is sometimes called as not snappy enough similar to C++. According to the raw numbers a jit compiler and especially Python like interpreters are much slower than low level languages. And the question is, if such constraints is important or not.
According to the amount of books about high level language and the number of newly created apps, high level languages have replaced low level languages. Most newly created software is not created in Ansi C anymore but in C# and other high level programming languages.Somebody may argue for or against this development but at least it is a fact. The motivation for the individual programmer is mostly to reduce the development time. Instead of programming an app with Ansi C within in 2 years, the idea is to program the same app with a high level language in only half of a year.
In the context of python this sort of programming style is sometimes criticized as creating throw away code. This implies that python code is equal to low quality software. In contrast, a dedicated low level language like Ansi C is labeled as high quality language because it takes much longer until the app is ready.
It is possible to discuss which of the opinion is correct. What we can say for sure is, that all the programing languages can be sorted in a linear chain from low level to high level. High level means, that the same app can be written with less amount of codelines. The result is, that it can be defined precisely if python is more high level than C# or Ansi C.
Python is not the top of the pyramid because there are some languages available which are more high level. GUI RAD builders and especially the MS Access database surpass easily the python language in terms of high level vs low level language. A MS Access database can be created without writing a single line of code. Somebody may ask if such a database has a high quality compared to a program written in C# or even Visual Basic, but at least the fact is, that MS Access is more high level than any programming language.
Another example for ultra high level languages are construction kits and level builder in Games. Here the user doesn't program the game or the app, but is modifying only a level. For example in a level editor for the lemmings game it is possible to draw some obstacles into the screen and after pressing the run button the game gets started. The possible alternative would be to create the game with C++ from scratch which will take for sure months upto years and a deep understanding of the C++ language. With a level builder the same objective can be realized much easier.
A while ago there was a debate at stackoverflow about rewriting an existing PHP webappliation in plain Ansi C.[1] The main argument against such an attempt was, that it will take too much manhours for doing so. Even if the php language runs a bit slower than Ansi C it is the better choice because it is more high level. High level are seen as equal to a good choice because it allows to save time and costs. Even some comments have argued pro C/C++ and the arguments were really good, it is easy to grasp that the stackoverflow post came to the conclusion that PHP is the better langauge for web development over C/C++. Simply because of the fact that only very few or even none web applications are written in C/C++.
[1] https://stackoverflow.com/questions/3079153/why-do-you-not-use-c-for-your-web-apps

Is C# obsolete?

 

In the history of programming language there is a tendency available from low level- towards high level languages. From the perspective of the C language all the programs written in Assembly are outdated. The reason is that the source code can be written in a shorter amount of time. Because of this reason the C# language has replaced the former C++ language entirely.
On the long hand the transition towards high level language will make even c# obsolete. The reason is, that in comparison to scripting languages like Python, C# can be called a low level language. There is need to use datatypes like int and string and many class initialization will need a lot of boiler plate code. Without any doubt the Python interpreter is more high level than C# so the C# language is outdated.
Writing a simple hello world gui app with C# will take around 30% more lines of code than the same app written in Python. Such an overhead is difficult to maintain and it will increase the time to market time.
According to the Rosetttacode website, the string append problem can be solved with many programming languages. The low level C language will need the most amount of written lines, then comes C# and the shortest code can be written in Python.

// c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char *sconcat(const char *s1, const char *s2)
{
  char *s0 = malloc(strlen(s1)+strlen(s2)+1);
  strcpy(s0, s1);
  strcat(s0, s2);
  return s0;
}
int main()
{
  char str1[]="Hello ";
  char str2[]="World";
  char *str3=sconcat(str1,str2);
  printf("%s\n",str3);
  return 0;
}


// c#
class Program
{
    static void Main(string[] args)
    {
        string x = "foo";
        x += "bar";
        System.Console.WriteLine(x);
    }
}

// python
str = "12345678";
str += "9!";
print(str)

 

The difference between C# and Python is not huge, but C# will need around 30% more space for writing the semicolon, the datatypes and lots of extra code for anything. This additional effort has to be written by the programmer which reduces the productivity. Basically spoken, C# is outdated because it not high level enough.
Let us take a closer look into the C program. First thing to mention is, that the string concat method was implemented safe and is one of the easiest to understand. It is using in addition external routines to make things easier. It is possible to create more advanced string concat routines. Nevertheless the lines of code are difficult to understand. For C programmers it makes sense to use three different commands like malloc, strcpy and so on only to add str1 with str2 but from an outside perspective, the source code is a bit hard to read.
Such situation is available for many problems which should be implemented in the C language. In most cases, there are at least 40 different ways available how to solve it, and all of them have to do with pointer manipulation and manually typing in lots of code lines. In contrast, the written code in the python language is much easier to type and to understand. All what is needed is a statement like “str1+str2” without further subroutines.
The advantage is not located in Python directly, because string manipulation is also easy going in languages like PHP and Ruby. But it has to do with low level vs high level languages.


 

 

Writing larger programs in Ansi C and Forth

 

The dominant reason why object oriented programming languages have become successful is because of its ability to split larger problems into classes. Without this feature, the program code is stored in a single large file in which hundred of functions have access to hundred of variables. On the other hand, many larger programs were written in Forth and Ansi C without any sort of OOP feature so it seems, that some sort of hidden technique is available which works similar to object oriented programming.
What Ansi C programmers are aware of it is the module concept. A module is a file in which the scope of the functions and variables is limited to this file. it is possible to set a value in a module like “module1.msg=”hello world” and that call a function in that module with “module1.show()”. This allows to simulate object oriented programming.
Modular programming allows to reduce complexity without introducing dedicated OOP techniques. This allows to use much simpler compilers like the ansi C compiler or even minimalist languages like Forth. The question is not if a certain programming supports objects but if the concept of modules is available. Similar to OOP programming modular programmings allows to split larger problems into smaller one. Each function is limited to a single module and this allows to solve the problem in general. The pascal language which lacks also of OOP features has of course the feature to utilize modules. In pascal a module is called a unit and similar to C it is stored in a single file. From the perspective of Java and Python a single pascal unit is equal to a class which has variables and methods.

Modular programming with python

 

Sometimes it was asked how to implement object oriented programming in low level languages like C and assembly. A possible idea would be to improve the existing struct datatype with functions which are pointers. Then the user has to allocate memory and can use objects in C. But the overall pipeline is very complicated and there is an option available to simplify the idea drastically.
The idea is to dismiss object oriented programming at all and prefer modular programming. Modular programming is a complexity reduction technique which is available in most existing programming languages. It can be used in python and in C and pascal as well. The idea is that a single file is equal to a module. [1] The file contains of variables and functions and comes close to the concept of a class. in the written sourcecode a program written with modules and one with classes are looking nearly the same. A module allows to group items in the sourcecode into cluster and maintain the code separately.
Most object oriented programs are in reality normal modules distributed over many files. The only reason why OOP is the dominant programming paradigm and modular programming has become a niche is because of the existing programming tutorials. The self understanding of most books is, that a programming language consists of statements and functions and this enough to solve a problem. The problem is, that without classes aka modules it is impossible to solve larger problems. If the code is longer than 500 lines of code there is a clear need to split it into sub entities. Unfortunately this strategy is not implemented directly in a programming language but it has to do with project management and compiler preprocessors. It can be s
From the perspective of modular programming it is surprising to see, how similar programming works in different languages. In contrast to a common myth the programming languages Pascal, Python, C++, C and , Assembly and Forth have one thing in common. They are supporting all the concept of units. A unit is – again – a single file which can be included in other files and allows to split the code into logical groups. Similar to OOP it allows to group variables and methods into the same instance.
To understand why modular programming is not very common it is important to take a look back into the didactic of programming. In the 1980s the concept of structured programming was mentioned frequently. Structured programming means to divide the sourcecode into functions which can be realized with C and pascal. Advantage is, that each function can be maintained separately which allows to writer longer programs. What was ignored in the 1980s is, that structured programming alone is useless because if a file contains of around 10 functions it will become hard to maintain anymore. So the limit is, around 500 lines of code which can be managed with structured programming alone.
In the 1990s the concept of object orientation was promoted as the logical next step after structured programming. The idea was to encapsulate the code in object libraries which allows an unlimited code size. Nobody has questioned the idea at all or compared it with modular programming which provides a similar feature.
In theory, OOP is superior over normal modular programming. But in the reality, most programs won't profit from these additional advantages. Nearly all projects which are smaller than 100k lines of code can be realized with modular programming very well.
[1] https://python-course.eu/python-tutorial/modules-and-modular-programming.php

The surprising success of the C programming language

 

There is some sort of mystery in the history of computing. In spite of many criticism, the C language was never abandoned but it was used over decades for creating all sorts of programs. To understand the success we have to focus first on some of possible alternatives.
Since the early 1980s the C language was known and many attempts were made to program in a different language. Notable examples are Basic, Pascal and recently Python. All these programming languages are promising to the newbie that it is possible to write software without much effort. Basic was the first widely used interpreted languages. First versions were oriented on line numbers, while later Basic interpreters like Omikron Basic and Visual basic provided the ability to create large projects with structured and even object oriented features.
The problem with basic language was, that it was never standardized. A program written in Qbasic can't be executed in visual basic. And both systems have the problem that the generated executable code is very slow. Many programming languages were invented over the years. Some very successful examples in the recent years are python and C#. In contrast to BASIC, Python is standardized, cross platform ready and has a huge amount of libraries. Similar to Basic it is used by non programmers for creating a prototype. Then the code is deleted but never reused again.
Parallel to the development of these alternative language, the C language has remained stable over the years. What is known as modern C is the same what was used in the late 1980s as C90 standard. It is a standardized compiled language which is used to create production software for the public.
Roughly spoken, Basic and python are toy languages even they are used by 99% of the programmers. In contrast, C is practical language because the code is written once and then improved over decades. The chance that newly written Basic code can't be executed after 5 years is high, while the future of C is more secure. Created C code in now will be uptodate after 10 years from now for sure.
The obvious difference between python and C is, that python is easier to read. Similar to Pascal It is a great language for academia, but it fails for writing operating systems or even normal software. Basically spoken,the programming world is divided into two groups: c programmers and non c programmers. The non c community is much larger. Most books, tutorials and written lines of code are created in non c languages like:
Python, Basic, PHP, Ruby, C# and Java
But, non of these projects are real projects in a sense that the code is executed on a computer. But the code is written once for testing purposes and then it gets archived somewhere
A look into one of the existing C tutorials is disappointing. There is no explanation how to program games, build a database program or create simple hello world apps. But the C books are explaining how the c compiler generates Assembly code without any practical purpose. C has replaced the former assembly language in the late 1980s. During that time it was normal to write code in assembly language. This was done for unix systems and home computers as well. C has replaced this programming technique.
The malloc command within the c language explains very well what the difference is between C and non c language. Malloc allows to ask for memory block from the operating system. The programmer can ask for 1 GB of memory and then the newly written code will nned such amount of RAM for runtime. Such kind of low level memory occupation is not possible with alternative language or at least it doesn't make much sense. Because most modern programming languages have a garbage collector. In Python for example, the user doesn't specify the datatype at all but he appends something in a list and the rest is delegated to the python interpreter.
[1] Bradley L. Jones: Sams Teach Yourself C in 21 Days, 2002
[2] O'Reilly Practical C Programming

The development of the c language after the year 1992

 

Until the year 1992 the programming world was easy to grasp. The only programming language available was C which was working on all hardware e.g. 8bit homecomputer, 16bit intermediate computers and 32bit workstations and C was working on all operating systems like Amiga, MS-DOS, Unix. C was superior in terms of performance and lots of books were available to teach the language. There was no sort of comparison available between different languages, because all the programmers were using C and nothing but C.
After the year 1992 something has happened. At least in the public reception, the C language has felt out of fashion. There was the newly created Win31 operating system which was working with window libraries. There was a new language which was C++ and many new languages like Java and perl were initiated. C has become only one of many programming languages, especially for non expert programmers.
But let us slow down the situation a bit. In the time before the year 1992, the C language was indeed the best programming language. With so called cross compilers it was possible to run the same code on many systems. Other programming languages like BASiC were not compatible with C. There was no BASIC standard available and the performance was poor. What was different after the year 1992 was the upraising of GUI software namely the MFC library from microsoft. It seems, that the c language wasn't able to handle this added complexity. Instead the C++ language was prefered because C++ has a larger amount of built in functions and it is working much better with the GUI libraries.
For Unix and Linux operating system the importance of GUI software was lower. Most linux programs are console only applications which can be written in Ansi C very well. But if the objective is to create a GUI application in Linux, C can become a bottleneck. The well known Qt framework was written not in C but in C++. It seems, that object oriented programming and GUI software matches closely together.
But this connection isn't fixed. In ansi C it is pretty easy to register a signal event. A least in the gtk+ framework this can be used similar to C++. Also creating a window and menus is possible with Ansi C. So the question is, if C is capable to create Windows GUI, why was C replaced by alternative languages?
First thing to mention is, that the C language has felt out of fashion. Since the year 1992 all the major compilers are combining C with C++. Former C only compilers are no longer available. Also all the programming tutorials are oriented in the same way. It is explained in detail how to create GUI windows with C++ but doing the same with C is not explained.

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.