Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use

Variables and Constants

Important Terms:

  A variable is a named memory location which temporarily stores data that can change while the program is running.

A constant is a named memory location which temporarily stores data that remains the same throughout the execution of the program. 

The type of a variable indicates what kind of value it will store.

The name of a variable is known as its identifier.

A variable can be given a value through an assignment statement.

C++ recognizes eleven built-in data types which are designated by reserved words:

char 
short 
float 
long double 
unsigned int 
unsigned long
int
long
double
unsigned char
unsigned short


Variables of different types occupy different amounts of memory space and are described as having different sizes.

** The basic C++ types that are included in the AP Computer Science subset are int, double, bool and char.

 

Variables Most Often Used

Data Type  C++ Keyword  Stores  Bytes of Memory  Range of Values
Character  char  1 character 1   1 character
Short integer  short   Integers  2 -32,768 to 32,767
Integer  int  Integers 4   -2,147,483,648 to 2,147,483,647
Long Integer   long  Integers 4 -2,147,483,648 to 2,147,483,647
Float float  Decimal values to 7 decimal digit precision   4 3.4e-38 to 3.4e38
 positive and negative
Double double Decimal values to 15 decimal digit precision  8 1.7e-308 to 1.73e308
 positive and negative
Boolean  bool  Boolean (Logical) values    True or False

 

Rules for assigning variables:

Assign short, int or long data types when you are sure a variable is an integer number (NO decimal points).  While these three integer choices are available, based upon the size of the numbers you are using, we will be using only int.

Assign float or double when decimals are needed.  We will primarily be using double, as suggested by the AP Computer Science subset.

Assign char if the variable will always contain ONE character of data.  This means only one letter (or character).

 

Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use