D Programming – Data Types

D Programming - Data Types

This topic is about 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
1Basic TypesThey are arithmetic types and consist of the three types: (a) integer, (b) floating-point, and (c) character.
2Enumerated typesThey are again arithmetic types. They are used to define variables that can only be assigned certain discrete integer values throughout the program.
3The type voidThe type specifier void indicates that no value is available.
4Derived typesThey 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 −

TypeStorage sizeValue range
bool1 bytefalse or true
byte1 byte-128 to 127
ubyte1 byte0 to 255
int4 bytes-2,147,483,648 to 2,147,483,647
uint4 bytes0 to 4,294,967,295
short2 bytes-32,768 to 32,767
ushort2 bytes0 to 65,535
long8 bytes-9223372036854775808 to 9223372036854775807
ulong8 bytes0 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 −

TypeStorage sizeValue rangePurpose
float4 bytes1.17549e-38 to 3.40282e+386 decimal places
double8 bytes2.22507e-308 to 1.79769e+30815 decimal places
real10 bytes3.3621e-4932 to 1.18973e+4932either the largest floating point type that the hardware supports, or double; whichever is larger
ifloat4 bytes1.17549e-38i to 3.40282e+38iimaginary value type of float
idouble8 bytes2.22507e-308i to 1.79769e+308iimaginary value type of double
ireal10 bytes3.3621e-4932 to 1.18973e+4932imaginary value type of real
cfloat8 bytes1.17549e-38+1.17549e-38i to 3.40282e+38+3.40282e+38icomplex number type made of two floats
cdouble16 bytes2.22507e-308+2.22507e-308i to 1.79769e+308+1.79769e+308icomplex number type made of two doubles
creal20 bytes3.3621e-4932+3.3621e-4932i to 1.18973e+4932+1.18973e+4932icomplex 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.

TypeStorage sizePurpose
char1 byteUTF-8 code unit
wchar2 bytesUTF-16 code unit
dchar4 bytesUTF-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
1Function returns as voidThere 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);
2Function arguments as voidThere 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.

In this topic we learned about D Programming – Data Types. To know more, Click Here.

This Post Has One Comment

Leave a Reply