< A Beginner's Guide to D < The Basics

Programmers familiar with C or C++ are likely to find this section familiar, and may want to skim or skip it at their discretion.

Where the Program Begins

There is a special location in code that is used as a starting point for all programs in D. This location is called the main function. The first code you want to run, the code that will start all the other code, is in this function.

Exactly how the main function works will be introduced in a later chapter, but for the moment you just need to know what the code for one style of main function:

void main()
{
    // place code here
}

Putting the Concepts Together

You now have enough knowledge to write a small, simple but complete program. Extending the "hello world" program introduced earlier, you can make a program start and store a number, by combining a main function with some simple variable code learned in the previous section. Here is a complete program, that will store the value 3 in the int variable 'a' and print it to console:

import std.stdio;

void main()
{
    int a;
    a = 3;
    writefln(a);
}
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.