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.
int和double足够大,如果愿意可以使用他们去表示所有的整型和浮点型。
The following are the terms that appeared in the table:
- Boolean: The type of logical expressions, having the value
truefor truth andfalsefor falsity. - Signed type: A type that can have negative and positive values. For example,
bytecan 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. The
uat 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
ithat 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.
4.1 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:
.stringofis the name of the type.sizeofis 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.).minis short for "minimum"; this is the smallest value that the type can have.maxis short for "maximum"; this is the largest value that the type can have.initis 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
4.2 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.
size_t是一种“动态”的类型,具体数据类型取决于用户的计算机系统。
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