# Variable Declaration

### Declaring Variables

##### Before they are used, all variables have to be declared. Declaring a variable means defining its type, and optionally, setting an initial value (initializing the variable). Variables do not have to be initialized (assigned a value) when they are declared, but it is often useful.

``` int inputVariable1; int inputVariable2 = 0; // both are correct ```

### Initializing Variables

##### Variables may be initialized (assigned a starting value) when they are declared or not. It is always good programming practice however to double check that a variable has valid data in it, before it is accessed for some other purpose.Example:
``` int calibrationVal = 17;
// declare calibrationVal and set initial value
```

### Variable Rollover

##### When variables are made to exceed their maximum capacity they “roll over” back to their minimum capacity, note that this happens in both directions.
```   int x
x = -32,768;
x = x - 1;       // x now contains 32,767 - rolls over in neg. direction

x = 32,767;
x = x + 1;       // x now contains -32,768 - rolls over
```

### Using Variables

##### Once variables have been declared, they are used by setting the variable equal to the value one wishes to store with the assignment operator (single equal sign). The assignment operator tells the program to put whatever is on the right side of the equal sign into the variable on the left side.
```inputVariable1 = 7;             // sets the variable named inputVariable1 to 7
inputVariable2 = analogRead(2); // sets the variable named inputVariable2 to the
// (digitized) input voltage read from analog pin #2
```

### Examples

``` int lightSensVal;
char currentLetter;
unsigned long speedOfLight = 186000UL;
char errorMessage = {"choose another option"}; // see string
```
##### Once a variable has been set (assigned a value), you can test its value to see if it meets certain conditions, or you can use its value directly. For instance, the following code tests whether the inputVariable2 is less than 100, then sets a delay based on inputVariable2 which is a minimum of 100:
```if (inputVariable2 < 100)
{
inputVariable2 = 100;
}

delay(inputVariable2);
```

### Some variable types

##### Language Reference Home

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.