Home

Variables

 

Introduction

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:

Random Access Memory (RAM)

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:

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;

Names in C++’

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
  • Can include letters, underscore, or digits.
    Examples are: keyboard, Master, Junction, Player1, total_grade, _Score_Side1
  • Cannot include special characters such as !, %, ], or $
  • Cannot include an empty space
  • Cannot be any of the reserved words
  • Should not be longer than 32 characters (although allowed)
 

Keywords

A name can consist of one word such as country. A name could also be a combination of more than one word, such as firstname or dateofbirth.

The C++ and the C++/CLI languages have a list of words reserved for its own use and you must not use any of these words to name your objects or functions. The reserved words are:

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
 

Home Copyright © 2006-2007 FunctionX, Inc. Next