D Programming - Data Types



In the D programming language, data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the stored bit pattern is interpreted.

The types in D can be classified as follows −

Sr.No. Types & Description
1

Basic Types

They are arithmetic types and consist of the three types: (a) integer, (b) floating-point, and (c) character.

2

Enumerated types

They are again arithmetic types. They are used to define variables that can only be assigned certain discrete integer values throughout the program.

3

The type void

The type specifier void indicates that no value is available.

4

Derived types

They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types, and (e) Function types.

The array types and structure types are referred to collectively as the aggregate types. The type of a function specifies the type of the function's return value. We will see basic types in the following section whereas other types will be covered in the upcoming chapters.

Integer Types

The following table gives lists standard integer types with their storage sizes and value ranges −

Type Storage size Value range
bool 1 byte false or true
byte 1 byte -128 to 127
ubyte 1 byte 0 to 255
int 4 bytes -2,147,483,648 to 2,147,483,647
uint 4 bytes 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
ushort 2 bytes 0 to 65,535
long 8 bytes -9223372036854775808 to 9223372036854775807
ulong 8 bytes 0 to 18446744073709551615

To get the exact size of a type or a variable, you can use the sizeof operator. The expression type.(sizeof) yields the storage size of the object or type in bytes. The following example gets the size of int type on any machine −

import std.stdio; 
 
int main() { 
   writeln("Length in bytes: ", ulong.sizeof); 

   return 0; 
}

When you compile and execute the above program, it produces the following result −

Length in bytes: 8 

Floating-Point Types

The following table mentions standard float-point types with storage sizes, value ranges, and their purpose −

Type Storage size Value range Purpose
float 4 bytes 1.17549e-38 to 3.40282e+38 6 decimal places
double 8 bytes 2.22507e-308 to 1.79769e+308 15 decimal places
real 10 bytes 3.3621e-4932 to 1.18973e+4932 either the largest floating point type that the hardware supports, or double; whichever is larger
ifloat 4 bytes 1.17549e-38i to 3.40282e+38i imaginary value type of float
idouble 8 bytes 2.22507e-308i to 1.79769e+308i imaginary value type of double
ireal 10 bytes 3.3621e-4932 to 1.18973e+4932 imaginary value type of real
cfloat 8 bytes 1.17549e-38+1.17549e-38i to 3.40282e+38+3.40282e+38i complex number type made of two floats
cdouble 16 bytes 2.22507e-308+2.22507e-308i to 1.79769e+308+1.79769e+308i complex number type made of two doubles
creal 20 bytes 3.3621e-4932+3.3621e-4932i to 1.18973e+4932+1.18973e+4932i complex number type made of two reals

The following example prints storage space taken by a float type and its range values −

import std.stdio;

int main() { 
   writeln("Length in bytes: ", float.sizeof); 

   return 0; 
}

When you compile and execute the above program, it produces the following result on Linux −

Length in bytes: 4

Character Types

The following table lists standard character types with storage sizes and its purpose.

Type Storage size Purpose
char 1 byte UTF-8 code unit
wchar 2 bytes UTF-16 code unit
dchar 4 bytes UTF-32 code unit and Unicode code point

The following example prints storage space taken by a char type.

import std.stdio;

int main() {
   writeln("Length in bytes: ", char.sizeof);
   
   return 0;
}

When you compile and execute the above program, it produces the following result −

Length in bytes: 1

The void Type

The void type specifies that no value is available. It is used in two kinds of situations −

Sr.No. Types & Description
1

Function returns as void

There are various functions in D which do not return value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);

2

Function arguments as void

There are various functions in D which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void);

The void type may not be understood to you at this point, so let us proceed and we will cover these concepts in upcoming chapters.

Advertisements