D Data Types - D Programming Language

What is D Data Types?

In the D programming language, data types mention to a wide system used for stating variables or functions of different types. The type of a variable governs how much space it occupies in storage and how the bit pattern stored is interpreted.

The types in D can be classified as follows:

S.N.

Types and Description

1

Basic Types:

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

2

Enumerated types:

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

3

The type void:

The type specifiervoidindicates 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 mentioned to together as the aggregate types. The type of a function agrees 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

Following table gives you details about standard integer types with its 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. Following is an example to get the size of int type on any machine:

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

Floating-Point Types

Following table gives you details about standard float-point types with storage sizes and 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

Following example will print storage space taken by a float type and its range values:

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

Character Types

Following table gives you details about 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

Following example will print storage space taken by a char type.


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

The void Type

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

S.N.

Types and 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 implicit to you at this point, so let us continue and we will cover these ideas in future sections.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

D Programming Language Topics