The Hello World Program

This lesson acquaints you with the Hello World program and gives a basic introduction to D.

An example #

Most programming languages start with a basic Hello World program. This concise and simple program merely displays “Hello, World!” on the screen and terminates. However, the program is important because it teaches you some of the most basic yet essential concepts of a programming language.

Hello world
Hello world

Here is how a “Hello, World!” program looks like in D:

import std.stdio;
void main() {
writeln("Hello, World!");
}

Contents of the “Hello World” program #

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

  • Core features: 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.
Core features
Core features
  • Libraries and functions: The core features only define the structure of a language. These are used for defining functions and user-defined 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 is a function in D’s standard library. As its name suggests (write line), it is used for printing a line of text.
Library and function
Library and function
  • Modules: Library contents are grouped by the 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.
Module
Module
  • Characters and strings: Expressions like “Hello, World!” are called strings, and the elements of strings are called characters. The only string in this program contains the characters ‘H’, ‘e’, ‘!’, and others.
Characters and strings
Characters and strings
  • 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!”.
  • Keywords: Special words that are a part of the core features of the language are called 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 the example program: import, which is used to introduce a module to the program, and void, which here means “not returning anything”.
Keyword
Keyword
  • Significance of uppercase and lowercase letters: You can choose to type any character inside a string, but you must type the keywords 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 and using Writeln will make the program throw an error.

Here is a list of the most commonly used keywords in D language. You can find the complete list at D Grammar.

Keywords in D language
Keywords in D language

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


In the next lesson, we will explore the difference between functions writeln and write.