Variables and Data Types

A variable can be defined as an area of memory that is set aside for the storage of a particular value. Each variable must be identified using a unique name, or identifier. A valid identifier is an alpha-numeric string consisting of one or more letters, digits or underscore characters. Spaces and punctuation characters cannot be used, and variable names must begin with a letter or an underscore character. A variable cannot have the same name as one of the C++ reserved keywords . Keywords are reserved identifiers that have special meaning in C++. They cannot be used as identifiers in your program. The following table lists the reserved keywords.



C++ Keywords
asm default float operator static_cast union
auto delete for private struct unsigned
bool do friend protected switch using
break double goto public template virtual
case dynamic_cast if register this void
catch else inline reinterpret_cast throw volatile
char enum int return true wchar_t
class explicit long short try while
const export mutable signed typedef  
const_cast extern namespace sizeof typeid  
continue false new static typename  


C++ is case sensitive. That means that the variable names myvar, MyVar and MYVAR are referring to three different variables.



Data types

Variables of different types will occupy different amounts of memory. Computer memory is allocated to variables in blocks of one or more bytes, depending on the data type. A single byte, for example, can store one alpha-numeric character, an unsigned integer with a value ranging from 0 to 255, or a signed integer with a value ranging from -128 to +127. Two or more bytes are required to store larger integer values and floating point numbers. The standard C++ data types, together with the range of values they can represent, are listed below.



C++ Data Types
Data typeDescriptionSize (bytes)Range
char Character or small integer 1 signed:
-128 to 127,

unsigned:
0 to 255
short int (short) Short Integer 2 signed:
-32768 to 32767,

unsigned:
0 to 65535
int Integer 4 signed:
-2147483648 to 2147483647,

unsigned:
0 to 4294967295
long int (long) Long integer 4 signed:
-2147483648 to 2147483647,

unsigned:
0 to 4294967295
bool Boolean value 1 true or false
float Floating point number 4 3.4e +/- 38 (7 digits)
double Double precision floating
point number
8 1.7e +/- 308 (15 digits)
long double Long double precision
floating point number
8 1.7e +/- 308 (15 digits)
wchar_t Wide character 2 or 4 1 wide character


Note that the Size and Range values shown above are those found on most 32-bit systems.



Declaring variables

Before we can use a variable in a C++ program, we have to declare it. We do this using a statement such as the one shown below.

int myInt;

The declaration consists of a specifier (int) which signifies the data type of the variable, and a unique identifier (myInt) which allows us to refer to the variable by name later in the program. When declaring more than one variable of the same type, they may be declared in a single statement by separating the identifiers using commas, as shown below.

int myIntA, myIntB, myIntC;

All of the integer data types (char , short , long and int) can be either signed or unsigned. Signed integer variables can be used to represent both positive and negative values. The appropriate keyword (signed or unsigned) Signed types can represent both positive and negative values, whereas unsigned types can only represent absolute values. This can be specified by using one of the keywords signed or unsigned in front of the type name, as shown below.

unsigned short int ageInYears;
signed int degreesC;

If an integer type variable is declared without being specified as either signed or unsigned, most compilers will assume the variable to be signed. An exception to this rule is the char type, which must be declared as signed or unsigned if it is to be used to store a numeric value. Otherwise, the compiler will treat the char variable as an alpha-numeric character or a control code.

The keywords short and long can be used on their own in place of the short int and long int data types respectively. The declarations below are equivalent.

short Age;
short int Age;

In the same manner, the keywords signed and unsigned can be used in place of signed int and unsigned int respectively. The declarations below are equivalent.

unsigned Year;
unsigned int Year;

The following short program demonstrates the declaration and use of program variables.

// Example Program 1 - declaring and using variables

#include <iostream>
using namespace std;

int main ()
{
  // declare the variables:
  int a, b, result;
  string s;

  // initialize variables a and b
  a = 5;
  b = 2;

  // perform some arithmetic operations
  a = a + 1;
  result = a - b;

  /* print out the value of result, wait for the user
     to press ENTER, then end the program */
  cout << result;
  cout << "\n\nPress ENTER to continue.";
  getline( cin, s );
  return 0;
}


The output from example program 1

The output from example program 1



Variable scope

As we have seen, the variables that are used in a C++ program must be declared before they can be used. The location of the variable declaration within the program code, however, will determine the scope of the variable. A variable can have either global or local scope. A global variable is one that is declared outside of all functions, while a local variable is one that is declared within the body of a function or code block. The following short program illustrates the difference between local and global variables.

// Example Program 2

#include <iostream>
using namespace std;

string s;  // a global variable

int square (int x)
{
  return x * x;
}

int main ()
{
  // local variables
  int myInt, result;
  string myString;

  myInt = 5;
  result = square(myInt);
  myString = "The value of result is: ";
  cout << myString;
  cout << result;
  cout << "\n\nPress ENTER to continue.";
  getline( cin, s );
  return 0;
}


The output from example program 2

The output from example program 2

A global variable can be referenced from anywhere in the program. A local variable can only be referred to from within the body of the function in which it is declared. In the example above, therefore, the value of the variable result cannot be referenced from within the square() function.



Initialising variables

When a variable is first declared, its value is by default undetermined. It is possible, however, to initialise (assign a value to) the variable in the same statement that declares the variable. There are two ways to do this in C++, both of which are shown below.

int x = 10;
/* this statement declares an integer variable called x
and assigns a value of 10 to it */

int x (10);
/* this statement does exactly the same thing as
the previous example */

The following program demonstrates the principle.

// Example Program 3

#include <iostream>
using namespace std;

int main ()
{
  string s;
  int x = 5;   // initial value = 5
  int y (10);  // initial value = 10
  int result;  // initial value undetermined

  result = x * y;
  cout << "The product of x and y is: ";
  cout << result;
  cout << "\n\nPress ENTER to continue.";
  getline( cin, s );
  return 0;
}


The output from example program 3

The output from example program 3



String variables

String variables are variables that can store two or more alpha-numeric characters. C++ provides the string class which, whilst not a fundamental type in the same way as an int or char, for example, can be declared and initialised in a similar way. In order to declare and use string variables, we must have access to the std namespace. The following program declares, initialises and outputs the contents of two string variables.

// Example Program 4

#include <iostream>
using namespace std;

int main ()
{
  string stringA = "This is string A.";
  string stringB ("This is string B.");
  string s;

  cout << stringA;
  cout << "\n\n";
  cout << stringB;
  cout << "\n\nPress ENTER to continue.";
  getline( cin, s );
  return 0;
}


The output from example program 4

The output from example program 4

Strings can be declared with or without an initial value, and may be initialised with any valid string literal.