* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx4b.html
blobd8d14341415a85b8c7a182bf6e25651cd43de376
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>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>
13 </head>
14 <body>
15 <H2>section 1.2: Variables and Arithmetic Expressions</H2>
17 <p>page 10
18 </p><p>Deep sentence:
19 <blockquote>Although
20 C compilers do not care about how a program looks,
21 proper indentation and spacing are critical
22 in making programs easy
23 for people to read.
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.
32 One is that
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.
36 The fragments
37 <pre>while(i &lt; j)
38 i = 2 * i;
39 </pre>and
40 <pre>while(i &lt; j) i = 2 * i;
41 </pre>and
42 <pre>while(i&lt;j)i=2*i;
43 </pre>and
44 <pre> while(i &lt; j)
45 i = 2 * i;
46 </pre>and
47 <pre>while (
48 i &lt;
49 j )
50 i =
51 2 *
52 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,
58 and there are
59 also
60 other, deeper style issues besides mere layout
61 and typography.
62 </p><p>There is some value in having a reasonably standard style
63 (or a few standard styles)
64 for code layout.
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.)
80 </p><p>Deep sentence:
81 <blockquote>...in C,
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,
86 but remember it:
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.
91 </p><p>page 12
92 </p><p>Here is more discussion on the difference between integer and
93 floating-point division.
94 Nothing deep; just something to remember.
95 </p><p>page 13
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
103 <TT>%</TT>.
104 The answer is to double it: <TT>%%</TT>.
105 </p><p>Also, note
106 (as was mentioned on page 11)
107 that <em>you</em> must match up the arguments to <TT>printf</TT>
108 with
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,
113 the code
114 <pre> printf("%d\n", fahr);
115 </pre>will <em>not</em> work.
116 You might ask,
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
119 automatically,
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
124 to <TT>printf</TT>;
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,
128 stated earlier,
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.)
134 </p><hr>
136 Read sequentially:
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>
141 </p>
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>
146 </p>
147 </body>
148 </html>