section 1.2: Variables and Arithmetic Expressions

page 10

Deep sentence:

Although C compilers do not care about how a program looks, proper indentation and spacing are critical in making programs easy for people to read. We recommend writing only one statement per line, and using blanks around operators to clarify grouping. The position of braces is less important, although people hold passionate beliefs. We have chosen one of several popular styles. Pick a style that suits you, then use it consistently.
There are two things to note here. One is that (with one or two exceptions) the compiler really does not care how a program looks; it doesn't matter how it's broken into lines. The fragments
while(i < j)
	i = 2 * i;
and
while(i < j) i = 2 * i;
and
while(i<j)i=2*i;
and
	while(i < j)
i = 2 * i;
and
while	(
i	<
j	)
i	=
2	*
i	;
are all treated exactly the same way by the compiler.

The second thing to note is that style issues (such as how a program is laid out) are important, but they're not something to be too dogmatic about, and there are also other, deeper style issues besides mere layout and typography.

There is some value in having a reasonably standard style (or a few standard styles) for code layout. Please don't take the authors' advice to ``pick a style that suits you'' as an invitation to invent your own brand-new style. If (perhaps after you've been programming in C for a while) you have specific objections to specific facets of existing styles, you're welcome to modify them, but if you don't have any particular leanings, you're probably best off copying an existing style at first. (If you want to place your own stamp of originality on the programs that you write, there are better avenues for your creativity than inventing a bizarre layout; you might instead try to make the logic easier to follow, or the user interface easier to use, or the code freer of bugs.)

Deep sentence:

...in C, as in many other languages, integer division truncates: any fractional part is discarded.
The authors say all there is to say here, but remember it: just when you've forgotten this sentence, you'll wonder why something is coming out zero when you thought it was supposed to be the quotient of two nonzero numbers.

page 12

Here is more discussion on the difference between integer and floating-point division. Nothing deep; just something to remember.

page 13

Hidden here are discriptions of some more of printf's ``conversion specifiers.'' %o and %x print integers, in octal (base 8) and hexadecimal (base 16), respectively. Since a percent sign normally tells printf to expect an additional argument and insert its value, you might wonder how to get printf to just print a %. The answer is to double it: %%.

Also, note (as was mentioned on page 11) that you must match up the arguments to printf with the conversion specification; the compiler can't (or won't) generally check them for you or fix things up if you get them wrong. If fahr is a float, the code

	printf("%d\n", fahr);
will not work. You might ask, ``Can't the compiler see that %d needs an integer and fahr is floating-point and do the conversion automatically, just like in the assignments and comparisons on page 12?'' And the answer is, no. As far as the compiler knows, you've just passed a character string and some other arguments to printf; it doesn't know that there's a connection between the arguments and some special characters inside the string. This is one of the implications of the fact, stated earlier, that functions like printf are not special. (Actually, some compilers or other program checkers do know that a function named printf is special, and will do some extra checking for you, but you can't count on it.)


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback