Variables - D Programming Language

Menu

- Learn
- Documentation
  - Language Reference
  - Library Reference
  - Command-line Reference
  - Feature Overview
  - Articles
- Downloads
- Packages
- Community
  - Blog
  - Orgs using D
  - Twitter
  - Calendar
  - Discord (community-run)
  - Forums
  - IRC
  - Wiki
  - Issues
  - Contributing
  - Contributors
  - Foundation
  - Security Team
  - Donate
  - Sponsors
- Resources
  - Tour
  - Books
  - Tutorials
  - Tools
  - Editors
  - IDEs
  - run.dlang.io
  - Visual D
  - Acknowledgments
  - D Style
  - Glossary
  - Sitemap

Search
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

# Variables

Concrete concepts that are represented in a program are called variables. A value like air temperature and a more complicated object like a car engine can be variables of a program.

The main purpose of a variable is to represent a value in the program. The value of a variable is the last value that has been assigned to that variable. Since every value is of a certain type, every variable is of a certain type as well. Most variables have names as well, but some variables are anonymous.

As an example of a variable, we can think of the concept of the number of students at a school. Since the number of students is a whole number, `int` is a suitable type, and `studentCount` would be a sufficiently descriptive name.

According to D's syntax rules, a variable is introduced by its type followed by its name. The introduction of a variable to the program is called its definition. Once a variable is defined, its name represents its value.

    import std.stdio;

    void main() {
        // The definition of the variable; this definition
        // specifies that the type of studentCount is int:
        int studentCount;

        // The name of the variable becomes its value:
        writeln("There are ", studentCount, " students.");
    }

The output of this program is the following:

    There are 0 students.

As seen from that output, the value of `studentCount` is 0. This is according to the fundamental types table from the previous chapter: the initial value of `int` is 0.

Note that `studentCount` does not appear in the output as its name. In other words, the output of the program is not "There are studentCount students".

The values of variables are changed by the `=` operator. The `=` operator assigns new values to variables, and for that reason is called the assignment operator:

    import std.stdio;

    void main() {
        int studentCount;
        writeln("There are ", studentCount, " students.");

        // Assigning the value 200 to the studentCount variable:
        studentCount = 200;
        writeln("There are now ", studentCount, " students.");
    }

    There are 0 students.
    There are now 200 students.

When the value of a variable is known at the time of the variable's definition, the variable can be defined and assigned at the same time. This is an important guideline; it makes it impossible to use a variable before assigning its intended value:

    import std.stdio;

    void main() {
        // Definition and assignment at the same time:
        int studentCount = 100;

        writeln("There are ", studentCount, " students.");
    }

    There are 100 students.

##### Problem

Define two variables to print "I have exchanged 20 Euros at the rate of 2.11". You can use the floating point type `double` for the decimal value.

... çözüm

Copyright © 1999-2026 by the D Language Foundation | Page generated by Ddoc on Mon Aug 17 18:57:14 2026