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
Hello, fellow D programming enthusiasts! Today, in this blog post, “Writing and Running First Program in
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.
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).
brew install dmd
sudo apt-get install dmd
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.
Once your environment is set up, it’s time to write your first D program.
.d
extension (e.g., helloworld.d
).import std.stdio;
void main() {
writeln("Hello, World!"); // Prints "Hello, World!" to the console
}
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.Save the file with a .d
extension (e.g., helloworld.d
) in a directory that’s easy to access.
Next, you need to compile the D code into an executable.
helloworld.d
file.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
).
Now that your program is compiled, you can run the executable:
helloworld.exe
./helloworld
This will execute the compiled D program, and you should see the following output in the terminal:
Hello, World!
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.
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.
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:
Subscribe to get the latest posts sent to your email.