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 −
Basic Types
They are arithmetic types and consist of the three types: (a) integer, (b) floating-point, and (c) character.
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.
The type void
The type specifier void indicates that no value is available.
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.
The following table gives lists standard integer types with their storage sizes and value ranges −
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 

The following table mentions standard float-point types with storage sizes, value ranges, and their purpose −
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

The following table lists standard character types with storage sizes and its purpose.
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 specifies that no value is available. It is used in two kinds of situations −
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);
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.

    10 Lectures 
     1.5 hours 

 Nishant Malik

    42 Lectures 
     1.5 hours 

 Ravi Kiran

    46 Lectures 
     7.5 hours 

 Aditya Dua

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    14 Lectures 
     1 hours 

 Mahesh Kumar

    18 Lectures 
     49 mins

 Rushi Panchal
 Print
 Add Notes
 Bookmark this page