|
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.
|
|
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.
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:
TypeOfVariable VariableName;
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:
TypeOfVariable Variable1;
TypeOfVariable Variable2;
TypeOfVariable Variable_n;
An alternative is to use one data type followed by
the names of variables that would share this type. The names of
variables must be separated by commas. The formula to follow would be:
TypeOfVariable Variable1, Variable2, Variable_n;
If you want to declare different variables that use
different data types, you can declare each with the type, its name, and
its semi-colon, on the same line. The formula to follow would be:
TypeOfVariable1 Variable1; TypeOfVariable2 Variable2; TypeOfVariable3 Variable3;
A better alternative is to declare each variable on
its own line. This makes the program easier to read. The formula to
follow would be:
TypeOfVariable1 Variable1;
TypeOfVariable2 Variable2;
TypeOfVariable3 Variable3;