What are the common properties of data types in D Programming?

Introduction

All variables in the D programming language must be of a particular data type. All data types have similar properties, for example, size, minimum value, maximum value, and so on.

Accessing the properties

We can access the properties of each of these data types with a dot after the name of the type, followed by the name of the property to access.

For example, the sizeof property of char data type is accessed as char.sizeof.

Type properties

Let’s check out some of these properties below:

  • .stringof: Returns the name of the data type as a string.
  • .sizeof: Returns the bytes length of a particular data type. To know the bit count, multiply the returned value by 8, the number of bits in a byte.
  • .min: Stands for the minimum. And it shows the smallest value that a data type can have.
  • .max: Stands for the maximum. And it indicates the largest value that a data type can hold.
  • .init: Means the initial value. The default value D will assign to a data type if an initial value is not provided.

Code

Below is a code snippet that prints all properties of the data type int:

import std.stdio;
void main() {
/*details for data type int*/
writeln("Type : ", int.stringof);
writeln("Length in bytes: ", int.sizeof);
writeln("Minimum value : ", int.min);
writeln("Maximum value : ", int.max);
writeln("Initial value : ", int.init);
}

Explanation

  • Line 1: We import the stdio module to print to display.
  • Line 3: We start the void main program wrapper function, and it ends on line 11.
  • Lines 6 to 10: The properties of int data type are returned and displayed using the writeln function.

Free Resources