Tuesday, 20 May 2014

D - Data Types

Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

D - 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 bit pattern stored is interpreted.
The types in D can be classified as follows:
S.N.Types and Description
1Basic Types:
They are arithmetic types and consist of the three types: (a) integer types , (b) floating-point types (c) character types
2Enumerated 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.
3The type void:
The type specifier void indicates that no value is available.
4Derived 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

Following table gives you details about standard integer types with its 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. Following is an example to get 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

Following table gives you details about standard float-point types with storage sizes and 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
Following example will print 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:
Storage size for float : 4 

Character Types

Following table gives you details about 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
Following example will print 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:
Storage size for float : 1 

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
1Function 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);
2Function 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.

0 comments: