Learn Programming

Learn D Programming Lesson 1 - Getting started

What is D?

D is a programming language that is based on C++ but adds a lot of new and useful features and it removes features that are considered harmful. D is a bit easier to use than C++ but at the same time it is just as powerful. You can find out more about it on the Digital Mars D Programming Language site.

Getting and installing the D compiler

To be able to program in D you need to first get a D compiler. For this tutorial we are going to use the Digital Mars D Compiler. You will need to download the compiler and the linker. Go to the Digital Mars D Downloads page and download the "dmd D 1.0 compiler" and the "Linker and Utilities".

You will have two files. The first one will have the filename dmd.1.???.zip where the question marks will be the sub-version number. The second file will have the name dmc.zip. You need to create a folder called D in the root of your main harddrive. You then need to extract the files from dmd.1.???.zip to this folder and then extract the files from dmc.zip to the same folder and make sure that you agree to overwrite any existing files.

Setting up the development environment

We are going to be writing our D programs in Notepad and we will be compiling and running them from the command line. You need to create a folder called programs in the directory you created earlier called D. We will use this for saving our programs that we write. You can make things a little bit easier on yourself by creating a bat file that will set up the programming environment for us. Create a file called dprogenv.bat in your D folder and edit it in notepad and paste the following text in it and save it:

@ECHO OFF
ECHO D Programming Environment
@SET PATH=%PATH%;\D\dmd\bin
ECHO PATH=%PATH%
CD \D\programs
%ComSpec%

Every time you want to program in D you can just run dprogenv.bat and it will open a command line window with all the necessary environment variables already set. If you know what you are doing then you can also set the path on your computer to include the path to the folder that contains the compiler and use it like that but using this bat file is much easier for beginners.

Here is an example of what the final directory structure should look like:

C:\D\dm\
C:\D\dmd\
C:\D\programs\
C:\D\dprogenv.bat

When you are asked to create a d source file in the tutorials you need to create it in the folder called programs. Remember that we are going to be editing the source files in notepad. You might have an easier time if you set all files with the extension .d to open automatically in notepad.

There are a few IDEs such as Poseidon available which make it easier to write programs in D but they are not really suitable for the simple programs that we are going to write while learning which is why we are going to be doing everything from the command line. There is no reason why you can't use one of these IDEs but it will be up to you to figure out how to use it.

You are now ready to start writing your first program which you will learn how to do in the next lesson.