C++ - Chapter 2 - Variables


Remember math? 3 + 5 = x? If you've passed 2nd grade, you'll know that x=8, right? In this example x is a variable. You will use variables significantly in all programming languages. You can use a variable to store a username, an identification number, the amount of money you have--what ever you choose. You can also use variables to calculate additional variables. Let's look deeper...

Definitions (in layman's terms)

Variables are storage bins in the computer's memory for you to store, retrieve, and manipulate data.

Arithmetic Operators are used to perform mathematic operations on variables.

Arithmetic Operators
Operator Operation
+ addition
- subtraction
* multiplication
/ division
% modulo division

Type Specifiers are special keywords used to define the type of data that is to be stored in a variable.

Common Type Specifiers
Type Specifier Variable Type Bytes Used Values
int integers 4 -2,147,483,648 to 2,147,483,687
double floating point numbers 8 1.7E (+/-)308
bool logical values 1 true or false
char characters 1 -127 to 128
Additional Type Specifiers
Type Specifier Variable Type Bytes Used Values
short int integers 2 -32,768 to 32,767
unsigned short int integers 2 0 to 65,535
unsigned int integers 4 0 to 4,294,967,295
long int integers 4 –2,147,483,648 to 2,147,483,647
unsigned long int integers 4 0 to 4,294,967,295
float floating point numbers 4 3.4E (+/-)38
long double floating point numbers 8 1.2E (+/-)4932
unsigned char characters or bytes 1 0 to 255
#include <iostream>

using namespace std;

int main (void)
    cout<<"Hello World!"<<endl<<endl;

    return 0;

int x, y; is called an initialization line. This line of code specifies that there are two variables named x and y and both are of type int.

x = 3; stores the value "3" into the variable x. When storing a value into a variable container, the variable must always be on the left hand side and the value on the right. For instance, 3 = x is incorrect.

int a = x + y; shows that you can initialize a variable in-line as well as perform arithmetic on the variables. This line initializes a variable of type "int", named "a", and assigns it a value of "8" (3 + 5).

// addition is called an in-line comment. These are not interpreted by the compiler, but are useful for any humans (including yourself) who read and try to interpret your source code. It is always a good idea to comment your source code so you can know what's going on if you ever have to edit your code later.

#include <iostream>

using namespace std;

int main (void)
	int a = 3, b = 7;
	int k, l, m, n, p;
	double o;

	k = b + a;                 // addition       (7 + 3) = 10
	l = b - a;                 // subtraction    (7 - 3) = 4
	m = b * a;	           // multiplication (7 * 3) = 21
	n = b / a;	           // division       (7 / 3) = 2
	o = (double)b / (double)a  // division (7.0 / 3.0) = 2.33333
	p = b % a;	           // modulo         (7 % 3) = 1

	cout<<"k = "<<k<<endl;
	cout<<"l = "<<l<<endl;
	cout<<"m = "<<m<<endl;
	cout<<"n = "<<n<<endl;
	cout<<"o = "<<o<<endl;
	cout<<"p = "<<p<<endl;

	return 0;
k = 10
l = 4
m = 21
n = 2
o = 2.333333
p = 1

Notice that when you store the division of two numbers as an integer, the fractional component is truncated. This means that only the whole-number (integer) part is used and fractional parts are neglected. Inserting the (double) modifier before a variable is called type-casting. This casts the variable into a new memory type. This is done to prevent what is referred to as mixed-mode operation. Avoid mixed-mode operations if at all possible as it leads to loss of precision in the values. Storing the division into a double value preserves the fractional component as can be seen by the variable "o".

When you perform a modulo divide, you are computing the remainder of a division. For instance 10 % 6 = 4. 4 is the remainder of 10 divided by six. The modulo operator only works on integer data types.

< Back - Your First Program Index Next - Working with Text >