Writing and Running First Program in D Programming Language

Introduction to Writing and Running First Program in D Programming Language

Hello, fellow D programming enthusiasts! Today, in this blog post, “Writing and Running First Program in

k" rel="noreferrer noopener">D Programming Language” – I will introduce you to one of the most exciting and relevant concepts about D programming. If you are a beginner in D, the very first thing you have to do is write a simple “Hello, World! program which will get you familiar with the structure and syntax of the language. In this post, I will take you through writing your first D program explaining the key components of the code, going through compilation and running it, and finally out what the output actually stands for. By the end of this post, you’ll have a good grasp of D programming and be ready to write larger programs. Let’s get started!

How to Write and Run First Program in D Programming Language?

Getting started with D programming language is an exciting step towards exploring the world of modern, efficient, and powerful systems programming. Writing and running your first D program, typically a “Hello, World!” application, is a great way to understand the basic structure of the language and the process of compiling and executing code. Here’s a step-by-step guide to help you write and run your first D program.

1. Install D Compiler

Before writing any D code, you need to install the D compiler, which compiles the D program into an executable. D’s official compiler is DMD, but you can also use LDC (LLVM D Compiler) or GDC (GNU D Compiler).

  • For Windows:
    • Download the DMD installer from the official D website.
    • Run the installer and follow the on-screen instructions to set up the D compiler on your system.
  • For macOS and Linux:
    • You can install DMD using Homebrew (macOS) or apt (Linux).
    • For macOS, use: brew install dmd
    • For Ubuntu-based Linux, use: sudo apt-get install dmd

2. Set Up a Text Editor

To write D code, you can use any text editor like Visual Studio Code, Sublime Text, or even a simple text editor like Notepad++. For a more feature-rich experience, it’s recommended to install the code-d plugin for Visual Studio Code, which provides features like syntax highlighting, code completion, and error checking.

3. Write Your First D Program

Once your environment is set up, it’s time to write your first D program.

  • Open your text editor and create a new file with the .d extension (e.g., helloworld.d).
  • Write the following code:
import std.stdio;

void main() {
    writeln("Hello, World!");  // Prints "Hello, World!" to the console
}

4. Explanation of the Code:

  • import std.stdio;: This line imports the stdio module, which contains functions for input and output, like writeln().
  • void main() { ... }: This is the entry point of any D program. The main() function is where execution begins. The void indicates that this function doesn’t return a value.
  • writeln("Hello, World!");: This function writes the string "Hello, World!" to the console and moves to the next line. It’s similar to println in other languages.

5. Save the File

Save the file with a .d extension (e.g., helloworld.d) in a directory that’s easy to access.

6. Compile the Program

Next, you need to compile the D code into an executable.

  1. Open a terminal or command prompt window.
  2. Navigate to the directory where you saved your helloworld.d file.
  3. Run the following command to compile the D program:
dmd helloworld.d

This command tells the D compiler (dmd) to compile the helloworld.d file. If there are no errors, it will generate an executable file (on Windows, this will be helloworld.exe).

7. Run the Program

Now that your program is compiled, you can run the executable:

  • For Windows:
    • In the same terminal window, type:
helloworld.exe
  • For macOS/Linux:
    • In the terminal, type:
./helloworld

This will execute the compiled D program, and you should see the following output in the terminal:

Hello, World!

8. Handling Errors

If there are any syntax errors in your code, the D compiler will display error messages in the terminal. For example, if you forget to import the std.stdio module, the compiler will notify you of the missing import. You can fix the errors in the code and recompile the program by running the dmd command again.

9. Editing and Re-running the Program

Once your first program runs successfully, you can modify the code to experiment with different statements, variables, or functions. Simply edit the code in your text editor, save it, and recompile it by running the dmd command again. This allows you to build more complex programs as you become familiar with the syntax and features of the D language.

10. Conclusion

Writing and running your first program in D is a simple but powerful way to get started with this modern programming language. By following these steps, you’ve learned how to:

  • Install the D compiler.
  • Write a basic “Hello, World!” program.
  • Compile and run your program using the terminal.

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading