Note: This website is archived. For up-to-date information about D projects and development, please visit

Declaring Variables

Part of TutorialFundamentals

In order to store information within a D program, you must declare variables before they are used. The syntax is type_name identifier. You can also use the auto keyword in place of a type, which means the type of the variable is implicitly inferred from the right hand side.

void main()
    int myInteger;
    double myDouble;
    bool myBit;
    string myString;

    auto anotherInt = 4;  // anotherInt has the type int
    auto someStrings = ["first", "second"];  // someStrings has the type string[]

Additionally, a variable can be marked as const or immutable, with the default being changeable (mutable).

void main()
    const int myInteger = 4;
    myInteger = 5; // Error, myInteger is constant

D1.x vs D2.x

In version 1 of D, 'string' is an alias for 'char[]' and in version 2 'string' is an alias for 'immutable(char)[]'.