Type | Description |
---|---|
bool
|
Boolean variable, can only be true or false. The C++ keywords true and false correspond to these values. |
char
|
Character. Almost always 8 bits wide. |
wchar_t
|
Wide character: used to support both Roman and Oriental languages typically 16 or 32 bits wide. |
float
|
Floating point number. |
double
|
Like float, but greater precision (also larger and sometimes slower, depending on the systems floating-point capability) |
Int
|
Integer value. Sizes vary according to system: typically 16 or 32 bits but can be larger. |
void
|
Nothing |
signed
|
(with int or char) value can be interpreted as positive or negative doubles and floats are inherently signed, and so do not require the signed keyword. |
unsigned
|
(with int or char value will never be interpreted as negative. Doubles and floats cannot be declared unsigned |
short
|
(with int) smaller value of int (on most systems short int is a 16 bit integer) |
long
|
(with int) Larger value of int (on most systems long int is a 32 bit integer) (With double) Higher precision than double. Not always supported. Some compilers allow a type called long long int, which is 64 bits wide (when long int is 32 bits). If the signed or unsigned, short or long modifiers are used in a declaration statement without specifying a basic type, then int is assumed. (eg.short I; declares a short int). |
If the signed or unsigned, short or long modifiers are used in a declaration statement without specifying a basic type, then int is assumed. (eg.short I; declares a short int). | |
The behaviour of these types can be controlled with the following keywords: | |
const
|
Indicates that the value of the objects is constant, and cannot be modified the compiler will report an error if a program attempts to assign a new value to a const object. |
mutable
|
In a class definition, indicates that the particular class member may change, even when the class instance itself is declared as const. Although useful in some situations, the need to declare lots of class members as mutable can indicate a design flaw in the class. |
static
|
The value of a static object will stay constant between different calls to the function in which it is declared. In this respect , static objects are like global variables, but unlike global variables, they are only visible within the scope which they were declared. |
auto
|
Opposite of static. An auto (automatic) object is created when it is declared, and destroyed when it goes out of scope, unless otherwise stated (by using the static keyword), all objects are interpreted as being auto. |
volatile
|
Seldom used. A volatile item is one whose value may be changed behind the programs back. Usually only used when dealing with hardware registers. |
These modifiers can be used in conjunction with each other, so it is possible to declare an object as both const and volatile, for example, the const keyword is used extensively in function declarations to prevent a function from inadvertently changing one of its arguments. Wherever the design of a system, allows objects and function arguments should be declared as const. This can allow the compiler to perform some time and space-saving optimisations, as well as trapping potentially dangerous assignments to objects which should not be modified.