Values and Type System

This page was translated by a robot.

The main task of programming is the manipulation of values. In the C and C++ languages, in order to be able to manipulate values, all values ​​must be assigned a unique type that determines the structural interpretation of the underlying data. The compiler can use this type to convert the program code into the appropriate assembler commands. This concept, known as typing , is a fundamental, powerful, and far-reaching concept of the C and C++ languages.

The C and C++ languages ​​specify a small number of base types and the ability to nest types within one another, allowing the creation of any complex type. All possible types can be read on the types page .

Details

Assembler works with data, while C and C++ work with values. Each value has a dedicated type that determines the structural interpretation of the underlying data. Thus, the programmer no longer needs to worry about storing the data, but can trust that the manipulations of values ​​will be translated into the desired assembler commands by the compiler based on the knowledge of the type.

There are three different types of values ​​in C and C++: A fixed value in the program code (a so-called literal ), the content of a variable and the return value of an operator:

Fixed value
Variable containing a value
Operator returning a value
3.14159;
int x = 42;
x + x;

Fixed values ​​are written directly into the program code and implicitly specify a type because of the syntax (how they are written). These values ​​are already encoded into the program during compilation and can then no longer be addressed by the programmer, let alone changed. The notation of such values ​​is described in more detail on the fixed values ​​page .

Variables denote a location in memory or in a processor register that can be addressed and changed by the programmer. In order to address this position, a symbol is set as a placeholder for the searched place, i.e. a user-defined name. When this symbol appears in the program text, the value of the variable is automatically addressed. A type must be assigned to each symbol so that the compiler knows how to interpret the data at the point to be addressed when a symbol occurs. This assignment is called a declaration .

Operators allow values ​​to be linked together. Each operator specifies what input values ​​it requires and what return value it returns. For example, the addition operator takes two input values ​​and returns one value. The type of return value is precisely specified for each operator. The detailed specifications can be found at the respective operators . The return values ​​can be used by the programmer in expressions to sequence multiple operations, nest them within one another, or ultimately store the return value in a variable.

Note that functions can also return a value. However, in C and C++, function calls are handled with the function call operator .

Designations of Typifications

Most modern programming languages ​​have some form of typing. A language that requires explicit statements for conversions between different types is called strongly typed ( also strong typing ). In contrast , typings that allow automatic conversions and, for example, dynamic evaluations of types, are referred to as weakly typed .

Furthermore, there is a second description of typing. Programming languages ​​that require a type for every value are called strictly typed . On the other hand, there are programming languages ​​that automatically determine the type of a value (like that of a variable) or even change it dynamically without the programmer ever having to specify a type. Such languages ​​are referred to as loosely typed .

All these English and German terms are very confusing. They don't really matter, however, as long as programming is only in one language. When porting between different typed languages, however, such keywords are inevitably thrown around, which is why the author tries to classify C and C++ in these terms:

The programming languages ​​C and C++ are strictly typed, which means that every value has a permanently declared (static) type. However, various sources disagree on the question of whether C and C++ are strongly or weakly typed. The author personally tends towards the weak variant, because: The languages ​​C and C++ allow a value (more or less) to be reinterpreted as desired, if desired. For example, it is possible to interpret a floatvalue as a intvalue ( arithmetic conversion ), cast that value as an address, and cast the content of the address as an object of a polymorphic classto address This example doesn't make much sense, but there are situations where weak-typing is used explicitly, for example when calculating array indices. However, since such conversions almost always have to be carried out explicitly and modern compilers usually issue warnings or even errors if type conversions do not make sense, the languages ​​C and especially C++ can also be described as strongly typed. However, warnings can usually be removed by explicitly specifying what the programmer wants

Next Chapter: Variables and Functions