Variables

Something that is critical to every useful program is a variable. They're like little boxes you can put pieces of data in.

# What is a variable?

A variable is like a box - you can put some data in it, get it back out, and change the contents, too. This is how you declare a variable:

```int number;
```

There are two main parts to this. The type, and the name. Here, you can see we are using the type int, and we are calling it number.

The type is, well, the type of variable it is. There are several primitive data types, but the main two categories are integers and floating point numbers. Integers are basically rounded numbers, while floating points can have numbers have the decimal point, too. There are a several different kinds, but the main two you should worry about for now are int and float.

Then, we have the name. It's what you refer to the variable with in your code.

# Shapes

Now, remember, variables are like boxes, right? Well, boxes can have different shapes, and sizes. Sometimes, when moving something from a different shaped/sized box, you may lose some data. For instance, guess what would happen in the following code:

```float num1 = 3.14;
int num2 = num1;
```

If you printed out num1, you would get 3.14. But if you tried num2, it would print out 3. This is because integers don't store digits after the decimal point, so it just throws it out.

What would happen if you changed it to something like 3.89? You may think it would round, and that num2 would end up being 4, but that isn't true. When turning floating points to integers, it completely ignores it. It gives you everything to the left of the decimal, and that's all.

# Sizes (sizeof)

I also mentioned that variables can have different sizes. You can see this, too, using sizeof. For instance, run the following:

```#include <iostream>

int main()
{
std::cout << sizeof( int ) << std::endl;
std::cout << sizeof( float ) << std::endl;
}
```

When you run that, you should see something like this:

``````4
4```
```

The "units" that the size is in, is bytes. As you can see, the first is the size of an int, and the second is the size of a float.

Although the sizes of int and float can vary by platform, typically you have something like this:

Type Size
char 1*
short 2
int 4
long 4*
long long 8*
float 4
double 8

You may be wondering why char has an asterisk beside it. This is because the size of it is guaranteed to be a single byte.

long also has an asterisk. This is because it has to have at least as many bytes as int does, but can have more. This means that with some compilers have it as 4 bytes, and some have it as 8.

This is why long long was added in the latest standard, [[C++11]] (originally in C99). It is defined to be at least the size of a long, but also have at least 8 bytes. Although this wasn't in the most widespread1 supported standard, C++03, most compilers already supported this.

Now, obviously if variables have different sizes, they can store different amounts of data, right? The smallest, char, has only one byte. So, this can store the smallest amount of data. However, long long has 8 bytes, so it can store exponentially more data (literally!).

How do you get this? Let's say x is the size of whatever type you are wondering about. Now, a byte has 8 bits, so the amount of bits something has is x * 8, or b. To get the maximum value of an integer (not floating points!) is 2^b, or 2 to the bth power. So, a char would be able to hold 256 different values.

# Signed, Unsigned

Something that you will most likely need is negative numbers. If you put something like int i = -3;, you'll notice it will work fine. This is because integers are signed, by default.

Remember that paragraph about the maximum value? Well, signed numbers use one bit to say whether it's positive or negative. Because of this, the maximum value is (2^(b - 1) - 1), or about half. But you still get the same amount of different values, because you also can go negative.
A complication that all nowadays negative integers have is that they use two's complement in order to be represented. This results in actually being able to represent one more negative value than positive. Hence for char, its range of values is [-128, 127].

Since integers are signed by default, you'll need to do this to make them unsigned (which uses all b bits for positive):

```unsigned int num;
```

Floating point numbers can't be declared as unsigned. You'll probably get an error if you try.

# Scope

One thing that will become very important later on (starting next tutorial :P), is that variables have a certain scope. That is, they "expire".

You've seen these brackets around main for a while. That's the scope of the function main, and any variables declared within it disappear after main exits.

You can also declare a smaller scope inside a function, like this:

```#include <iostream>

int main()
{
int var1 = 0;
int var2 = 5;

{
int var3 = 7;
int var4 = var3 + var2;
}

int var5 = var2;
}
```

This might be a little confusing. var1, var2, and var5 can be accessed anywhere within the function, but var3 and var4 can only be accessed within the brackets that contain them. So, trying to access var3 or var4 outside of the middle set of brackets would cause an error saying that the variable doesn't exist.

Another thing to keep in mind is that variables only exist from the point they are declared and onward. For instance, I can't do int var1 = var2; because var2 doesn't exist yet.

# Example

Here's something you may want to tinker with for a while.

```#include <iostream>

int main()
{
int num1 = 10;
int num2 = 4;

int num3 = num1 * num2;

std::cout << num1 << " * " << num2 << " = " << num3 << std::endl;
}
```

# Conclusion

So, you should have a good grasp of variables by now2. :) Next, go learn about arrays.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 3.0 License