Basic input/output

Here is a snippet that reads two string variables from the console, and writes them out. Not much to write home about… Explanation follows.


#include <iostream>
#include <string>

using namespace std;

int main(){

string fname, lname;
cout << "Enter first name: ";
cin >> fname;
cout << "Enter lastname: ";
cin >> lname;

cout << "Hello " << fname << " " << lname << endl;

return 0;

}

Now what goes on here? First, the #include directives tells the compiler what parts of code to include that does not reside in the file itself. In this example we use components from the c++ standard library, that comes with every decent compiler. More about this stuff later.
The “using namespace std” thing is a way to avoid typing more than necessary. cout for example belongs to the iostreams part of the standard library we included. So if we did not define “using namespace std”, we must have written std::cout instead, and who wants to do that?

cout prints stuff, cin gets stuff. The << and >> is a logical way to visualize what direction each of them takes. cin >> “put this inside”. cout << “puke this out”.

C++ itself does not have a variable type for strings, so we have to include the <string> from the standard library as well, so the compiler knows what a string actually is.

Think of the standard library as a (big) collection of pre-coded utilities, that you can include, and use in your own programs, so you don’t have to figure out how to actually make wheels. It’s done.

main() is the starting point for every program, so to compile one, you have to put some code inside this “block”, surrounded by curly braces.
It is common to return a value from main, so that if all is well, 0 is returned. therefore when defining main(), like when defining variables, a datatype is required. So int main(), means that main() will return an int, in this case (and most others) the int 0. The word “return” is a keyword, that is reserved for the purpose of just that. To return something, when the statement is reached.

So this snippet defines two string variables, prints something, gets some input, and prints out some more.

Babysteps, the compiler

To get our C++ program to run, we have to translate it into a machine language program, or let’s say an executable. To do this we need a couple of things.

Let’s start with a C++ compiler. To keep it simple for now, we’ll drop some intermediate steps and say that what the compiler primarily does is:

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.
Each complete operation understood by the compiler is called a statement, and ends with a semicolon (;).
An example could be:
int age = 32;

This tells the compiler to make room for an integer in memory and assign the variable “age” the value of 5. This is called a “statement”. Values are assigned from right to left, so in case of:
age = age + 10;
The calculation of age + 10 is done before assigning the new value back into the already initialized age variable of type int.

Some definitions

A machine instruction is one of the fundamental operations that a CPU can perform. Some examples of these operations are addition, subtraction, or other arithmetic operations; other possibilities include operations that control what instruction will be executed next. All C++ programs must be converted into machine instructions by a compiler before they can be executed by the CPU.

An assembly language program is the human-readable representation of a set of machine instructions; each assembly language statement corresponds to one machine instruction. By contrast, a C++ program consists of much higher-level operations which cannot be directly translated one-for-one into machine instructions.

What is a digit?

A digit is one of the characters used in any positional numbering system to represent all numbers starting at 0 and ending at one less than the base of the numbering system. In the decimal system, there are ten digits, 0 – 9, and in the hexadecimal system there are sixteen digits, 0 – 9 and a – f.

A binary number system is one that uses only two digits, 0 and 1.

A hexadecimal number system is one that uses sixteen digits, 0 – 9 and a – f.

To C or not to C

Bjarne Stroustrup, on page 169 of his book, The Design and Evolution of C++, he says:

“Learn C++ first. The C subset is easier to learn for C/C++ novices and easier to use than C itself.”

So let’s start there.