D Programming – Basic Syntax


D Programming – Basic Syntax


”;


D is quite simple to learn and lets start creating our first D program!

First D Program

Let us write a simple D program. All D files will have extension .d. So put the following source code in a test.d file.

import std.stdio;  

/* My first program in D */ 
void main(string[] args) { 
   writeln("test!"); 
}

Assuming D environment is setup correctly, lets run the programming using −

$ dmd test.d 
$ ./test

We can see the following output.

test

Let us now see the basic structure of D program, so that it will be easy for you to understand basic building blocks of the D programming language.

Import in D

Libraries which are collections of reusable program parts can be made available to our project with the help of import. Here we import the standard io library which provides the basic I/O operations. writeln which is used in above program is a function in D”s standard library. It is used for printing a line of text. Library contents in D are grouped into modules which is based on the types of tasks that they intend perform. The only module that this program uses is std.stdio, which handles data input and output.

Main Function

Main function is the starting of the program and it determines the order of execution and how other sections of the program should be executed.

Tokens in D

A D program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following D statement consists of four tokens −

writeln("test!");

The individual tokens are −

writeln (
   "test!"
)
;

Comments

Comments are like supporting text in your D program and they are ignored by the compiler. Multi line comment starts with /* and terminates with the characters */ as shown below −

/* My first program in D */ 

Single comment is written using // in the beginning of the comment.

// my first program in D

Identifiers

A D identifier is a name used to identify a variable, function, or any other userdefined item. An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).

D does not allow punctuation characters such as @, $, and % within identifiers. D is a case sensitive programming language. Thus Manpower and manpower are two different identifiers in D. Here are some examples of acceptable identifiers −

mohd       zara    abc   move_name  a_123 
myname50   _temp   j     a23b9      retVal

Keywords

The following list shows few of the reserved words in D. These reserved words may not be used as constant or variable or any other identifier names.

abstract alias align asm
assert auto body bool
byte case cast catch
char class const continue
dchar debug default delegate
deprecated do double else
enum export extern false
final finally float for
foreach function goto if
import in inout int
interface invariant is long
macro mixin module new
null out override package
pragma private protected public
real ref return scope
short static struct super
switch synchronized template this
throw true try typeid
typeof ubyte uint ulong
union unittest ushort version
void wchar while with

Whitespace in D

A line containing only whitespace, possibly with a comment, is known as a blank line, and a D compiler totally ignores it.

Whitespace is the term used in D to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the interpreter to identify where one element in a statement, such as int, ends and the next element begins. Therefore, in the following statement −

local age

There must be at least one whitespace character (usually a space) between local and age for the interpreter to be able to distinguish them. On the other hand, in the following statement

int fruit = apples + oranges   //get the total fruits

No whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish for readability purpose.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *