Variables

Now that we know how we will be encoding things from our world and putting them in memory we must deal with another problem. How will we find these things in memory when we want to refer to them?

Actually, this is pretty easy. We give those places names!

Memory is addressed

As we talked about earlier, memory is just a huge series of bytes (eight bit chunks). Each of those bytes has a unique address, like a street number. The addresses start at zero and go all the way up to whatever number of bytes you have in the memory of your machine.

Storing a piece of data

When we want to put a number representing something in memory, we must find a set of byte chunks big enough to hold the number and record the address of that sequence of bytes. These bytes must not be being used for another piece of data at the moment. Usually, these chunks are just assigned in order starting in low (address) memory and moving to high (address) memory. It is important to remember where each piece of data lives, so we must record the address of that data somewhere. The actual address we record is the memory address of the first byte we assigned to this piece of data. If we know the address of the data item, and know how many bytes were required to store the item, we are set to go - right?

Recording information about the data item

Actually, we cannot just record the address. We also need to know how big the data item is, and we need to know what kind (the data type of that data item. All these pieces are enough for our program to make sure it uses the data right - applying the right rules as it does its work!

Variables

Now, we are ready to talk about what a variable actually is! It is just a name we choose to represent one of those data numbers we just recorded!

Why use names and not just numbers?

Humans are pathetic at remembering numbers, but we are pretty good at remembering names! (Think about how much fun you would have if we all were known by just our Social Security Number! “I ran into 123-45-6543 yesterday and we were talking about 342-34-6789”)

We pick a name for a piece of data to help us remember what that data was all about. We are not concerned with the address where that item will live when our program runs. Instead, we will refer to the data by using the variable name.

Picking a variable name

Most of the time, we will need to obey a few rules about how we pick names. You know all about this - we are not allowed to have human names like roie123 no matter know much weird parents might like to try! Programming rules are not so strict. In fact, these rules are usually pretty simple:

  • names must start with a letter, not a digit (numbers always start with a digit as we shall see)
  • names must only be made up of letters, numbers, underscores, or a few others
  • names must be less than so long
  • blank spaces are not allowed in names

Most programmers end up using a few more rules, but they are really just conventions. Here are a few examples:

  • names that are two words are separated by an underscore
  • names that are two words are jammed together and the first letter of each is capitalized

Warning

We will use these rules and conventions in this class. Pay attention to the names you pick! Make them mean something!

Declaring Variables

When we write our programs, we will need to set up some names for data items we want to manipulate. When we start programming, we will only use a few variables, later we will use many (and show you how to come up with names that refer to many data items all at once.

Here is how we tell our C++ program that we want a new container to store a data item encoding:

int ny_data_item;
float angle = 0.0;

There are two (or three) parts to these data declarations.

  • “data type” variable name” “Optional initial value” semicolon

We are using a special keyword defining the type of data item we want. We must use the kinds of data types provided by C++ here, more on that later.

The name we pick is up to use to come up with. Remember that you can use both upper and lower case letters.

Warning

In C++ the case is important, Two names differing only by the case of the letters are considered different. You will have problems with this!

Finally, we are allowed to give the container we are defining an initial value. We do not have to do this, but there is one important thing to note:

Note

Any chunk of memory always has a number in it. When the computer is first powered up, the bits take on a random pattern. This means that there is a number in the containers you set up - but you have no idea what that number is. Such containers are called uninitialized. This is not a problem if you are sure you will put a meaningful number in them later. The biggest mistake you can make in programming is to forget this and end up with a mess because you used a random number in your data manipulations!

C++ data types

The simple data types we will be using are these:

type description
int numbers with no fractional part
float numbers with a fractional part
double bigger floats (more accurate)
DWORD used in DargGDK to refer to colors

Each of these data types takes a fixed number of dytes of memory to store. Each programming language control this, and you need to look up the size for each system you use. Most of the time, integers take 32 bits, floats take 32 bits, and doubles take 64 bits.

Literals

Each data type has a specific format for writing down values. We often want to write down specific values for our data items in our programs. But we need to be careful doing so.

Values written down in the program are called literal values. They will be converted from text form to internal binary form as our program is processed. Here are some examples of literal values:

  • int: 0, 123, -27, +42
  • float: 1.0, 0.23E+12
  • string “This is a literal string of characters”

There are two literals that need explaining here:

Scientific notation

Because numbers with fractions can get very big, or very small, scientists came up with a way of writing them down that is fairly wasy to figure out.

The number is usually shown as something between 0 and 1 with a bunch of digita after a decimal point. Then we includ eht eleter “E”, meaning exponent followed by an integer number with an optional sign symbol. The exponent tells us how to move the decimal point to get the real number we are talking about. Here are a few examples to show how this works:

  • 0.123E3 = 123.0 (slide the decimal three spaced to the right)
  • -0.123E-3 = -0.000123 (slide the decimal three spaces to the left)

Actually, the first part can be non-zero and the computer will figure it out:

  • 123.456E3 = 123456.0

String literals

We will talk about these later, since they are special. If you recall our earlier discussion, a string looks like many more than one container. We need as many bytes as we have letters. Still, the computer will set up the container and fill it with the right codes for each letter. We will see what is going on here in more detail in a later lecture.

Literals have no names!

One important thing to notice. If we set up literal values in our programs, the computer will convert the text into the right binary form. But where does this literal get stored. The answer is - it doesnt. The literal is converted and usually placed in another container you have defined with a suitable data type. We usually use literals to initialize other containers. We use strings when we want to generate output to see what is going on in our program. (Remember that “hello, world” string we displayed in our first program. The literal string was converted, and sent to an output device where it was converted back into letters and printed on the screen. Yikes!

Note

Later, we will see how to manipulate strings as normal data items. We would do this if we wanted to write a program something like MS Word.

Assigning values to variables

We have already seen how this works.

int my_variable = 0;
my_variable = 5;

The first line is a declaration which set up the variable and gives it an initial value. That is one way to assign a value to a variable. but not a very flexible one.

The second line is called an assignment statement. Technically, there are two parts to this. On the left side of the equal sign we set up a variable name` we have previously declared (as we did here). On the right side of the equal sign we provide an expression, which can involve math operators and a few other things. The expression can be as simple as a literal value as shown here. That final value will be placed in the naed container we have defined in this statement. We can do this as often as we like. Each time, the old value stored in the container is lost, and a new one recorded in memory.

What would this do?

int my_variable;
my_variable = 1.0;

Here, we have a problem. The variable is designed to hold numbers with no fraction, but the literal value we wrote down has a decimal point in it. The encoding for fliats is different from the encoding for integers. SO how should we handle this.

The answer is simple,. The computer will do a type conversion for us (it knows this is a reasonable thing to do). Basically, the fractional part will be thrown away and the rest encoded properly and stored in the variable container.

Warning

The computer will never do anything like rounding whan this happens. If you want that to happen it is up to you to set up your program so that happens!

For example, it is wrong to put a decimal point in a number we have defined as an int (meaning integer). This can be a bit confusing, since C++ is good at converting between the data types for numbers. But you shoud be careful. We will talk about this more in the next section! We often want to write programs with values

Constants

Why have we been calling the data containers we are going to manipulate variables? Simple! We might change the number in them during the program. They might vary! Duh!

What if we do not want them to vary - ever?

It is hard (but not impossible) to keep the machine from flipping bits when it wants to. If we want to make sure it does not do that, we need to tell our programs that we never want the value stored in the containers to change. This is done by marking them as constant:

  • const int screen_width = 640;

The container will be set up as usual, and the initial value recorded in the memory assigned to that variable. But, once that has been done, it will not be allowed to change.

Note

I tend to make names for constants all upper case as a reminder to me not to try to change them. I would have written the example this way:

  • const int SCREEN_WIDTH = 640;