The Compiler

Yes, but it can't run a C++ program. The only kind of program any computer can run is one made of machine instructions; this is called a machine language program, for obvious reasons. Therefore, to get our C++ program to run, we have to translate it into a machine language program. Don't worry, you won't have to do this yourself; that's why we have a program called a compiler.[4] The most basic tasks that the compiler performs are the following:

[4] How is the compiler itself translated into machine language so it can be executed? The most common method is to write the compiler in the same language it compiles and use the previous version of the compiler to compile the newest version! Of course, this looks like an infinite regress; how did the first compiler get compiled? By manual translation into assembly language, which was then translated by an assembler into machine language. To answer the obvious question, at some point an assembler was coded directly in machine language.

  1. Assigning memory addresses to variables. This allows us to use names for variables, rather than having to keep track of the address of each variable ourselves.

  2. Translating arithmetic and other operations (such as +, , etc.) into the equivalent machine instructions, including the addresses of variables assigned in the previous step.[5]

    [5] The compiler also does a lot of other work for us, which we'll get into later.

This is probably a bit too abstract to be easily grasped, so let's look at an example as soon as we have defined some terms. Each complete operation understood by the compiler is called a statement, and ends with a semicolon (;).[6] Figure 3.1 shows some sample statements that do arithmetic calculations.[7]

[6] By the way, blank lines are ignored by the compiler; in fact, because of the trailing semicolon on each statement, you can even run all the statements together on one line if you want to, without confusing the compiler. However, that will make it much harder for someone reading your code later to understand what you're trying to do. Programs aren't written just for the compiler's benefit but to be read by other people; therefore, it is important to write them so that they can be understood by those other people. One very good reason for this is that more often than you might think, those “other people” turn out to be you, six months later.

[7] The // indicates the beginning of a comment, which is a note to you or another programmer. Everything on a line after // is ignored by the compiler.

Figure 3.1. A little numeric calculation
short i;
short j;
short k;
short m;

i = 5;
j = i * 3;             // j is now 15
k = j - i;             // k is now 10
m = (k + j) / 5;       // m is now 5
i = i + 1;             // i is now 6

To enter such statements in the first place, you can use any text editor that generates “plain” text files, such as the EDIT program that comes with DOS or the Notepad program in Windows. Whichever text editor you use, make sure that it produces files that contain only what you type; stay away from programs like Windows Write™ or Word for Windows™, as they add some of their own information to indicate fonts, type sizes, and the like, to your file, which will foul up the compiler.

Since we're already on the subject of files, this would be a good time to point out that the two main types of files in C++ are implementation files (also known as source files), which in our case have the extension .cpp, and header files, which by convention have the extension .h.[8] Implementation files contain statements that result in executable code, while each header file contains information that allows us to access a set of language features.

[8] Except that the C++ standard library header files supplied with the compiler, such as string, often have no extension at all. Also, other compilers sometimes use other extensions for implementation files, such as .cc, and for header files, such as .hpp.

Once we have entered the statements for our program, we use the compiler to translate the programs we write into a form that the computer can perform. As defined in Chapter 1, the form we create is called source code, since it is the source of the program logic, while the form of our program that the computer can execute is called an executable program, or just an executable for short.

As I've mentioned before, there are several types of variables, the short being only one of these types. Therefore, the compiler needs some explanatory material so that it can tell what types of variables you're using; that's what the first four lines of our little sample program fragment are for. Each line tells the compiler that the type of the variable i, j, k, or m is short; that is, it can contain values from – 32768 to +32767.[9]

[9] Other kinds of variables can hold larger (and smaller) values; we'll go over them in some detail in future chapters.

After this introductory material, we move into the list of operations to be performed. This is called the executable portion of the program, as it actually causes the computer to do something when the program is executed; the operations to be performed, as mentioned above, are called statements. The first one, i = 5;, sets the variable i to the value 5. A value such as 5, which doesn't have a name, but represents itself in a literal manner, is called (appropriately enough) a literal value.

This is as good a time as any for me to mention something that experienced C++ programmers take for granted, but which has a tendency to confuse novices. This is the choice of the = sign to indicate the operation of setting a variable to a value, which is known technically as assignment. As far as I'm concerned, an assignment operation would be more properly indicated by some symbol suggesting movement of data, such as 5 => i;, meaning “store the value 5 into variable i”. Unfortunately, it's too late to change the notation for the assignment statement, as such a statement is called, so you'll just have to get used to it. The = means “set the variable on the left to the value on the right”.[10]

[10] At the risk of boring experienced C programmers, let me reiterate that = does not mean “is equal to”; it means “set the variable to the left of the = to the value of the expression to the right of the =. In fact, there is no equivalent in C++ to the mathematical notion of equality. We have only the assignment operator = and the comparison operator ==, which we will encounter in the next chapter. The latter is used in if statements to determine whether two expressions have the same value. All of the valid comparison operators are listed in Figure 4.5.

Now that I've warned you about that possible confusion, let's continue looking at the operations in the program. The next one, j = i * 3;, specifies that the variable j is to be set to the result of multiplying the current value of i by the literal value 3. The one after that, k = j – i;, tells the computer to set k to the amount by which j is greater than i; that is, j – i. The most complicated line in our little program fragment, m = (k + j) / 5;, calculates m as the sum of adding k and j and dividing the result by the literal value 5. Finally, the line i = i + 1; sets i to the value of i plus the literal value 1.

This last statement may be somewhat puzzling; how can i be equal to i + 1? The answer is that an assignment statement is not an algebraic equality, no matter how much it may resemble one. It is a command telling the computer to assign a value to a variable. Therefore, what i = i + 1; actually means is “Take the current value of i, add 1 to it, and store the result back into i.” In other words, a C++ variable is a place to store a value; the variable i can take on any number of values, but only one at a time; any former value is lost when a new one is assigned.

This notion of assignment was the topic of quite a few messages with Susan. Let's go to the first round:

Susan: I am confused with the statement i = i + 1; when you have stated previously that i = 5;. So, which one is it? How can there be two values for i?

Steve: There can't; that is, not at one time. However, i, like any other variable, can take on any number of values, one after another. First, we set it to 5; then we set it to 1 more than it was before (i + 1), so it ends up as 6.

Susan: Well, the example made it look as if the two values of i were available to be used by the computer at the same time. They were both lumped together as executable material.

Steve: After the statement i = 5;, and before the statement i = i + 1;, the value of i is 5. After the statement i = i + 1;, the value of i is 6. The key here is that a variable such as i is just our name for some area of memory that can hold only one value at one time. Does that clear it up?

Susan: So, it is not like algebra? Then i is equal to an address of memory and does not really equate with a numerical value? Well, I guess it does when you assign a numerical value to it. Is that it?

Steve: Very close. A variable in C++ isn't really like an algebraic variable, which has a value that has to be figured out and doesn't change in a given problem. A programming language variable is just a name for a storage location that can contain a value.

With any luck, that point has been pounded into the ground, so you won't have the same trouble that Susan did. Now let's look at exactly what an assignment statement does. If the value of i before the statement i = i + 1; is 5 (for example), then that statement will cause the CPU to perform the following steps:[11]

[11] If you have any programming experience whatever, you may think that I'm spending too much effort on this very simple point. But I can report from personal experience that it's not necessarily easy for a complete novice to grasp. Furthermore, without a solid understanding of the difference between an algebraic equality and an assignment statement, that novice will be unable to understand how to write a program.

1.
Take the current value of i (5).

2.
Add one to that value (6).

3.
Store the result back into i.

After the execution of this statement, i will have the value 6.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset