©1994-2003 Kevin Boone
Home     Section index     K-Zone home C++ tutorial (under construction)

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
fundamentals
contents
types
C++ tutorial: variables


Variables can store numbers whose values can change
It is all very well to specify calculations with pre-defined numbers, like '2' or '3', but what about numbers whose values are not known in advance? For example, suppose the program uses numbers entered by the user? Inputting data will be covered later; this section simply introduces the use of variables.

In C/C++ a variable is any piece of data with a name. Variables can contain numbers (various types), text or complex information. A very simple use of variables, to store integer numbers, is demonstrated in the program vars1.cpp. This calculates '4 + 6', but uses variables to store the numbers, rather than directly entering the numbers. The advantage of doing this will become clear later, it if is not now.

The first important line in the program is

int i = 4;

In this line 'int' means 'integer', i.e., 'i' is to be an integer variable. In this case 'i' is initially set to '4'. 'j' is initially set to '6'. Of course, because these are variables, the program can change these values later if required.

You don't have to set a value to a variable when it is created. For example, the variable may be set later in response to user input. In this case, we could have written:

int i;

meaning: 'i' is to be an integer variable, but its value is not yet known. The program could later set a particular value to the variable using a line like:

i = 99;

Variables can be used like the data they store
For example, the line

cout << i << " + " << j << " = " << i+j << "\n";

adds the variables 'i' and 'j' and prints the result exactly as if 'i' and 'j' had been pre-defined numbers like '2' or '3'.

Variables can store any type of data that the compiler knows about
Here are some other variable definitions:

float Speed = 30.0; // A standard real number variable
char NextCharacter = 'x'; // A single character variable
char Message[] = "Press any key..."; // A character string variable

As ever, things are a bit more complicated than they appear. For example, real numbers can be stored with various precisions. 'float' defines a variable of standard precision, 'double' is a variable of higher precision, and 'long double' the highest precision available. With a modern compiler on a PC, a 'long double' variable is good for about 20 decimal places. Incidentally, the word 'float' stands for 'floating point', the technique used to represent factional numbers in the computer.

For more information about data types and their properties, see the reference section.

Variables must be defined before use
If the compiler encounters an instruction of the form 'i + j' before a line that says what 'i' and 'j' are, it will complain. Why? Because it does not know what 'i' and 'j' are. They could be integers, they could be real numbers, they could be characters. Moreover, even if the compiler could have a guess at the type of data being stored, it still does not what values these variables have. C++ is very strict about this rule, which is good. It allows the compiler to pick up all sorts of trivial errors that would otherwise cause the program to fail. It is a big improvement over languages like BASIC, where a new variable can be introduced simply by the programmer making a spelling mistake.

Variables have fixed type
This means that once a variable has been defined, the type of data stored will never change. Of course the value might change, this is the meaning of 'variable', but an integer variable will never become a real number. There is perhaps an exception to this rule when the variable are objects, rather than simple data elements like integers, but that need not concern us at this stage.