C++ tutorial: types


Data types reference
The table below shows the most commonly used data types available to C++ programmers. A complication is the the C++ standards do not define the ranges of numbers that can be represented by each type. They do specify that, for example, a 'long integer' must accept a range at least as large as a standard integer, if not larger.

It has to be admitted that this causes some problems when converting programs to run on different computers. These problems can easily be prevented if it is known from the outset that the program will have to run on different machines; but there are often difficulties when a large program has be converted and the issue of data type ranges was never considered at design time.

The figures given in the table are typical for compilers that run on '32 bit' systems, i.e., those where the processor's basic data registers are 32 bits wide. This applies to compilers for Microsoft Windows 95 and Windows NT, as well as most Unix systems. It does not apply to compiler for MS-DOS or Windows 3.1 or earlier. These systems use 16-bit data registers, so the ranges are generally smaller than the ones in this table.

bool data type
C++ has a 'bool' data type; variables of this type can strictly take only two values: 'true' or 'false'. However, the compiler will convert any non-zero value to 'true' and a zero value to 'false' if assigned to a boolean variable.

Integer numbers and characters
Name Use Bits used Range
char integer number; character 8 -128 to 127
unsigned char positive integer number; character 8 0 to 255
short integer number 16 -32768 to 32767
unsigned short positive integer number 16 0 to 65535
int integer number 32 -2,147,483,648 to 2,147,483,647
unsigned int positive integer number 32 0 to 4,294,967,295
long integer number 32 -2,147,483,648 to 2,147,483,647
unsigned long positive integer number 32 0 to 4,294,967,295

Real numbers
Name Use Bits used Range
float Real number 32 ±3.4x10 -38 to ±1.7x1038, with 7-digit precision
double Real number 64 ±1.7x10 -308 to ±3.4x10308, with 15-digit precision
long double Real number 80 ±3.4x10 -4932 to ±1.7x104932, with 18-digit precision

Notes
1. 'unsigned int' can be abbreviated to 'unsigned', and usually is.
2. 'char' variables are used to store characters, but they are actually numbers. Since computers represent each character by a number anyway, this is logial from an engineering point of view. From a computer science standpoint it is rather illogical. You can, if you wish, use 'char' variables to store small numbers. There are a few speclialized applications where this is appropriate, but usually it's more trouble than it's worth.
3. 'Signed' data types can store negative numbers while, in principle, 'unsigned' ones should not be so used. However, the instruction 'unsigned int x = -1' is unlikely to cause an error message from many compilers. The whole idea of signed and unsigned integers is rather shaky.
4. In practice, problems often arise when comparing signed and unsigned variables using '>' and '<'.
5. C++ has strict rules about what should happen when a program assigns a value from a data type to one with a bigger or smaller range. Very often something unexpected happens; use caution.
6. There is no 'text' or 'string' data type in C. C considers a string to be identical to an array of characters. This simple assumption, whether it is logically tru or not, has had far-reaching consequences. It makes certain C programs very fast and efficient, as the programmer has to manage trivial things like reserving memory space for strings. However, this is a major source of errors in C programs. C++ provides better mechanisms for modelling text strings but, sadly, they are non-standardized, so they aren't covered in this course.

©1994-2003 Kevin Boone, all rights reserved