1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
7 <link rev=
"owner" href=
"mailto:scs@eskimo.com">
8 <link rev=
"made" href=
"mailto:scs@eskimo.com">
9 <title>3.1 Expression Statements
</title>
10 <link href=
"sx3.html" rev=precedes
>
11 <link href=
"sx3b.html" rel=precedes
>
12 <link href=
"sx3.html" rev=subdocument
>
15 <H2>3.1 Expression Statements
</H2>
17 <p>[This section corresponds to K
&R Sec.
3.1]
18 </p><p>Most of the statements in a C program are
<dfn>expression statements
</dfn>.
19 An expression statement is simply an expression followed by a semicolon.
25 printf(
"Hello, world!\n");
27 are all expression statements.
28 (In some languages, such as Pascal,
29 the semicolon separates statements,
30 such that the last statement is not followed by a semicolon.
31 In C, however, the semicolon is a statement terminator;
32 all simple statements are followed by semicolons.
33 The semicolon is also used for a few other things in C;
34 we've already seen that it terminates declarations, too.)
35 </p><p>Expression statements do all of the real work in a C program.
36 Whenever you need to compute new values for variables,
37 you'll typically use expression statements
38 (and they'll typically contain assignment operators).
39 Whenever you want your program to do something visible,
41 you'll typically call a function
42 (as part of an expression statement).
43 We've already seen the most basic example:
44 calling the function
<TT>printf
</TT> to print text to the screen.
45 But anything else you might do--read or write a disk file,
46 talk to a modem or printer,
47 draw pictures on the screen--will
48 also involve function calls.
49 (Furthermore, the functions you call to do these things
50 are usually different depending on which operating system you're using.
51 The C language does not define them,
52 so we won't be talking about or using them much.)
53 </p><p>Expressions and expression statements can be arbitrarily complicated.
54 They don't have to consist of exactly one simple function call,
55 or of one simple assignment to a variable.
57 many functions return values,
58 and the values they return can then be used
59 by other parts of the expression.
60 For example, C provides a
<TT>sqrt
</TT> (square root) function,
61 which we might use to compute the hypotenuse of a right
66 </p><p>To be useful, an expression statement must
68 it must have some lasting effect on the state of the program.
69 (Formally, a useful statement must have at least one
70 <dfn>side effect
</dfn>.)
77 assign new values to the
79 and the third one calls
<TT>printf
</TT> to print something out,
80 and these are good examples of statements that do something useful.
81 </p><p>(To make the distinction clear, we may note that degenerate
89 are syntactically valid statements--they
90 consist of an expression followed by a semicolon--but
92 they compute a value without doing anything with it,
93 so the computed value is discarded,
94 and the statement is useless.
96 the ``degenerate'' statements in this paragraph
97 don't make much sense to you, don't worry;
98 it's because they, frankly, don't make much sense.)
99 </p><p>It's also possible for a single expression to have multiple
100 side effects, but it's easy for such an expression to be
101 (a) confusing or (b) undefined.
102 For now, we'll only be looking at expressions
103 (and, therefore, statements)
104 which do one well-defined thing at a time.
108 <a href=
"sx3.html" rev=precedes
>prev
</a>
109 <a href=
"sx3b.html" rel=precedes
>next
</a>
110 <a href=
"sx3.html" rev=subdocument
>up
</a>
111 <a href=
"top.html">top
</a>
114 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
115 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
116 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>