Which data type is duplicate

Next:Variable attributesUp:Data types, variables, constants Previous:Representation of numbers

Basic data types and declaration

The basic built-in data types of C ++ are. These types of data are processed particularly efficiently by the computer.

The type is used for the results of comparison operations () used. He can own the values ​​or. Furthermore, it can be freely converted into the data type, where a 1 and a 0 correspond. This conversion makes upward compatibility with old programs that used 's instead of just' s. Some simple declarations are:

bool test; // declare test test = (0 <5); // comparison with result true // test is set to true int i (test); // i is declared and set to 1

The initialization / construction of a variable can be in the form type variable = type_constant or equivalent by type variable (type_constant) respectively. The equal sign at this point is not an assignment because the variable is just being brought into being and does not already exist in the program.

The type is the data type that the compiler writer perceives on the respective machine as being the most `` suitable '' for processing whole numbers. The standard does not specify anything about its size. With the help of the operator you can get its size in units of the size of one which is also the smallest data type. One was chosen by the compiler writer as the type suitable for single (character) characters. Different computers also support other integer data types, e.g. in addition to 32-bit also 16 or 64-bit large variables. These can be addressed by declarations of and or at all (not ANSI conform). After these key words may be missing. Note that an integer variable always has an integer value - even if a real number is assigned to it. Examples of Eger declarations are:

bool test; // declare test test = (0 <5); // (see above) int i (test); // i is declared and set to 1 int j = 0; // j is declared and set to 0 long l, n, m; // l, n, and m are declared "long" short s1, s2; // s1 and s2 are declared "short" cout << sizeof (test) << "" << sizeof (i) << "" << sizeof (l) << "" << sizeof (s1) << " \ n "; // check size of: test, i, l, s1 // Note: this is not guaranteed // on my machine I get: 8 4 8 2 // ANSI should give: 1 (for bool) i = 37; j = 3.5674; // ATTENTION cout << i << "" << j << "\ n"; // this leads to: 37 3

and are single and double precision floating point types that are useful for numeric purposes. can be used to modify to get quadruple precision (if supported). The exact floating point representation depends on the hardware support. Today (1998) it is common practice to carry out at least the basic arithmetic operations within the CPU in computer hardware for double-precision floating point numbers. To do this, single-precision numbers must first be converted into double-precision numbers. For this reason and the generally high requirements of numerical calculations, one should only use it where this is required by, for example, storage space restrictions.

C / C ++ does not specify anything about the actual representation of the variables in memory. In particular, the memory requirement of a variable is not defined, but the relationships always apply:


In general, on UNIX workstations, variables are 32, 16 and 8 bits long. Double-precision s require 64 bit, 32 bit. However, the real values ​​can in principle even differ from compiler to compiler!

The following examples declare different data types.

#include using std :: string; char c1 = 'x'; // one character: 'x' char c2 [] = "hello"; // text array with 5 characters // (note: length is 6 due to added '\ 0') string str ("hello"); // the C ++ way, arbitrary length strings // (note: length is 5 - no added '\ 0') char c3 = '\ n'; // the new-line character int i_var; // integer variable with name i_var long int il_var = 1L; // long constant short is_var; // short integer variable (int by default) double d_var = 34.2; // real number 34.2 with double precision In the case of variables, it should be noted that individual characters can be enclosed with a single, but longer character strings must be limited by a double. The variables and both have the length one, whereas the character string can be called up in the `` field '' and has the length six, since an end of the character string is automatically added. (The definition leads to an error message). The C ++ method is more flexible to use s that do not need the string terminator.

In C ++, variable declarations are not restricted to the beginning of a block (as in C), but can be made wherever the variable appears for the first time and where it can be assigned. The declaration within a loop is particularly important

double a; cin >> a; // input a via keyboard int i_trunc = a; // throw away the fractional part for (int i = 0; i Summary
(i) Declarations can be placed at almost any point in the program
(ii) initialization takes place in or form; it finds no assignment but an initialization instead!
(iii) can be used when the size of a type must be known in the program, this is interesting for portability between different computers.

Next:Variable attributesUp:Data types, variables, constants Previous:Representation of numbers& copy R.Hilfer et al., ICA-1, Univ. Stuttgart