The three data types we will use most often when we declare variables
Not only variables have types in C++. So do constants and other arithmetic expressions. Integer constants do not have decimal points. They also do not have commas. They look like this:
Real constants are considered to be of type double. They may have a decimal point or they may be written in C++'s version of scientific notation. Here are some double constants:
We won't use the last form very often. The fourth real number above is 18.2 times 10 to the -3 power, or .0182. Note that both integers and reals may be positive or negative.
Arithmetic expressions have types, too. If all the constants and variables in an expression are of type integer, then the expression is also of type integer. But if any constant or variable in an expression is of type float or double, then the expression is of type double. Here are two expressions of type integer and two of type double:
6 - 3 * 2 % 4
17 * (2485 - 301)
14 - 12 * 1.5
8.2 / (3 + 2 - 0.5)
We may safely assign an integer constant to a double variable because C++ will convert the constant to a double. Saying the following places 32.0 into the variable called freezing:
// temp at which water freezes
freezing = 32;
If we assign a constant of type double to a variable of type integer, the fractional part of the constant is lost. Saying the following will assign 0 to the variable called half:
half = 0.5;
Clearly, half should have been declared to be a double. What will be the value of answer after the following?
answer = 0.5 * 2.5; // In math, 0.5 * 2.5 = 1.25.
The expression 0.5 * 2.5 is of type double, but since answer is an integer its value will be 1 after the assignment statement above.
Do you think that 1/2 and 0.5 are interchangeable in a C++ program? They are not. Since 1 and 2 are both integers, the / will do integer division and thus 1/2 is equal to 0!
The arithmetic operators in C++ are shown in the following table. The operators in the top row have higher precedence than those in the bottom row. We'll say more about precedence shortly.
|Multiply, divide, modulo (also called remainder)||*, /, %|
|Add, subtract||+, -|
The modulo operator is defined for positive integers only. It produces
the remainder after division. All the other operators are defined for both
integers and reals. The division operator, however, behaves differently
on integers than it does on reals. If both of its operands are integers,
then / does integer division. If either operand is a float or double, then
/ does real division. Here are some examples of the use of modulo and division
|6 % 2||0||0 is the remainder after you divide 6 by 2|
|7 % 2||1||1 is the remainder after you divide 7 by 2|
|18 % 5||3||3 is the remainder after dividing 18 by 5|
|5 % 18||5||5 is the remainder after dividing 5 by 18|
|6 / 3||2||this is integer division|
|7 / 3||2||this is integer division so the .5 is lost|
|7 / 3.0||2.5||this is real division|
|7.0 / 3||2.5||this is real division|
We mentioned above that multiply, divide, and modulo have higher precedence than add and subtract. This means that in evaluating an expression that contains more than one operator, C++ makes one pass from left to right evaluating multiplies, divides, and mods. It then makes another pass from left to right evaluating adds and subtracts. We can change this default behavior by enclosing parts of expressions in parentheses. This forces C++ to evaluate the parenthesized portion first. Within the parentheses, C++ follows its rules of precedence.
Here are some examples of expressions and the value C++ would obtain
after evaluating them:
|3 + 2 * 5||13|
|18 / 4 * 2||8|
|25 % 6 - 2 * 3||-5|
|25 % (6 - 2) * 3||3|
|8 * 10 - 7||73|
|8 * (10 - 7)||24|
Integer division is a hard notion to get used to. For example, 5/3*3 is 3, not 5, since 5/3 is 1. Modulo arithmetic (remainders) also cause confusion. To help you understand the evaluation of C++ arithmetic expressions once and for all, here is a random problem generator which will patiently make up new problems for as long as you like. We recommend that you keep trying new problems until you get five in a row right. By the way, the evaluation of such expressions makes for good quiz/exam problems!