D (The Programming Language)/d2/Hello, World!

Lesson 1: Hello, World!

edit

In this lesson, you will learn how to use the Phobos library to write to the console. Also, you will learn some about the structure of D programs.

Introductory Code

edit

We will start with the absolutely necessary Hello World example.

Hello World

edit
/* This program prints a
   hello world message
   to the console.  */

import std.stdio;

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

Concepts

edit

In this lesson we see the import statement, the main function, the Phobos standard library in use, and also a code comment.

import std.stdio

edit

The Phobos library contains the std.stdio module which in turn contains the writeln function (along with various other functions). In order to use that function, you must first import that module. Notice that statements in D end in a semicolon.

void main()

edit

All executable D programs contain a main function. This function does not return a value, so it is declared "void" (Technically, this is an over-simplification. Return values of main will be explained in more detail later.) This function is executed when the program runs.

Function body code is enclosed in curly brackets. Although the indentation and whitespace is only for the reader, not for the compiler, make sure you indent and format your code properly and consistently; it's generally a good coding practice.

writeln and the write family

edit

writeln is for writing to the standard output (console in this case). You can supply a string such as "Hello, World!" as an argument, and that string will be printed out. There are four basic flavors of this function: With or without formatting, and with or without a trailing newline. To demonstrate how they work, all of the following are equivalent (although the first and thirds ones don't flush stdout on Windows):

// write: Plain vanilla
write("Hello, World!\n"); // The \n is a newline

write("Hello, ", "World!", "\n");

write("Hello, ");
write("World!");
write("\n");
// writeln: With automatic newline
writeln("Hello, World!");
writeln("Hello, ", "World!");
// writef: Formatted output
writef("Hello, %s!\n", "World");
// writefln: Formatted output with automatic newline
writefln("Hello, %s!", "World");
writefln("%s, %s!", "Hello", "World");
writefln("%2$s, %1$s!", "World", "Hello"); // Backwards order

/* I am a comment */

edit

The first few lines of this program are comments. They are ignored by the compiler. Block comments are enclosed in /* */. Line comments continue after // .

This is an example of a line comment:

import std.stdio; // I am a comment
void main(){} //this program does nothing

D also supports nested block comments, enclosed in /+ +/:

/+
thisIsCommentedOut();
    /+ thisIsCommentedOut(); +/
thisIsSTILLCommentedOut();
+/

This is different from ordinary block comments which behave just like in C:

/*
thisIsCommentedOut();
    /* thisIsCommentedOut(); */
thisIsNOTCommentedOut();
// The following line is a syntax error:
*/

Tips

edit
  • writeln is different from write because it adds a newline to the end.