C Language

Comprehensive Guide to C Language

What is C?

The C programming language has been around since the early 1970s. This language is still up-to-date, quick and universally applicable. Their area of ​​application is constantly expanding and extends from the normal user PC through the embedded system within a technical device to the networked computer network. The current C standard is called C11 and was introduced in 2011. Many well-known and widely used programming languages ​​have been developed on the basis of C, including C ++, Java, PHP, C # and JavaScript.

What are the advantages of C?

Here are some of the advantages of C:

  • Portability: The C language has a wide range of uses and is very well known and popular. C programs are easily portable between the various computer systems. You do not need a special runtime environment or an interpreter. You can use C to create fast, resource-efficient programs. This is thanks, among other things, to the direct access to the storage space and the hardware.
  • Independence: C is widespread and does not bind you to a specific manufacturer. The C compiler from the GNU Compiler Collection (GCC C Compiler for short) is available for many operating systems and represents the central element of many development environments.
  • Simplicity: Due to its simple structure and its small number of keywords, C is easy to learn. Its efficiency has made C the basis of many well-known languages. C is used both for programming applications for the end user and for system programming for components and extensions of operating systems.

What do I need for programming?

For the common operating systems (Windows, Ubuntu Linux, macOS) there are a number of IDEs (Integrated Development Environment) that offer you everything you need as a budding C programmer:

  • An editor with the help of which you write the program code,
  • a compiler, with the help of which you translate the program code into the language that the respective computer understands and
  • a debugger to help you troubleshoot.

Take one of the freely available IDEs such as Eclipse, NetBeans or the clear, easy-to-use Code :: Blocks. All IDEs mentioned are available under Windows as well as Linux or macOS.

But you can also work with a simple text editor and a C compiler at the command line level. This partially simplifies the use of external libraries that offer you additional functions. Many simple text editors already offer so-called syntax highlighting, i.e. a color highlighting of the elements of C.

Pointers and dynamic fields

One of the special features of C is its pointers, which, among other things, allow you to create dynamic fields. Pointers access addresses in the main memory directly and can therefore be used to develop very fast programs. However, it is easy for developers to make serious programming errors when working with pointers. Pointers are often the cause of memory access violations, which in turn can lead to crashes.

Larger amounts of data are stored in fields. Static fields are easy to declare, but have a fixed, unchangeable size. However, fields are often used for different amounts of data. In this case you have to make sure that the field is not too small. Well, you can agree on a very large field. This in turn can be too large for many cases, it can lead to a waste of memory and a reduction in performance.

Dynamic fields offer a solution to this problem. Their size is not determined at the time of development, but rather at the time of execution. You can adjust their size according to the size of an input file, according to the wishes of the user or according to other requirements. Your declaration is a little more complex. However, access is the same as for a static field. So: The little extra effort at the beginning is worth it!

An example program

In this section follows an example using a dynamic field. If you have no programming experience yet, this example should illustrate a typical process:

  • The creation of a dynamic two-dimensional field,
  • filling the field with data, for example a set of random values,
  • the clear output of this data and
  • the release of the dynamic field so that its storage space is available again afterwards.

If you already have knowledge of another programming language, you will find many familiar elements in the code and the short comments, but also many elements that are specific to C. Of course, this demo example is not yet suitable for a thorough learning of C. At this point I refer to my book at Rheinwerk-Verlag .

One possible call of the program: The following is the program code in the “dyn.c” file:
Anzahl der Zeilen: 8
Anzahl der Spalten: 4
9451      14662      15494      22146
12106      15082      30606      32457
24212       6126      30241      16245
28578      21200      17810      29211
4354      28155      29808      27040
3669      31132      14600       3756
19188      13233      24875       8284
20442      28258      28466      27959

include <stdlib.h>
#include <time.h>

/* Deklaration der Funktionen für das Hauptprogramm */
int **erzeugen(int *pz, int *ps);
void fuellen(int **pb, int zeilen, int spalten);
void ausgeben(int **pb, int zeilen, int spalten);
void freigeben(int **pb, int zeilen);

/* Hauptprogramm */
int main()
{
/* Zeiger für das dynamische Feld */
int **pa;

/* Gewünschte Anzahl der Zeilen und Spalten */
int zeilen, spalten;

/* Initialisierung des Zufallsgenerators */
srand((unsigned)time(NULL));

/* Erzeugen des dynamischen Felds, mit Prüfung */
pa = erzeugen(&zeilen, &spalten);
if(pa == NULL) return 1;

/* Füllen des dynamischen Felds mit zufälligen Werten */
fuellen(pa, zeilen, spalten);

/* Ausgabe aller Werte */
ausgeben(pa, zeilen, spalten);

/* Freigabe des dynamischen Felds */
freigeben(pa, zeilen);

return 0;
}

/* Definition der Funktion zum Erzeugen des Felds */
int **erzeugen(int *pz, int *ps)
{
int **pb, i;

/* Gewünschte Größe des Felds */
printf(“Anzahl der Zeilen: “);
scanf(“%d”, pz);
printf(“Anzahl der Spalten: “);
scanf(“%d”, ps);

/* Allokierung des Felds, Stufe 1, mit Prüfung */
pb = (int **) malloc(*pz * sizeof(int *));
if(pb == NULL) { printf(“Speicherfehler”); return NULL; }

/* Allokierung des Felds, Stufe 2, mit Prüfung */
for(i=0; i<*pz; i++)
{
pb[i] = (int *) malloc(*ps * sizeof(int));
if(pb[i] == NULL) { printf(“Speicherfehler”); return NULL; }
}

/* Rückgabe des Zeigers */
return pb;
}

/* Definition der Funktion zum Füllen des Felds */
void fuellen(int **pb, int zeilen, int spalten)
{
int i, k;
for(i=0; i<zeilen; i++)
for(k=0; k<spalten; k++)
/* Aufruf des Zufallsgenerators für jedes Feldelement */
pb[i][k] = rand();
}

/* Definition der Funktion zur Ausgabe aller Werte */
void ausgeben(int **pb, int zeilen, int spalten)
{
int i, k;
for(i=0; i<zeilen; i++)
{
for(k=0; k<spalten; k++)
/* Ausgabe jedes Feldelements */
printf(“%10d “, pb[i][k]);
printf(“\n”);
}
}

/* Definition der Funktion zum Freigeben des Felds */
void freigeben(int **pb, int zeilen)
{
int i;
for(i=0; i<zeilen; i++)
free(pb[i]);
free(pb);
}

C Language

About the author