Let’s learn D – #2: Variables

#2 Programming in D - Let's learn D

What is a variable?

In programming, a variable is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value; this separation of name and content allows the name to be used independently of the exact information it represent.

Each variable in D has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because D is case-sensitive.

Basic Data Types

There are following some basic types of variables in D:

Keyword Default Initializer
void no type
bool false boolean value
byte 0 signed 8 bits
int 0 signed 32 bits
float float.nan 32 bit floating point
double double.nan 64 bit floating point
real real.nan largest FP size implemented in hardware (Implementation Note: 80 bits for x86 CPUs or double size, whichever is larger)
char 0xFF unsigned 8 bit (UTF-8 code unit)

For more data types, click here.

Variable Definition

A variable definition means to tell the compiler where and how much to create the storage for the variable. A variable definition specifies a data type, and contains a list of one or more variables of that type as follows this syntax:

type variable_name;
  • type: this need to be a valid data type. It can be predefined or user defined;
  • variable_name: the name of the variable. it must start with a letter or an underscore.


int integer, i, n;
char c, ch;

Variable Declaration

A variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable. A variable declaration has its meaning at the time of compilation only, compiler needs actual variable definition at the time of linking of the program.


int i = 42;
int n;
n = 33;
char ch = 'f';



To see how variables works in action within a program, let’s have a look at the entire D code of the example about what you learned in this tutorial:

import std.stdio;

int i = 1;

int main(string[] args)
    char ch = 'a';
    int n;
    n = 2;

  • Ev1lbl0w

    Good tutorial! Will you make a Java one too?