Variables in D Programming Language
Our first example will demonstrate variable declaration and initialization in D. Here’s the full source code:
To run the program, save the code in a file with a .d
extension (e.g., variables.d
) and use the D compiler (dmd
) to compile and run it:
Let’s break down the key points:
In D, variables can be explicitly declared with a type, or you can use
auto
for type inference.You can declare multiple variables of the same type in one line.
D performs type inference when you use
auto
or initialize a variable.Variables declared without initialization are default-initialized. For integers, the default value is 0.
D doesn’t have a specific shorthand syntax like
:=
for declaration and initialization, butauto
provides a concise way to declare and initialize variables with type inference.The
writeln
function from thestd.stdio
module is used for printing to the console.
Now that we’ve covered basic variable declaration and initialization in D, let’s move on to more language features.