Showing posts with label Ansi_C. Show all posts
Showing posts with label Ansi_C. Show all posts

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

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.

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/

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

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.