📜  D编程-数据类型

📅  最后修改于: 2020-11-04 05:06:01             🧑  作者: Mango


在D编程语言中,数据类型是指用于声明不同类型的变量或函数的扩展系统。变量的类型决定了它在存储中占据多少空间以及如何解释所存储的位模式。

D中的类型可以分类如下-

Sr.No. Types & Description
1

Basic Types

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

2

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.

3

The type void

The type specifier void indicates 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.

数组类型和结构类型统称为集合类型。函数的类型指定函数的返回值的类型。我们将在下一节中看到基本类型,而其他类型将在接下来的章节中介绍。

整数类型

下表列出了标准整数类型及其存储大小和值范围-

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

要获取类型或变量的确切大小,可以使用sizeof运算符。表达式type。(sizeof)产生对象的存储大小或类型(以字节为单位)。以下示例获取任何机器上的int类型的大小-

import std.stdio; 
 
int main() { 
   writeln("Length in bytes: ", ulong.sizeof); 

   return 0; 
}

当您编译并执行上述程序时,它将产生以下结果-

Length in bytes: 8 

浮点类型

下表提到了标准浮点类型及其存储大小,值范围及其用途-

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

以下示例打印浮点类型及其范围值占用的存储空间-

import std.stdio;

int main() { 
   writeln("Length in bytes: ", float.sizeof); 

   return 0; 
}

当您编译并执行上述程序时,在Linux上会产生以下结果-

Length in bytes: 4

字符类型

下表列出了标准字符类型及其存储大小及其用途。

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

下面的示例显示char类型占用的存储空间。

import std.stdio;

int main() {
   writeln("Length in bytes: ", char.sizeof);
   
   return 0;
}

当您编译并执行上述程序时,它将产生以下结果-

Length in bytes: 1

虚空类型

void类型指定没有可用值。它在两种情况下使用-

Sr.No. Types & 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);

此时您可能还无法理解void类型,因此让我们继续,我们将在接下来的章节中介绍这些概念。