# D (programming language)/Variables and User Input

Part of the 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 a will hold the value n.

### Basic Types

This way of creating variables can be simplified into the following form.
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:
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

### Arrays
#### 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.
The other way is to create an empty array that has a size specified.
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:
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:
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 x are: 1, 2 and 3.
Modifying the integer array.
The values held by x are: 4, 2 and 3. If we were to print x[1] 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:

## User Input
### 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 import keyword, so that we can write to the screen and read input from the keyboard.
Line 3 declares the function main which is the starting point of any D program. When you compile a D program and run it, the main function is the first function executed.
Line 5 prints out Enter your name: using the function writef.
Line 6 creates a variable name 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 readln function is then assigned to name.
Line 8 prints out Hello and then the name, again using the writef function.
Lines 4-9 enclose the body of the main function.

### Example 2: Reading numbers

To read numbers from the user, you need to import std.conv which contains all the functions that convert strings (char) to a number.
Line 7 reads in a line of text 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 text to an integer and stores it in number.
Line 9 prints out 3 + number = along with the result of 3 + number.

### Modules Used

* std.stdio
* std.conv