Chapter 2 (B) - Data Type and Operators
Chapter 2 (B) - Data Type and Operators
Integer Types
and Operators
Fundamental type
Integral Types
Integer Types
short
int
long
unsigned short
unsigned int
unsigned long
1 2
Arithmetic Operators
Division Operator
The division operator returns the quotient.
However, the value of the quotient depends
on whether at least one of the operands is a
floating point data type.
For example, the value of 10 / 4 is 2.5.
However, in C++, the value is 2
When both operands are an integer or other
whole number data type, then the result is
an integer as well, and the remainder is not
part of the quotient
5 6
This is true even if the result is assigned to a floating However, the value of 10.0 / 4 is 2.5 in C++
point variable.
The output of the following program is 10 / 4 = 2 When at least one of the operands is a
floating point data type, and 10.0 would be
#include <iostream>
using namespace std; interpreted as floating point, then the result
int main(void) is a floating point as well.
{
int firstOp = 10, secondOp = 4;
The output of the following program is
float result = firstOp / secondOp; 10 / 4 = 2.5 because we changed the data
cout << firstOp << " / " << secondOp << " = " << type of firstOp from int to float:
result;
return 0;
} 7 8
The Modulus Operator
#include <iostream> The modulus operator also involves division,
using namespace std; but returns only the remainder.
int main(void) For example, the result of 7 % 2 is not the
{ quotient, 3, but the remainder, 1
float firstOp = 10, result; The modulus operator works only with
int secondOp = 4; whole number operands.
result = firstOp / secondOp; The result of an attempt to use it with a
cout << firstOp << " / " << secondOp << " = " <<
floating point operand is undefined.
result; The result often is a compiler error, but this
return 0; is compiler dependent.
}
9 10
m + n= 54 + 20 = 74
m - n= 54 - 20 = 34
m * n= 54 X 20 = 1080
2
20 54
m % n= 54 % 20 = 14 40
14
11 12
Composite Assignment
Operators
The standard assignment operator in New programmers sometimes are
C++ is the equal sign = confused by statements such as total
= total + added
C++ also include the following
composite assignment In mathematics, a variable cannot
equal itself plus another number.
+= -= *= /= %= However, in C++ programming, in
which the = operator is not used for
equality, but instead for assignment.
15 16
Nevertheless, there also is another way to
express
total = total + added
total += added;
To the compiler, it makes no difference
However, many programmers prefer total
+= added, some because it looks more
elegant, others because it seems more
readable, and still others for the practical
reason that it requires less typing.
17 18
Examples:
int m = 6 ; m-=2
m=m-2
m+=4 m= 6-2
m=m+4 m=4
m= 6+4
m = 10
19 20
n=22
After n+=9 (n=22+9=31), n=31
After n -=5 (n=31-5=26), n=26
After n*=2 (n=26*2=52), n=52
21 22
Floating-Point Types
23 24
Type Conversion
25 26
Type Casting
27 28
Numeric Overflow
29 30
1/3 is not exactly 0.333333
This is the round off error − b ± b 2 − 4ac
x=
In some cases, these errors can cause 2a
serious problems
31 32
float d = b*b -4*a*c; d = b 2 − 4ac
− b ± b 2 − 4ac − b + b 2 − 4ac
x= float x1 = (-b + sqrtd) / (2*a); x1 =
2a 2a
35 36
37