CIS 15AG
Answers Chapter 3:
Structure of a C Program: Expressions and Statements
1.
Evaluate the following arithmetic expressions. For each result circle the appropriate
type (int, float, double, char).
Answer:
Expression
Value
Type
a.
11 / 2
(int
divided by
int
is
int)
5
int
,
float,
double,
char
b.
5 + 2 * 2
= 5 + 4 = 9
9
int
,
float,
double,
char
c.
11 / 2 * 2
= 5 * 2 = 10
10
int
,
float,
double,
char
d.
15 / 5.0
Implicit type conversion: first
int
is promoted to
double: 15
is promoted to
15.0
, then
15.0 /
5.0
gives
3.0
, a
double
(See page 89).
3.0
int,
float,
double
,
char
e.
11 % 5
(remainder)
1
int
,
float,
double,
char
f.
5 % 11
(remainder)
5
int
,
float,
double,
char
g.
( 2 + 2 * 3 + 4 * ( 5 - 1 ) ) / 2.0
=
( 2 + 6 + 4 * 4 ) / 2.0 =
( 2 + 6 + 16 ) / 2.0 =
24 / 2.0 =
12.0
12.0
int,
float,
double
,
char
What is the type of a literal integer constant? In other words, in the following program
fragment, what is the type of
4
?
int a;
int b;
a = b +
4
;
The answer to this question is
int
. The type of a literal floating point constant is
double
, not
float
as we might be tempted to say.
Why is this important? It is important to understand the warnings given by the compiler;
for instance:
float num = 3.14;
1