Department of Engineering

IT Services

3. Variables and Constants

department image Programs need a way to store the data they use. Variables and constants offer various ways to represent and manipulate data. Constants, as the name suggests, have fixed values. Variables, on the other hand, hold values which can be assigned and changed as the program executes.

Variable types

Every variable and constant has an associated type which defines the set of values that can be legally stored in it. Variables can be conveniently divided into integer, floating point, character and boolean types for representing integer (whole) numbers, floating point numbers (real numbers with a decimal point), the ASCII character set (for example 'a', 'b', 'A') and the boolean set (true or false) respectively.

More complicated types of variable can be defined by a programmer, but for the moment, we will deal with just the simple C++ types. These are listed below:

int

to store a positive or negative integer (whole) number

float

to store a real (floating point) number

bool

to store the logical values true or false

char

to store one of 256 character (text) values

Declaration of a Variable

A variable is introduced into a program by a declaration which states its type (i.e. int, float , bool or char) and its name, which you are free to choose. A declaration must take the form:
   type          variable-name;

   int           count;
   float         length;
   char          firstInitial;
   bool          switched_on;
or:
   type             variable1, variable2, ... variableN;

    float           base, height, areaCircle;
    int             myAge, number_throws;

The variable name can be any sequence of characters consisting of letters, digits and underscores that do not begin with a digit. It must not be a special keyword of the C++ language and cannot contain spaces. C++ is case-sensitive: uppercase and lowercase letters are considered to be different. Good variable names tell you how the variable is used and help you understand the flow of the program. Some names require two words and this can be indicated by using the underscore symbol (_) or using an uppercase letter for the beginning of words.

Storage of variables in computer memory

When you run your program it is loaded into computer memory (RAM) from the disk file. A variable is in fact a location in the computer's memory in which a value can be stored and later retrieved. The variable's name is merely a label for that location - a memory address. It may help to think of variables as named boxes into which values can be stored and retrieved.

The amount of memory required for the variables depends on their type. This can vary between machines and systems but is usually one byte (8 bits) for a char variable, four bytes for an int and four bytes for a float. This imposes limits on the range of numbers assigned to each variable. Integer numbers must have values in the range -2147483648 to 2147483647 (i.e. ±231). Floats must be real numbers with magnitudes in the range 5.9 x 10-39 to 3.4 x 1038 (i.e. 2-127 to 2128). They are usually stored using 1 bit for the sign (s), 8 bits for the exponent (e) and 23 bits for the mantissa (m) such that the number is equal to s x m x 2e. The ratio of the smallest and largest numbers that can be correctly added together must therefore be greater than 2-23≈10-7 (i.e. 7 digits of accuracy). This depends only on the number of bits used to represent the mantissa.

If an application requires very small or large numbers beyond these ranges, C++ offers two additional data types for integers and floating point numbers: long and double. Variables of type double require double the amount of memory for storage but are useful when computing values to a high precision.

Back to top