D (programming language)/Variables and User Input

Part of the D|D Programming Language Course

Variables
A variable is the association of a name with a value of a particular type. This is done so that we can store values and retrieve them later on in a program.

To be more technical about it, the name is a pointer to a memory address that holds the value. The type declaration forces the value to fit into a certain number of bytes.

But we aren't concerned with that at the moment so here are some examples of assigning values to variables.

The integer we're calling x will hold the value of 3.

The character  will hold the value.

Basic Types
This way of creating variables can be simplified into the following form.

type name = value; where type is a data type in D,    name is the name we'll refer to when we want the value stored, value is a value of the same data type as type, and = assigns value to name.

The data type of a variable can be any of the following:
 * void - signifies the lack of a data type, like the word nothing
 * bool - shorthand for boolean and holds one of two values, true or false More detail
 * int - shorthand for integer, basically numbers that have no decimal point such as 3, 25, -1
 * float - shorthand for floating-point which are numbers that contain decimal places
 * char - shorthand for character

There are many more types, but since we've just started with the language we'll only look at those five.

Creating Variables
You can create a variable with no value with the following general form: type name;

When you do this, the value associated with name is the default of the data type. This is detailed here.

For our basic data types, the default values are:
 * void:-
 * bool: false
 * int: 0
 * float: float.nan, in other words, Not A Number
 * char: 0xFF

Creating Arrays
An array is a way of holding multiple objects of the same data type. There are two general forms for creating arrays.

One way creates an empty array that has no size specified. type[] name;

The other way is to create an empty array that has a size specified. type[] name = new type[size];

The important item to notice is the inclusion of [] in the type declaration. This tells us that name is an array of type.

Assigning Values
There is one small difference between assigning values to a variable and to an array. With an array, you need to specify exactly which variable in the array, the index, you'll be assign the value to. The general form for this is: name[i] = value; The value must be of the same data type as the name array was declared. You cannot, for example, assign a float like 0.0 to an int. Also, i must be more than or equal to 0 and less than the size of the array.

Another way to assign values is when you're creating an array: type[] name = [values]; The values must be separated by commas.

Getting The Values
To get a value out of an array, all you need to do is specify the index of the variable you'd like to extract the value from.

The general form:

Examples
An array of integers.

The values held by  are: 1, 2 and 3.

Modifying the integer array.

The values held by  are: 4, 2 and 3. If we were to print  out, we would see the number 2 on the screen.

Creating a character array, also known as a string.

This is the same as this:

Example 1: Reading strings
Now that we know how to store values, we can start using variables for something exciting. The source code for a program that asks for a user to enter their name and then prints out their name is below.

Line 1 imports the standard input/output module, using the  keyword, so that we can write to the screen and read input from the keyboard.

Line 3 declares the function  which is the starting point of any D program. When you compile a D program and run it, the  function is the first function executed.

Line 5 prints out  using the function.

Line 6 creates a variable  that is an array of char.

Line 7 reads an array of char until the user presses the Enter Key (also known as Return Key) on the user's keyboard. Whatever has been read by the  function is then assigned to.

Line 8 prints out  and then the , again using the   function.

Lines 4-9 enclose the body of the  function.

Example 2: Reading numbers
To read numbers from the user, you need to import  which contains all the functions that convert strings  to a number.

Line 7 reads in a line of  from the user. Notice how we've dropped the parenthesis? D allows us to do this when a function requires zero arguments.

Line 8 converts the line of  to an integer and stores it in.

Line 9 prints out  along with the result of 3 +.

Modules Used

 * std.stdio
 * std.conv