Programming in D – Tutorial and Reference
Ali Çehreli

Other D Resources

The Hello World Program

The first program to show in most programming language books is the hello world program. This very short and simple program merely writes "Hello, World!" and finishes. This program is important because it includes some of the essential concepts of that language.

Here is a hello world program in D:

import std.stdio;

void main() {
    writeln("Hello, World!");
}

The source code above needs to be compiled by a D compiler to produce an executable program.

Compiler installation

At the time of writing this chapter, there are three D compilers to choose from: dmd, the Digital Mars compiler; gdc, the D compiler of GCC; and ldc, the D compiler that targets the LLVM compiler infrastructure.

dmd is the D compiler that has been used during the design and development of the language over the years. All of the examples in this book have been tested with dmd. For that reason, it would be the easiest for you to start with dmd and try other compilers only if you have a specific need to. The code samples in this book were compiled with dmd version 2.098.1.

To install the latest version of dmd, go to the download page at Digital Mars and select the compiler build that matches your computer environment. You must select the dmd build that is for your operating system and package management system, and whether you have a 32-bit or a 64-bit CPU and operating system. Do not install a D1 compiler. This book covers only D version two.

The installation steps are different on different environments but it should be as easy as following simple on-screen instructions and clicking a couple of buttons.

Source file

The file that the programmer writes for the D compiler to compile is called the source file. Since D is usually used as a compiled language, the source file itself is not an executable program. The source file must be converted to an executable program by the compiler.

As with any file, the source file must have a name. Although the name can be anything that is legal on the file system, it is customary to use the .d file extension for D source files because development environments, programming tools, and programmers all expect this to be the case. For example, test.d, game.d, invoice.d, etc. are appropriate D source file names.

Compiling the hello world program

You will write the source file in a text editor (or an IDE as mentioned below). Copy or type the hello world program above into a text file and save it under the name hello.d.

The compiler will soon check that the syntax of this source code is correct (i.e. it is valid according to the language rules) and make a program out of it by translating it into machine code. Follow these steps to compile the program:

  1. Open a terminal window.
  2. Go to the directory where you saved hello.d.
  3. Enter the following command. (Do not type the $ character; it is there to indicate the command line prompt.)
$ dmd hello.d

If you did not make any mistake, you may think that nothing has happened. To the contrary, it means that everything went well. There should be an executable file named hello (or hello.exe under Windows) that has just been created by the compiler.

If the compiler has instead printed some messages, you probably have made a mistake when copying the program code. Try to identify the mistake, correct it, and retry compiling. You will routinely make many mistakes when programming, so the process of correcting and compiling will become familiar to you.

Once the program has been created successfully, type the name of the executable program to run it. You should see that the program prints "Hello, World!":

$ ./hello      ← running the program
Hello, World!  ← the message that it prints

Congratulations! Your first D program works as expected.

Compiler switches

The compiler has many command line switches that are used for influencing how it compiles the program. To see a list of compiler switches enter just the name of the compiler:

$ dmd    ← enter just the name
DMD64 D Compiler v2.098.1
...
  -de            show use of deprecated features as errors (halt compilation)
...
  -unittest      compile in unit tests
...
  -w             warnings as errors (compilation will halt)
...

The abbreviated output above shows only the command line switches that I recommend that you always use. Although it makes no difference with the hello world program in this chapter, the following command line would compile the program by enabling unit tests and not allowing any warnings or deprecated features. We will see these and other switches in more detail in later chapters:

$ dmd hello.d -de -w -unittest

The complete list of dmd command line switches can be found in the DMD Compiler documentation.

One other command line switch that you may find useful is ‑run. It compiles the source code, produces the executable program, and runs it with a single command. ‑run must be the last of compiler switches, specified right before the name of the source file:

$ dmd -de -w -unittest -run hello.d
Hello, World!  ← the program is automatically executed
IDE

In addition to the compiler, you may also consider installing an IDE (integrated development environment). IDEs are designed to make program development easier by simplifying the steps of writing, compiling, and debugging.

If you do install an IDE, compiling and running the program will be as simple as pressing a key or clicking a button on the IDE. I still recommend that you familiarize yourself with compiling programs manually in a terminal window.

If you decide to install an IDE, go to the IDEs page at dlang.org to see a list of available IDEs.

Contents of the hello world program

Here is a quick list of the many D concepts that have appeared in this short program:

Core feature: Every language defines its syntax, fundamental types, keywords, rules, etc. All of these make the core features of that language. The parentheses, semicolons, and words like main and void are all placed according to the rules of D. These are similar to the rules of English: subject, verb, punctuation, sentence structure, etc.

Library and function: The core features define only the structure of the language. They are used for defining functions and user types, and those in turn are used for building libraries. Libraries are collections of reusable program parts that get linked with your programs to help them achieve their purposes.

writeln above is a function in D's standard library. It is used for printing a line of text, as its name suggests: write line.

Module: Library contents are grouped by types of tasks that they intend to help with. Such a group is called a module. The only module that this program uses is std.stdio, which handles data input and output.

Character and string: Expressions like "Hello, World!" are called strings, and the elements of strings are called characters. The only string in this program contains characters 'H', 'e', '!', and others.

Order of operations: Programs complete their tasks by executing operations in a certain order. These tasks start with the operations that are written in the function named main. The only operation in this program writes "Hello world!".

Significance of uppercase and lowercase letters: You can choose to type any character inside strings, but you must type the other characters exactly as they appear in the program. This is because lowercase vs. uppercase is significant in D programs. For example, writeln and Writeln are two different names.

Keyword: Special words that are a part of the core features of the language are keywords. Such words are reserved for the language itself, and cannot be used for any other purpose in a D program. There are two keywords in this program: import, which is used to introduce a module to the program; and void, which here means "not returning anything".

The complete list of D keywords is abstract, alias, align, asm, assert, auto, body, bool, break, byte, case, cast, catch, cdouble, cent, cfloat, char, class, const, continue, creal, dchar, debug, default, delegate, delete, deprecated, do, double, else, enum, export, extern, false, final, finally, float, for, foreach, foreach_reverse, function, goto, idouble, if, ifloat, immutable, import, in, inout, int, interface, invariant, ireal, is, lazy, long, macro, mixin, module, new, nothrow, null, out, override, package, pragma, private, protected, public, pure, real, ref, return, scope, shared, short, static, struct, super, switch, synchronized, template, this, throw, true, try, typedef, typeid, typeof, ubyte, ucent, uint, ulong, union, unittest, ushort, version, void, volatile, wchar, while, with, __FILE__, __FILE_FULL_PATH__, __MODULE__, __LINE__, __FUNCTION__, __PRETTY_FUNCTION__, __gshared, __traits, __vector, and __parameters.

We will cover these keywords in the upcoming chapters with the exception of the following ones: asm and __vector are outside of the scope of this book; body, delete, typedef, and volatile are deprecated; and macro is unused by D at this time.

Exercises
  1. Make the program output something else.
  2. Change the program to output more than one line. You can do this by adding one more writeln line to the program.
  3. Try to compile the program after making other changes; e.g. remove the semicolon at the end of the line with writeln and observe a compilation error.