* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx3a.html
blob7d15a5188ca4433daeb68fbc6caacfa43e201255
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
15 <H2>3.1 Expression Statements</H2>
17 <p>[This section corresponds to K&amp;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.
20 The lines
21 <pre>
22 i = 0;
23 i = i + 1;
24 and
25 printf("Hello, world!\n");
26 </pre>
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,
40 in the real world,
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.
56 For one thing,
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
62 triangle like this:
63 <pre>
64 c = sqrt(a*a + b*b);
65 </pre>
66 </p><p>To be useful, an expression statement must
67 do something;
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>.)
71 The first two
72 sample
73 expression
74 statements
75 in this section
76 (above)
77 assign new values to the
78 variable <TT>i</TT>,
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
82 constructions such as
83 <pre>
87 i + 1;
88 </pre>
89 are syntactically valid statements--they
90 consist of an expression followed by a semicolon--but
91 in each case,
92 they compute a value without doing anything with it,
93 so the computed value is discarded,
94 and the statement is useless.
95 But if
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.
105 </p><hr>
107 Read sequentially:
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>
112 </p>
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>
117 </p>
118 </body>
119 </html>