How to Use C++ Data Types and Variables

Examining C++ Variables and Their Effect on Memory Usage

© Mark Alexander Bain

Feb 21, 2009
Using Variables with C++, Mark Alexander Bain
C++, like all programming languages, needs a way of working with values (such as numbers). That's done by using variables.

Variables and constants are used in the majority of programming languages and C++ is no different. Both variables and constants are used to store information within a program and this information only exists whilst the program is running.

Therefore variables and constants are very similar, but with one major difference. Once set the value of a constant cannot be changed and so, before moving on to constants, it is important to understand C++ variables.

What is a C++ Variable?

A C++ variable is, quite simply, a location in a computer's memory and a program can either read from this memory location or write to it. The programmer allocates some of the computer's memory for use by a variable when they declare it in their code.

Declaring a Variable in C++

A C++ variable is declared by telling the program:

  • the data type to be used
  • the name of the variable

for example, to declare a new integer the code would be:

#include <iostream.h>
int main () {
int x = 3;
int y, z;
y = 4;
z = x * y;
cout << z << "\n";
return 0;
}

Here 3 and 4 are multiplied together to produce 12 (which is output to the screen). Interestingly all three values take up the same amount of memory.

C++ Variables and Memory Usage

The amount of memory used by a variable does not depend on the value of the variable. It depends on its data type:

int x = 1;
int y = 100;
int z = 1000000;
cout << sizeof(x) << " " << sizeof(y) << " " << sizeof(z) << "\n";

In each case the sizeof method will show that 4 bytes of memory is used by an integer regardless of the value of that integer. However, the memory used by the integer can be changed by using the long and short key words:

short int x = 1;
long int z = 1000000;
cout << sizeof(x) << " " << sizeof(z) << "\n";

In this case the short integer will use 2 bytes and the long integer will use 4. This, of course, means that the maximum number that a short integer can hold is half that of a long integer and an interesting thing happens if the maximum number is exceeded:

C++ and the Sign of a Variable

The maximum value that a small integer can hold is 65535 but it is worth seeing what happens if this is exceeded:

short int x = 65535;
x += 2;
cout < x << "\n";

The program will not crash in this instance but neither will it return 65537. Instead it will return 1. This is because the short integer is unsigned (or positive) by default and can contain the values 0 to 65535. When the maximum value is exceeded it just loops back to the minimum. If negative numbers are requires then a signed integer should be used:

signed short int x = 32767;

The size of the integer will still be 2 bytes but now the range is -32768 to 32767.

C++ Variable Types and Ranges

Each variable type has its own predefined memory usage. This, therefore,limits what can be stored in any variable:

  • bool: True or False (1 byte)
  • char: 256 characters (1 byte)
  • double: 2.2e-308 to 1.8e308(8 bytes)
  • float: 1.23-38 to 3.4e38 (4 bytes)
  • signed long int: -2,147,483,648 to 2,147,483,647 (4 bytes)
  • unsigned long int: 0 to 4,294,967,295 (4 bytes)
  • signed short int: -32,768 to 32,767 (2 bytes)
  • unsigned short int: 0 to 65,535

It is then just a matter of the programmer ensuring that they use the correct data types for their variables. By doing that they will ensure that the program does not produce incorrect results due to any memory limitations.

Summary

Variables are an important part of any C++ program and are defined by:

  • assigning a data type
  • assigning a name

The variables are easy to use but each one uses a certain amount of memory (and that's dependent on the data type) and and variable may only use a very strict range of values (and again that's dependent on the data type). However, provided that the limitations are observed correctly, the programmer can manage the data used by a program and its memory usage in an efficient and professional manner.


The copyright of the article How to Use C++ Data Types and Variables in C Programming is owned by Mark Alexander Bain. Permission to republish How to Use C++ Data Types and Variables in print or online must be granted by the author in writing.


Using Variables with C++, Mark Alexander Bain
C++ Code Using Variables, Mark Alexander Bain
Compiling and Running a C++ Program, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo