Introduces you to the basic types and objects used by C++ programs.
Fundamental Types
Overview[*]
A program can use several data to solve a given problem, for
example, characters, integers, or floating-point numbers. Since a computer uses
different methods for processing and saving data, the data type must be known. The type defines
-
the internal representation of the data, and
-
the amount of memory to allocate.
A number such as -1000 can be stored in
either 2 or 4 bytes. When accessing the part of memory in which the number is
stored, it is important to read the correct number of bytes. Moreover, the
memory content, that is the bit sequence being read, must be interpreted
correctly as a signed integer.
The C++ compiler recognizes the fundamental
types, also referred to as built-in
types, shown on the opposite page, on which all other types (vectors,
pointers, classes, ...) are based.
The Type bool
The result of a comparison or a logical association using
AND or OR is a boolean value, which can be true or false.
C++ uses the bool type to represent boolean values. An
expression of the type bool can either be true or false, where the internal
value for true will be represented as the numerical
value 1 and false by a zero.
The char and wchar_t Types
These types are used for saving character codes. A character code is an integer associated with each character.
The letter A is represented by code 65, for example.
The character set defines which code represents a certain
character. When displaying characters on screen, the applicable character codes
are transmitted and the "receiver," that is the screen, is responsible for
correctly interpreting the codes.
The C++ language does not stipulate any particular characters set,
although in general a character set that contains the ASCII
code (American Standard Code for Information Interchange) is used. This 7-bit code contains definitions for 32
control characters (codes 0 – 31) and 96 printable characters (codes 32 –
127).
The char (character) type is used to
store character codes in one byte (8 bits). This amount of storage is sufficient
for extended character sets, for example, the ANSI character set that contains
the ASCII codes and additional characters such as German umlauts.
The wchar_t (wide character type) type
comprises at least 2 bytes (16 bits) and is thus capable of storing modern
Unicode characters. Unicode is a 16-bit code also used in
Windows NT and containing codes for approximately 35,000 characters in 24
languages.
Integral types
Sample program
#include <iostream> #include <climits> // Definition of INT_MIN, ... using namespace std; int main() { cout << "Range of types int and unsigned int" << endl << endl; cout << "Type Minimum Maximum" << endl << "--------------------------------------------" << endl; cout << "int " << INT_MIN << " " << INT_MAX << endl; cout << "unsigned int " << " 0 " << UINT_MAX << endl; return 0; }
Integral Types
The types short, int, and long are available for operations with integers. These types are distinguished by their ranges of values. The table on the opposite page shows the integer types, which are also referred to as integral types, with their typical storage requirements and ranges of values.
The int (integer) type is tailor-made for
computers and adapts to the length of a register on the computer. For 16-bit
computers, int is thus equivalent to short, whereas for 32-bit computers int will be equivalent to long.
C++ treats character codes just like normal integers. This means
you can perform calculations with variables belonging to the char or wchar_t types in exactly the
same way as with int type variables. char is an integral type with a size of one byte. The range
of values is thus –128 to +127 or from 0 to 255, depending on whether the
compiler interprets the char type as signed or
unsigned. This can vary in C++.
The wchar_t type is a further
integral type and is normally defined as unsigned
short.
The signed and unsigned Modifiers
The short, int, and long types are normally
interpreted as signed with the highest bit representing the sign. However,
integral types can be preceded by the keyword unsigned.
The amount of memory required remains unaltered but the range of values changes
due to the highest bit no longer being required as a sign. The keyword unsigned can be used as an abbreviation for unsigned int.
The char type is also normally
interpreted as signed. Since this is merely a convention and not mandatory, the
signed keyword is available. Thus three types are
available: char, signed char, and
unsigned char.
Note |
In ANSI C++ the size of integer types is not preset.
However, the following order applies:
char <= short <= int <= long
Moreover, the short type comprises
at least 2 bytes and the long type at least 4
bytes.
|
The current value ranges are available in the climits header file. This file defines constants such as
CHAR_MIN, CHAR_MAX, INT_MIN, and INT_MAX, which
represent the smallest and greatest possible values. The program on the opposite
page outputs the value of these constants for the int
and unsigned int types.
Floating-point types
Note |
IEEE format (IEEE = Institute of Electrical and Electronic Engineers) is normally used to represent floating-point types.
The table above makes use of this representation.
|
Arithmetic types
Note |
Arithmetic operators are defined for arithmetic types, i.e.
you can perform calculations with variables of this type.
|
Floating-Point Types
Numbers with a fraction part are indicated by a decimal
point in C++ and are referred to as floating-point numbers. In contrast to
integers, floating-point numbers must be stored to a preset accuracy. The
following three types are available for calculations involving floating-point
numbers:
float
|
for simple accuracy
|
double
|
for double accuracy
|
long double
|
for high accuracy
|
The value range and accuracy of a type are derived from the amount
of memory allocated and the internal representation of the type.
Accuracy is expressed in decimal places.
This means that "six decimal places" allows a programmer to store two
floating-point numbers that differ within the first six decimal places as
separate numbers. In reverse, there is no guarantee that the figures 12.3456 and
12.34561 will be distinguished when working to a accuracy of six decimal places.
And remember, it is not a question of the position of the decimal point, but
merely of the numerical sequence.
If it is important for your program to display floating-point
numbers with an accuracy supported by a particular machine, you should refer to
the values defined in the cfloat header file.
Readers interested in additional material on this subject
should refer to the Appendix, which contains a section on the representation of
binary numbers on computers for both integers and floating-point
numbers.
The sizeof Operator
The amount of memory needed to store an object of a certain
type can be ascertained using the sizeof operator:
sizeof(name)
yields the size of an object in bytes, and the parameter
name indicates the object type or the object itself.
For example, sizeof(int)represents a value of 2 or 4
depending on the machine. In contrast, sizeof(float)
will always equal 4.
Classification
The fundamental types in C++ are integer
types, floating-point types, and the void type. The types used for integers and floating-point
numbers are collectively referred to as arithmetic types,
as arithmetic operators are defined for them.
The void type is used for
expressions that do not represent a value. A function call can thus take a void type.
[*] without type void,
which will be introduced later.
0 Comments