Data types in C

from Wikipedia, the free encyclopedia

The article Data types in C describes the use of data types in the C programming language . In C, data types are declarations for memory addresses or variables that determine which operators and functions can be used on the addresses and variables.

C offers basic arithmetic data types for storing integers and floating point numbers , as well as the necessary syntax for creating fields and compound types . A number of header files in the C standard library also offer definitions of other data types, each of which has certain useful properties.

Basic arithmetic data types

C has four arithmetic data types char, int(both for integers ), floatand double(both for point numbers). The selection of one of these data types influences the size of the reserved memory and the size of the values ​​that can be displayed. In addition, different operators and functions are permitted for the various data types.

The waiver of fixed sizes and value ranges in order to support as many architectures as possible is mitigated by defined minimum value ranges and the following fixed relation:

signed charshort intintlong intlong long int.
("≤" means that the type on the right can accept all values ​​of the type on the left.)

Character

To store a character, C uses the data type Character , written as char. It is not the character that is actually stored by the computer, but an equivalent eight- bit binary number (this results in 256 different values ​​that can be assigned to a character). The programmer can easily have the computer translate the binary number into a character or a decimal integer. A number is translated into a character and vice versa using a table (e.g .: ASCII table or EBCDIC ).

char zeichen = 'A';    /* intern gespeichert wird nicht der Buchstabe „A“ sondern
                        * die anhand der ASCII-Tabelle errechnete Binärzahl „01000001“ */
printf("%d", zeichen); /* gibt „01000001“ als Dezimalzahl aus, also: „65“ */
printf("%c", zeichen); /* gibt „01000001“ als ASCII-Zeichen aus, also: „A“ */

A character represents the smallest addressable unit in C, usually eight bits. This is why the size of objects and types is often specified as an integer multiple of a character. Depending on the compiler, it can chareither be synonymous with signed char(-128 to 127, almost always the case) or with unsigned char(0 to 255). In order to be able to include characters from character sets that contain more characters than the relatively small ASCII character set, wchar_ta second data type conceived for characters was soon introduced. It has more than eight bits in almost all implementations.

Integer

To store an integer, use a variable of the integer data type , written as int. In order to reduce or enlarge the value range of an integer, one of the qualifiers short, longor is placed in long longfront of it. The key word intcan then also be omitted, so is longsynonymous with long int. To switch between signed and unsigned integers, there are two qualifiers signedand unsigned. For a signed integer, however, the qualifier can also be omitted, which is signed intequivalent to int.

char ganzzahl = 1;      /* mindestens 8 Bit, also 256 mögliche Werte */
short ganzzahl = 2;     /* mindestens 16 Bit, also 65536 mögliche Werte */
int ganzzahl = 3;       /* mindestens 16 Bit, also 65536 mögliche Werte */
long ganzzahl = 4;      /* mindestens 32 Bit, also 4294967296 mögliche Werte */
long long ganzzahl = 5; /* mindestens 64 Bit, also 18446744073709551616 mögliche Werte */

The size of an integer depends on the respective compiler, but the C standard guarantees a minimum size of 16 bits. The actual size is nowadays (depending on the processor architecture and operating system) mostly 32 bits, but often 64 and sometimes 16 bits. 65536 different values ​​can be saved in 16 bits. To enable the use of negative numbers, the value range for 16 bits is usually from -32768 to 32767. If negative numbers are not required, the programmer unsigned intcan use an unsigned integer with. With 16-bit integers, this results in a value range from 0 to 65535.

The actual size of an integer is <limits.h>stored in the header file . INT_MAXthe C preprocessor replaces, for example, the intmaximum value that the type can assume.

The C standard library supplements these data types via the platform-independent header file <stdint.h>in which a wealth of integer types with a fixed length is defined.

float, double and long double

Numbers with decimal places are stored in one of the three data types float, doubleand long double.

float kommazahl = 0.000001f;           /* Genauigkeit ist implementierungsabhängig */
double kommazahl = 0.000000000000002; /* Genauigkeit ist implementierungsabhängig */
long double kommazahl = 0.3l;          /* Genauigkeit ist implementierungsabhängig */

Accuracy does not mean the number of significant decimal places - rather the standard with FLT_DIG, DBL_DIG, LDBL_DIG only specifies the number of (directly following) significant decimal digits as an accuracy criterion, and at least 6/10/10 in each case. The standard does not specify the position of this sequence of digits (before comma, after comma, divided by comma).

In most C implementations, the data types float and double correspond to the internationally valid standard for binary floating point arithmetic (IEC 559, which emerged in 1989 from the older American standard IEEE 754 ). Under this assumption, float implements the “ single long format ”, a double the “ double long format ”. A float comprises 32 bits and a double 64 bits. so doubles are more accurate. Due to this fact, floats are only used in special cases. The size of long doubles varies depending on the implementation, but a long double must never be smaller than a double.

The exact properties and value ranges on the architecture used can be determined via the header file <float.h>.

Complex numbers

In addition, since C99 three floating-point data types exist complex numbers , which are derived from the three floating-point types: float _Complex, double _Complexand long double _Complex. Floating-point data types have also been introduced in C99 for purely imaginary numbers: float _Imaginary, double _Imaginaryand long double _Imaginary.

In a hosted environment, the _Complexdata types must be present; the _Imaginarytypes are optional. In a freestanding environment, these six data types are optional.

bool

Until the C99 standard, there was no data type for storing a truth value . Only since 1999 can variables be _Booldeclared as and accept one of the two values ​​0 (false) or 1 (true). The size of a _Boolvariable depends on the platform and can exceed 8 bits. If the header stdbool.his included, the alias can also be used boolinstead _Bool, as well as falseand trueinstead of 0 and 1.

_Bool a = 23;   /* Alle zugewiesenen Werte ungleich 0, werden von einer _Bool-Variablen als 1 gespeichert */

#include <stdbool.h>
bool b = false;

void

The data type voidis referred to as "incomplete type" in the C standard. You cannot create variables of this type. It is used voidfirstly when a function should not return a value, secondly for the declarations of an empty parameter list for a function and thirdly as part of the regular but anonymous data pointer type void *, which can accept pointers of all data types (no functions).

void funktionsname();      /* Deklaration einer Funktion, die keinen Wert zurückgibt */
void* zeigername;          /* Zeiger auf ein Objekt von beliebigem Typ */

Data model

The C language standard does not specify the size (and thus the range of values) of the individual basic data types, but only defines relationships between the sizes of the basic data types and requires minimum sizes for each basic data type. In practice, this results in several design options, which are called data models or programming models .

The data type intis usually defined on a platform so that its size corresponds to the natural data word size of the CPU. The size of the pointer types depends on the size of the memory area that is to be addressable by the program. This memory area can be smaller or larger than the memory area that can be addressed by the CPU architecture.

On today's architectures, one is charusually 8 bits, the other data types must therefore be an integer multiple of 8 bits. This results in the following possible data models:

Bits per data type
Data model Data type Platforms (selection)
char short int long long long void*
IP16 8th 16 16 32 64 16 MS-DOS in the SMALLmemory model
LP32 8th 16 16 32 64 32 MS-DOS in the LARGEmemory model
ILP32 8th 16 32 32 64 32 most 32-bit operating systems
LLP64 8th 16 32 32 64 64 Windows on x86-64 and IA64
LP64 8th 16 32 64 64 64 most Unix operating systems on 64-bit platforms
ILP64 8th 16 64 64 64 64 SPARC64
SILP64 8th 64 64 64 64 64 Unicos (Cray)

Web links