The usefulness of the
"Hello World"
programs shown in the previous section is quite questionable.
We had to write several lines of code, compile them, and then execute the resulting program just to obtain a simple sentence written on the screen as result! 😲.
It certainly would have been much faster to type the output sentence by ourselves rather than having to go through all that process.
However, programming is not limited only to printing simple texts on the screen.
In order to go a little further on and to become able to write
programs that perform useful tasks that really save us work we need
to introduce the concept of
variable.
Let us think that I ask you to retain the number
5
in your mental memory, and then I ask you to memorize also the
number
2
at the same time. You have just stored two different values in your
memory.
Now, if I ask you to add
1
to the first number I said, you should be retaining the numbers
(that is 5+1) and
2
remain in your memory. Values that we could now for example subtract
from the first number and obtain
4
as result.
The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables in a seconds.
The same process can be archieved in C++ with the following instruction set:
a = 5;
b = 2;
a = a + 1;
result = a - b;
Obviously, this is a very simple example since we have only used two small integer values, but consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them.
Therefore, we can define a variable as a portion of memory to store a determined value.
Each variable needs an
identifier
that distinguishes it from the others, for example, in the previous
code the variable identifiers were
a,
b
and
result, but we could have called the variables any names we wanted to
invent, as long as they were valid identifiers.
Example
A valid identifier is a sequence of one or more letters, digits or
underscore characters
(_).
Neither spaces nor punctuation marks or symbols can be part of an identifier.
Only letters, digits and single underscore characters are valid.
In addition, variable identifiers always have to begin with a letter.
They can also begin with an underline character
(_), but in some cases these may be reserved for compiler specific
keywords or external identifiers, as well as identifiers containing
two successive underscore characters anywhere.
In no case they can begin with a digit.
Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler's specific ones, which are reserved keywords.
The standard reserved keywords are:
| Keyword | Keyword | Keyword | Keyword |
|---|---|---|---|
| asm | auto | bool | break |
| case | catch | char | class |
| const | const_cast | continue | default |
| delete | do | double | dynamic_cast |
| else | enum | explicit | export |
| extern | false | float | for |
| friend | goto | if | inline |
| int | long | mutable | namespace |
| new | operator | private | protected |
| public | register | reinterpret_cast | return |
| short | signed | sizeof | static |
| static_cast | struct | switch | template |
| this | throw | true | try |
| typedef | typeid | typename | union |
| unsigned | using | virtual | void |
| volatile | wchar_t | while |
Additionally, some compilers also support additional keywords that are not part of the C++ standard but are supported by the compiler for specific purposes.
Declaration of variables
In order to use a variable in C++, we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float...) followed by a valid variable identifier. For example:
int a;
float mynumber;
These are two valid declarations of variables. The first one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in the program.
If you need to declare several variables of the same type and want to save some writing work you can declare them in a single line by separating their identifiers with commas. For example:
int a, b, c;
This declares three variables (a, b and c), all of type int, and has exactly the same meaning as:
int a;
int b;
int c;
The integer data types char, short, long and int can be either signed or unsigned depending on the range of numbers that we want to store. Signed types can represent both positive and negative numbers, while unsigned types can only represent positive numbers (including zero). This is specified by the keywords signed and unsigned. By default, most integer types are signed unless explicitly specified as unsigned.
To see what variable declaration looks like in action within a program, we are going to see the full code of our second program:
// my first program in C++
#include <iostream>
using namespace std;
int main(){
int length = 5;
int width = 2;
int area = length * width;
cout<<area;
return 0;
}
It is one of the simplest programs that can be written in C++, but it already contains the fundamental components that every C++ program has. We are going to look line by line at the code we have just written:
#include <iostream>
This line is a directive to the preprocessor, which includes the
iostream standard file. This file contains the declarations of the
basic standard input-output library in C++, and it is included
because its functionality is going to be used later in the program.
using namespace std;
This line tells the compiler to use the std namespace. All the
elements of the standard C++ library are declared within what is
called a namespace with the name std. So in order to access its
functionality we declare with this expression that we will be using
these entities.
int main()
This line defines a function named main, which is the function where
the program begins its execution. The int before main indicates that
the function returns an integer value.
int length = 5;
This line declares a variable named length of type int and assigns
it the value 5. The variable length is used to store the length of
the rectangle.
int width = 2;
This line declares a variable named width of type int and assigns it
the value 2. The variable width is used to store the width of the
rectangle.
int area = length * width;
This line declares a variable named area of type int and assigns it
the result of the multiplication of the variables length and width.
The variable area is used to store the area of the rectangle.
cout << area;
This line uses the cout object to output the value of the variable
area to the standard output (usually the screen).
return 0;
This line returns the value 0 to the calling process, which is
usually the operating system. A return value of 0 usually means that
the program has terminated successfully.
Now that you have learned the how to inventing variables in C++ programming. Lets learn someting about Data type in C++ in the next section.