12
Chapter 1
'V
An
Overview
of
C
II
miles
26;
yards
385;
These are assignment statements. The equal sign is
an
assignment operator. The two
numbers
26
and
385 are integer
constants.
The value 26 is assigned to
the
variable
mi
1
es.
The value 385 is assigned to the variable
yards.
II
kilometers
1.609
*
(miles
+
yards
/
1760.0);
This is
an
assignment statement. The value
of
the
expression
on
the right side
of
the
equal sign is assigned to the variable ki 1
ometers.
The operators
''<,
+,
and
/
stand
for
multiplication, addition,
and
division, respectively. Operations inside
parentheses
are
performed first. Because division
has
higher precedence
than
addition, the value of the
subexpression
yards
/
1760.0
is
calculated
first. (See Appendix
E,
"Operator
Precedence
and
Associativity.")
That
value is added to the value of the variable
mi
1
es
to produce a value
that
is
then
multi-
plied
by
1.
609. This final value is
then
assigned
to
the variable ki
lometers.
II
printf("\nA
marathon
is
%f
kilometers.\n\n",
kilometers);
This
is
a
statement
that
invokes,
or
calls,
the
pri
ntfO
function.
The
function
pri
ntfO
can
have a variable
number
of
arguments.
The first
argument
is always a
string, called the
control string. The control string
in
this example is
"\nA
marathon
is
%f
kilometers.\n\n"
It
is
the
first
argument to
the
function
pri
ntfO.
Inside this string is
the
conversion
specification, or format,
%f.
The formats
in
a control string, if any, are
matched
with
the remaining arguments
in
the
pri
ntfO
function.
In
this case,
%f
is
matched
'\vith the
argument
kilometers.
Its effect is to
print
the
value of the variable
kilometers
as a
floating-point
number
and insert it into the
print
stream
where the format
%f
occurs .
•
Certain words, called keywords are reserved
and
CalIDOt
be
used
by the programmer
as
names
of variables. For example, i
nt,
float,
and
double
are keywords. A table of
keywords
appears
in
Section 2.4, "Keywords,"
on
page 77. Other
names
are knowll to
the
C
system
and
normally
would
not
be
redefined
by
the
programmer.
The
name
pri
ntf
is
an
example. Because
pri
ntf
is
the
name
of a
function
in
the
standard
library,
it
usually is not
used
as the name of a variable.
1.4
'V
The
Use of
#defi
ne
and
#i
ncl
ude
13
~
decimal point
in
a number indicates
that
it
is a floating-point constant
rather
than
an mteger constant. Thus, the numbers 37
and
37.0
would be treated differently in a
progran:. Although there are three floating
types-float,
double,
and
long
doub 1
e-
and varIables can
be
declared to be
of
any of these types, floating constants are auto-
matically of type dou b 1 e.
Expressions
typi~ally
are
f~und
on
the
right
side
of
assignment
operators
and
as
arguments to functlOns. The
SImplest expressions are
just
constants
such
as 385 d
1760.0,
which were
used
in the previous program. The name of a variable itself an
b
'd
d . d can e
conSI ere
an
expreSSlOn, an meaningful combinations of
operators
with variables
and constants are also expressions.
!he
evalu.a:i~n
of
expr~ssions
can
involve
conversion
rules. This
is
an
important
pomt. The
dlVIslOn
of
two mtegers results
in
an
integer value, and any remainder is dis-
carded.
T~us,
for
exam~l~,
the
expre~sion
7/2
has
i
nt
value
3.
The expression
7.0/2,
~owever,
IS
a doubl.e
dlv~ded
by
an
lnt.
When the expression
7.0/2
is evaluated, the
value of the expresslOn
2.1S automatically converted to a doubl e, causing
7.0/2
to have
the value 3.5. In the prevlOus program, suppose
that
the
statement
kilometers
1.609
*
(miles
+
yards
/
1760.0);
changed to
=
1.609
*
(miles
+
yards
/
1760);
lea~s
to a
progra~
bug. Because
the
variable
yards
is of type i
nt
and
has
value
the mteger expresslOn
s integer division, and the result is the i
nt
value
O.
This is
not
what is wanted. Use
"0
constant
1760.0,
which is of type
double,
corrects the bug.
The Use
of
#defi
ne and
#i
ncl ude
C
compi~er
~as
a preprocessor built into it. Lines
that
begin '\vith a # are called pre-
~eSSIYla
dIrectIVes.
If
the lines
LIIVJIT
100
PI
3.14159