Programming in D – Tutorial and Reference
Ali Çehreli

Other D Resources

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:

D's Fundamental Data Types
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 0L
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:

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:

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
Exercise

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.