D Programming/Types

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

To be completed.

TODO

TODO
a) create a table for data types b) explain them.

Contents

[edit] D Data Types

If you look directly at the content of a computer's memory, you see only a lot of bytes. These are often shown as numbers but in fact they don't have a meaning on its own, not even the meaning of a number.

Types are used in computer languages like D to give one byte or a sequence of bytes a meaning. Types define if these bytes are a number, a text, a date or whatever.


[edit] Basic Types

[edit] Void Type

  • void

[edit] Boolean Type

  • bool

[edit] Integer Types

Provided types are:

  • byte
  • ubyte
  • short
  • ushort
  • int
  • uint
  • long
  • ulong
  • cent (reserved for future use)
  • ucent (reserved for future use)


Each type has a size in bits which defines how many different values(=states) it can hold.

An 8 bit type, for instance, can store 256 different states, while a 16 bit type can store 65536.


The sizes of the types are:

Name Size in Bits Min Max Comment
byte 8 -128 +128
short 16 -256 +256
int 32 -65536 +65536
long 64 -4294967296 +4294967296
cent 128 -1,84E+019 +1,84E+019


Each type exists as signed and unsigned variant. The unsigned ones have an "u" prefix (like uint, ubyte).

Unsigned means that the range of possible states is interpreted as nonnegative values starting at 0 and going up to the maximum number of possible states for this type minus 1 because the 0 is also a state which must be stored.

For the ubyte type (8 bit = 256 states) this means it can store values from 0 up to and including 255.

Signed types interpret one half of their states as positive, the other half as negative values, so the signed byte type has a range of values from -128 to +127.

[edit] Floating-Point Types

  • float
  • double
  • real
  • ifloat
  • idouble
  • ireal
  • cfloat
  • cdouble
  • creal

[edit] Character Types

  • char
  • wchar
  • dchar

[edit] Derived Types

  • pointer
  • array
  • associative array
  • function
  • delegate

[edit] User Defined Types

  • alias
  • typedef
  • enum
  • struct
  • union
  • class
Personal tools