Fundamental Types
We have seen that the brain of a computer is the CPU. Most of the tasks of a program are performed by the CPU and the rest are dispatched to other parts of the computer.
The smallest unit of data in a computer is called a bit. The value of a bit can be either 0 or 1.
Since a type of data that can hold only the values 0 and 1 would have very limited use, the CPU supports larger data types that are combinations of more than one bit. As an example, a byte usually consists of 8 bits. If an N-bit data type is the most efficient data type supported by a CPU, we consider it to be an N-bit CPU: as in 32-bit CPU, 64-bit CPU, etc.
The data types that the CPU supports are still not sufficient: they can't represent higher level concepts like name of a student or a playing card. Likewise, D's fundamental data types are not sufficient to represent many higher level concepts. Such concepts must be defined by the programmer as structs and classes, which we will see in later chapters.
D's fundamental types are very similar to the fundamental types of many other languages, as seen in the following table. The terms that appear in the table are explained below:
Type | Definition | Initial Value |
---|---|---|
bool | Boolean type | false |
byte | signed 8 bits | 0 |
ubyte | unsigned 8 bits | 0 |
short | signed 16 bits | 0 |
ushort | unsigned 16 bits | 0 |
int | signed 32 bits | 0 |
uint | unsigned 32 bits | 0 |
long | signed 64 bits | 0L |
ulong | unsigned 64 bits | 0LU |
float | 32-bit floating point | float.nan |
double | 64-bit floating point | double.nan |
real | either the largest floating point type that the hardware supports, or double; whichever is larger | real.nan |
ifloat | imaginary value type of float | float.nan * 1.0i |
idouble | imaginary value type of double | double.nan * 1.0i |
ireal | imaginary value type of real | real.nan * 1.0i |
cfloat | complex number type made of two floats | float.nan + float.nan * 1.0i |
cdouble | complex number type made of two doubles | double.nan + double.nan * 1.0i |
creal | complex number type made of two reals | real.nan + real.nan * 1.0i |
char | UTF-8 code unit | 0xFF |
wchar | UTF-16 code unit | 0xFFFF |
dchar | UTF-32 code unit and Unicode code point | 0x0000FFFF |
In addition to the above, the keyword void
represents having no type. The keywords cent
and ucent
are reserved for future use to represent signed and unsigned 128 bit values.
Unless there is a specific reason not to, you can use int
to represent whole values. To represent concepts that can have fractional values, consider double
.
The following are the terms that appeared in the table:
- Boolean: The type of logical expressions, having the value
true
for truth andfalse
for falsity. - Signed type: A type that can have negative and positive values. For example,
byte
can have values from -128 to 127. The names of these types come from the negative sign. - Unsigned type: A type that can have only positive values. For example,
ubyte
can have values from 0 to 255. Theu
at the beginning of the name of these types comes from unsigned. - Floating point: The type that can represent values with fractions as in 1.25. The precision of floating point calculations are directly related to the bit count of the type: higher the bit count, more precise the results are. Only floating point types can represent fractions; integer types like
int
can only represent whole values like 1 and 2. - Complex number type: The type that can represent the complex numbers of mathematics.
- Imaginary number type: The type that represents only the imaginary part of complex numbers. The
i
that appears in the Initial Value column is the square root of -1 in mathematics. - nan: Short for "not a number", representing invalid floating point value.
Properties of types
D types have properties. Properties are accessed with a dot after the name of the type. For example, the sizeof
property of int
is accessed as int.sizeof
. We will see only some of type properties in this chapter:
-
.stringof
is the name of the type -
.sizeof
is the length of the type in terms of bytes. (In order to determine the bit count, this value must be multiplied by 8, the number of bits in abyte
.) -
.min
is short for "minimum"; this is the smallest value that the type can have -
.max
is short for "maximum"; this is the largest value that the type can have -
.init
is short for "initial value" (default value); this is the value that D assigns to a type when an initial value is not specified
Here is a program that prints these properties for int
:
import std.stdio; void main() { writeln("Type : ", int.stringof); writeln("Length in bytes: ", int.sizeof); writeln("Minimum value : ", int.min); writeln("Maximum value : ", int.max); writeln("Initial value : ", int.init); }
The output of the program is the following:
Type : int Length in bytes: 4 Minimum value : -2147483648 Maximum value : 2147483647 Initial value : 0
size_t
You will come across the size_t
type as well. size_t
is not a separate type but an alias of an existing unsigned type. Its name comes from "size type". It is the most suitable type to represent concepts like size or count.
size_t
is large enough to represent the number of bytes of the memory that a program can potentially be using. Its actual size depends on the system: uint
on a 32-bit system and ulong
on a 64-bit system. For that reason, ulong
is larger than size_t
on a 32-bit system.
You can use the .stringof
property to see what size_t
is an alias of on your system:
import std.stdio; void main() { writeln(size_t.stringof); }
The output of the program is the following on my system:
ulong
Problem
Print the properties of other types.
Note: You can't use the reserved types cent
and ucent
in any program; and as an exception, void
does not have the properties .min
, .max
and .init
.
Additionally, the .min
property is deprecated for floating point types. (You can see all the various properties for the fundamental types in the D property specification). If you use a floating point type in this exercise, you would be warned by the compiler that .min
is not valid for that type. Instead, as we will see later in the Floating Point Types chapter, you must use the negative of the .max
property e.g. as -double.max
.