Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 36 ยท Getting Started

Introduction to the D Programming Language

Meet D, a compiled systems language with C-like speed, garbage collection, templates and compile-time execution. See what it is for and how this course teaches it.

D is a general-purpose, compiled programming language created by Walter Bright, who also wrote one of the first native C++ compilers. Its first stable release appeared in 2007 and the language has been developed in the open ever since. The goal was simple to state and hard to achieve: keep the speed and low-level control of C and C++, but remove the sharp edges and add the comforts of modern languages such as garbage collection, built-in arrays and strings, lambdas, and strong metaprogramming.

If you know C, much of D will look familiar: curly braces, for loops, pointers, structs and the same basic types. If you know Java or C#, you will recognize classes, interfaces, exceptions and a garbage collector. D lets you mix those styles in one program and choose, function by function, how close to the metal you want to be.

What makes D different

Four ideas set D apart from most compiled languages:

  • Fast native code. D compiles ahead of time to machine code. The LLVM-based compiler ldc2 and the GCC-based gdc produce code in the same league as C and C++.
  • Memory you can choose to manage. By default a garbage collector frees memory for you. When you need predictable timing, you can switch it off for a function with @nogc, use malloc/free yourself, or use scoped and reference-counted types.
  • Compile-time power. Ordinary D functions can run while the compiler works (CTFE, compile-time function execution), and templates, static if and mixins let a program write parts of itself. That is the reason a whole chapter of this course is about mixins and metaprogramming.
  • Productive built-ins. Dynamic arrays with slicing, associative arrays (hash maps), Unicode strings and ranges are part of the language and the standard library, Phobos.

D at a glance

Native speed Compiled to machine code
Optional GC Collect, or manage by hand
Templates Generic code, checked at compile time
C interop Call C libraries directly

One language, several styles

1 / 2

A first look at D code

Here is a complete D program. It counts how many numbers from 1 to 20 are divisible by 3 and prints the result:

D
import std.stdio;

void main()
{
    int count = 0;
    foreach (n; 1 .. 21)
    {
        if (n % 3 == 0)
        {
            ++count;
        }
    }
    writeln("Multiples of 3: ", count);
}

It prints Multiples of 3: 6. Notice import std.stdio; (D's way of including the standard I/O module), void main() as the entry point, foreach over the range 1 .. 21 (the upper bound is exclusive, like a half-open interval), and writeln for printing. There are no header files and no semicolon after a function body.

How this course teaches D

The site cannot run a D compiler in your browser or on its servers. Every D listing in this course is read-only. To keep you practicing, each lesson also contains runnable C stand-ins: small C programs on our C runner that model the same idea (what a slice really is, how a hash table works, how a lazy range yields values). They are labelled "C stand-in" every time, and they are not D. To run real D, install a compiler as described in Installing D and your first program.

Outputs are shown for D code only when they follow directly from the language rules. For the stand-ins, you can press Run and see the real output.

Here is the first stand-in. It does the same job as the D program above, in C, so you can compare the two shapes:

C C stand-in: counting multiples of 3

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The C loop has to spell out the start, the stop test and the step; the D foreach states only the range. That small gain in clarity repeats throughout the language.

D, C, C++ and Rust in one picture

flowchart LR
    C["C: tiny, manual memory"] --> CPP["C++: huge, manual + RAII"]
    C --> D["D: C-like core, GC by default, strong templates"]
    CPP --> D
    D --> Both["Choose per function: GC, @nogc or manual"]

D borrows from both C and C++ but aims for a smaller, more regular language.

D is not a drop-in replacement for either. Its ecosystem (the package registry is code.dlang.org, the build tool is dub) is smaller than C++'s, and fewer jobs list it. It shines for tools where you want C-class speed with a pleasant syntax, for projects that call into C libraries, and for programmers who enjoy compile-time metaprogramming.

What you will learn

This course has nine chapters. You will start with installing and building, then types and operators, control flow and functions, and arrays and ranges. After that come classes and templates, mixins and metaprogramming, concurrency and memory, tooling and C interop, and finally projects and practice.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement about D's memory model is true?

    D has a collector by default, and you can opt out per function or manage memory yourself.
  2. 2. What does foreach (n; 1 .. 21) iterate over?

    The right bound of a D range is exclusive, so the loop sees 1 through 20.
  3. 3. Why does this course use C programs next to the D code?

    No D compiler runs on the site; the stand-ins are runnable models, not D.
  4. 4. Which is a headline feature of D?

    CTFE lets ordinary functions run while the compiler works.

Summary

  • D is a compiled systems language with C-like speed and modern conveniences.
  • It has a garbage collector by default, optional manual control, built-in arrays, strings and hash maps.
  • Templates and compile-time function execution make powerful metaprogramming possible.
  • D code on this site is read-only; runnable C stand-ins illustrate the ideas.

Next: Installing D and your first program