D-programming-data-types
Dプログラミング-データ型
Dプログラミング言語では、データ型は、異なる型の変数または関数を宣言するために使用される広範なシステムを指します。 変数のタイプによって、ストレージ内で占めるスペースの量と、保存されたビットパターンの解釈方法が決まります。
Dのタイプは次のように分類することができます-
Sr.No. | Types & Description |
---|---|
1 |
Basic Types これらは算術型であり、(a)整数、(b)浮動小数点、および(c)文字の3つの型で構成されます。 |
2 |
Enumerated types これらも算術型です。 これらは、プログラム全体で特定の離散整数値のみを割り当てることができる変数を定義するために使用されます。 |
3 |
The type void 型指定子_void_は、使用可能な値がないことを示します。 |
4 |
Derived types それらには、(a)ポインター型、(b)配列型、(c)構造体型、(d)ユニオン型、および(e)関数型が含まれます。 |
配列型と構造体型は、集合型と総称されます。 関数のタイプは、関数の戻り値のタイプを指定します。 次のセクションで基本的なタイプについて説明しますが、他のタイプについては今後の章で説明します。
整数型
次の表は、標準の整数型とそのストレージサイズと値の範囲を示しています-
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タイプは、使用可能な値がないことを指定します。 それは2種類の状況で使用されます-
Sr.No. | Types & Description |
---|---|
1 |
Function returns as void Dには値を返さないさまざまな関数がありますが、それらはvoidを返します。 戻り値のない関数の戻り値の型はvoidです。 たとえば、 void exit(int status); |
2 |
Function arguments as void Dには、パラメーターを受け入れないさまざまな関数があります。 パラメーターを持たない関数は、voidとして受け入れることができます。 たとえば、 int rand(void); |
現時点ではvoidタイプは理解できない可能性があるため、先に進みましょう。これらの概念については、今後の章で説明します。