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>section
1.2: Variables and Arithmetic Expressions
</title>
10 <link href=
"sx4a.html" rev=precedes
>
11 <link href=
"sx4c.html" rel=precedes
>
12 <link href=
"sx4.html" rev=subdocument
>
15 <H2>section
1.2: Variables and Arithmetic Expressions
</H2>
20 C compilers do not care about how a program looks,
21 proper indentation and spacing are critical
22 in making programs easy
24 We recommend writing only one statement per line,
25 and using blanks around operators to clarify grouping.
26 The position of braces is less important,
27 although people hold passionate beliefs.
28 We have chosen one of several popular styles.
29 Pick a style that suits you,
30 then use it consistently.
31 </blockquote>There are two things to note here.
33 (with one or two exceptions)
34 the compiler really does not care how a program looks;
35 it doesn't matter how it's broken into lines.
40 <pre>while(i
< j) i =
2 * i;
42 <pre>while(i
<j)i=
2*i;
53 </pre>are all treated exactly the same way by the compiler.
54 </p><p>The second thing to note is that style issues
55 (such as how a program is laid out)
56 <em>are
</em> important,
57 but they're not something to be too dogmatic about,
60 other, deeper style issues besides mere layout
62 </p><p>There is some value in having a reasonably standard style
63 (or a few standard styles)
65 Please don't take the authors' advice to
66 ``pick a style that suits you''
67 as an invitation to invent your own brand-new style.
69 (perhaps after you've been programming in C for a while)
70 you have specific objections to specific facets of existing styles,
71 you're welcome to modify them,
72 but if you don't have any particular leanings,
73 you're probably best off copying an existing style at first.
74 (If you want to place your own stamp of originality
75 on the programs that you write,
76 there are better avenues for your creativity than inventing a bizarre layout;
77 you might instead try to make the logic easier to follow,
78 or the user interface easier to use,
79 or the code freer of bugs.)
82 as in many other languages,
83 integer division
<I>truncates
</I>:
84 any fractional part is discarded.
85 </blockquote>The authors say all there is to say here,
87 just when you've forgotten this sentence,
88 you'll wonder why something is coming out zero
89 when you thought it was supposed the be the quotient
90 of two nonzero numbers.
92 </p><p>Here is more discussion on the difference between integer and
93 floating-point division.
94 Nothing deep; just something to remember.
96 </p><p>Hidden here are discriptions of some more of
97 <TT>printf
</TT>'s ``conversion specifiers.''
98 <TT>%o
</TT> and
<TT>%x
</TT> print integers, in octal (base
8) and
99 hexadecimal (base
16), respecively.
100 Since a percent sign normally tells
<TT>printf
</TT> to expect
101 an additional argument and insert its value,
102 you might wonder how to get
<TT>printf
</TT> to just print a
104 The answer is to double it:
<TT>%%
</TT>.
106 (as was mentioned on page
11)
107 that
<em>you
</em> must match up the arguments to
<TT>printf
</TT>
109 the conversion specification;
110 the compiler can't (or won't) generally check them for you
111 or fix things up if you get them wrong.
112 If
<TT>fahr
</TT> is a float,
114 <pre> printf(
"%d\n", fahr);
115 </pre>will
<em>not
</em> work.
117 ``Can't the compiler see that
<TT>%d
</TT> needs an integer and
118 <TT>fahr
</TT> is floating-point and do the conversion
120 just like in the assignments and comparisons on page
12?''
121 And the answer is, no.
122 As far as the compiler knows,
123 you've just passed a character string and some other arguments
125 it doesn't know that there's a connection between the arguments
126 and some special characters inside the string.
127 This is one of the implications of the fact,
129 that functions like
<TT>printf
</TT> are not special.
130 (Actually, some compilers or other program checkers do know
131 that a function named
<TT>printf
</TT> is special,
132 and will do some extra checking for you,
133 but you can't count on it.)
137 <a href=
"sx4a.html" rev=precedes
>prev
</a>
138 <a href=
"sx4c.html" rel=precedes
>next
</a>
139 <a href=
"sx4.html" rev=subdocument
>up
</a>
140 <a href=
"top.html">top
</a>
143 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
144 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
145 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>