These are named boxes that hold something. Similar to the memory cells described earlier, they have four properties. These are a name, location, type and contents. Variables can contain different values at different times.
The name of a thing in C++ is called an identifier. An identifier can be any string of characters, but must start with a letter or an underscore, then be followed by any number of digits, letters or underscores. An identifier cannot be the same as one of the keywords of the language. They are case sensitive, so my_name and My_Name are different. Choose identifiers to describe what the object is for, not what it contains. For example, my_integer is a poor choice, number_of_books would be better.
This is the memory address where the variables contents begin. We will look at this much more closely when we discuss pointers.
This is the variables size and dictates what it can hold. While there are exception, the different types of variables cannot hold each others contents.
This is what is in the memory location used by the variable.
Variables have two kinds of value. They are called the Right Hand Side (RHS) and the Left Hand Side (LHS). These refer to the sides of the assignment operator, = . The LHS value is the location, its memory address. The RHS value is the contents. We will see more of this as we get into pointers.
This sets up a variable and specifies its type and name. There is a difference between the declaration and definition of a variable. The declaration tells the compiler the name and type of the variable. The definition sets up the memory space for the variable. These sometimes happen at the same time, sometimes at different times.
When a variable is created and space is set up for it, the contents are undefined. This means that the contents of the variable is whatever was in that space before it was given to the variable. There is some bit pattern in there, but nothing specific. The variable is not set to zero. So it is very important for you to set the variable as soon as you make it. This setting is called initialization.
In C++, variable are declared by the following syntax:
type_name variable_name [,variable_name];
type_name variable_name=initial_value;
int one;
int two,three;
int one = 2, two = 9;
There are several legal values for the type_name part above. These include int, float, char, double, long, short.
These are indicated by the keyword int. These are ordinary integer values. You can modify the integer by using these other keywords.
signed | The default case, the variable can be positive or negative |
---|---|
unsigned | This means the variable can only be positive, allows somewhat larger numbers |
short | This used to mean the variable was half the size of an int, usually now it means the same as int |
long | This makes an integer that is twice as long as an int |
Some combinations of these are allowed. An example would be
unsigned long int joe;
These are one byte (typically) variables that contain the ASCII code for
a character. For example, if a char variable is assigned the letter A,
char x='A'; // note the single quotes
The integer value would be 65. You can do arithmetic on characters.
So after doing
c=c+1;
C would contain 66, which is the letter 'B'. There are special characters
and here is how you denote them
'\n' | A newline character |
---|---|
'\t' | tab |
'\0' | NUL end of string marker |
'\\' | a backslash |
'\'' | a single quote |
'\nnn' | Any ASCII character, the nnn is the octal number of the character |
Boolean variables are either true or false. There is a convention
in C++ that the number 0 is false and all other numbers are true.
But in most cases it is better to use the actual Boolean variables rather
than the integer convention.
So given
bool flag;
This can be set to true or false;
flag = false;
And used in if statements
if(flag) {do true stuff }
These are used to represent numbers with decimal points.
float f=3.5;
Be careful of mixing floats and ints together. In the expression
int i=12;
float f=3.4;
print(i/f);
will give 4, rather than 3.52.
This is because, when the types are mixed, everything is made into
an integer. And when a float is converted, it is truncated. So 3.4 becomes
3. To fix this, you have to tell the compiler to make the integers into
floats like this.
print ( (float) i / f);
The (float) is a cast. This temporarily changes the thing after it into the type in the parentheses. So since i is an integer, this changes the integer value in i (12) into a floating point value (12.0) and then the expression works like you would expect.
Expressions are combinations of variables and operators. Statements
are built out of expressions. A single variable is an expression and an
expression is an expression. So, lets make some variables and then make
some expressions.
int a,b,c,d;
a+b
c - d
b - ( a + b)
C++ has a large number of operators. They include arithmetic, Boolean
and logical operators. Operators come in two flavors, binary and
unary. Binary operators involve two expressions, unary operators
involve one.
Operators | Binary | Unary |
---|---|---|
Arithmetic | +,-,*,/,% | +,- |
Boolean | ||,&&,== | ! |
Logical | |,& | ~ |
Other | ->,.,= | * |
Expressions have values. The value of a single variable is its contents. So the value of the variable a is its contents. The value of an expression like a+b is the sum of the values of the two variables. The usual rules about precedence that you learned in algebra class apply. You can use parentheses to group parts of an expression.
The assignment operator is used to copy values from one variable to another. Assignment does not alter the source. The contents of one variable is copied to the contents of the other variable. So, the expression a = 2, sets the contents of the variable a to hold the value 2. If we set a couple of variables like this,
Like other expressions, assignment in C++ has a value. The value of an assignment expression is the value of the right hand side. So the value of the expression a = b is the value of b. This allows us to create expressions like a = b = 2; This is interpreted to mean that b is set to 2 and a is set to the value of the expression b=2. The value of this expression is 2, so a is set to 2. The fact that assignment has a value is important when we look at conditional statements.
There are two major I/O methods in C++, standard I/O and streams. We will talk about streams first. The basic model of the iostreams library is a pipe. Imagine that you put things into a pipe and then flow out the end. There are sources and sinks to these streams. For now, we will look at one source and one sink. The sink is called cout and the source is cin.
Some examples;