When interacting with the computer, a user enters values,
changes them, retrieves them, etc. You can monitor most of these operations as
they relate to your application. To start, you can display values to the user
using the
Write and the WriteLine methods of the Console class of the
System namespace. A variable is a value that you want to use in your program without primarily knowing
how much exactly that value will be when the program runs.

The Stack

A computer is equipped with a memory area known as Random
Access Memory or RAM that we will illustrate as a group of small boxes:

When a computer comes up (boots), part of this
memory is filled with some values that primary applications, such as the
operating system, need. Since the other parts of the memory are not
used, they may be filled with garbage, that is, insignificant values you
can't use because they mean nothing:

The memory (RAM) is huge enough to contain various
values. The particularity of this memory is that it is temporary. In
other words, it holds its values for a while and looses them when they
are not needed anymore (an example is when the computer shuts down, the
RAM is emptied). Normally, the operating system is in charge of the
management of this whole memory, including when to put
("push") things in it and when to remove ("pop")
things from it. This memory is also made available to you so that, while
your application is "running", you can "put" things
in it.

To better manage its memory, the computer sets aside
an amount of memory space for programs like yours so that, when your
application runs, it can use that memory to put values in it. This area
of memory is called the stack. As you may guess from its name, values
are entered ("pushed") in it from a side, like entering a
tunnel. Another particularity of this scenario is that the values are
removed ("popped") from another side:

Keep in mind that the operating system (OS) removes any
value when it (the OS) judges or finds out that the value(s) is(are) not used
or needed anymore. When your application runs, it will use (part of)
this memory space as necessary. One way you can use it is to put values
in the space as you judge necessary.

Variable Declaration

Imagine you are creating a program that will
calculate the weekly salary of various employees. This would be done by
multiplying the weekly hours by the hourly salary:

Weekly Salary = Weekly Hours * Hourly Salary

This means that you will likely need an amount of
memory space to store a value for the weekly salary (a monetary value),
another amount to store a value for the weekly hours (a regular number)
and another to store a value for the hourly salary (another regular
number).

In order to store these values in the memory, you
must tell the computer how much space each value will need. This is
because the compiler cannot predict what your program will need. When
specifying the amount of space, the compiler will reserve an amount of
space for each value you specify, in the stack memory. To locate that memory, you must give it a name.
The name is not stored as a value; it only allows you to be able to
refer to the space memory that was reserved for your variable.

Reserving an area of memory is referred to as
declaring a variable. There are rules you must follow when doing
this. The general formula used to declare a variable in C is:

TypeOfVariableVariableName;

When you ask the compiler to reserve an amount of memory
space for your variable, you must tell it how much memory the
variable will need. This is because different values are meant to do different things. For example, while a number can be used to hold the
salary of an employee, a word or a group of words is necessary to hold
the name of an employee. The second piece of information you must specify, as
mentioned already, is a name that you will use to refer to the memory
that was reserved for a particular variable.

If you want to declare more than one variable of the
same type, you can declare each with the type, its name, and its
semi-colon. The formula to follow would be:

When using the various necessary variables in your programs, you will need to identify each one of them. A variable is primarily recognized by its name. C++ provides rules for naming items in your program.

The name of a variable:

Must start with an underscore “_” or a letter, lowercase or uppercase, such as a letter from a to z or from A to Z. Examples are Name, gender, _Students, pRice

Avoid starting the name of a variable with two underscores; sometimes, the compiler would think that you are trying to use one of the words reserved for the compiler.

C++ is case-sensitive; this means that CASE, Case, case, and Case are four completely different words. To make your programming experience easier and personal, you can add your own rules to those above. Some (most) companies also adopt a naming convention throughout their documentation.

Throughout this book, a name will:

Start in uppercase, whether it is a variable, a function, a structure, or a class. In some situations, a name will start with an underscore, when useful. Examples are Country, Printer, _Number

Start each element of its combined names in uppercase. Examples are FirstName, DateOfBirth