c programming style - READ THIS

Guy Harris guy at sun.uucp
Tue Aug 6 15:50:31 AEST 1985


> Well, you should know that `++' and `--' *ONLY* works on variables of type
> scalar, where a there is a defined successor or predecessor

No, I didn't know that.  Then again, neither did Kernighan nor Ritchie.  See
the sixth paragraph under the heading "7.2 Unary operators" in the C
Reference Manual (p. 187 of *The C Programming Language* - I'm sick of
typing in quotes from the CRM for people who won't go read their own copy;
you've got the reference, go read it) wherein it states quite clearly that
"++" and "--" work on any type which can have 1 added to it (type "scalar"?
There's no "scalar" in the index to *The C Programming Language.*  Sure
you're not thinking of Pascal?).

> (You can compare these operators as the Pascal functions pred() and succ()),

No, you can't.  My J&W says that "succ" and "pred" apply to all scalar types
except "real".  My K&R says that "++" applies to any type which you can add
1 to, which *includes* "float" and "double" and does *not* include "enum".
(Besides, "++" has a side effect, while "succ" is a function, but we won't
discuss that.)  The fact that "a + 1" in C and "succ(a)" in Pascal happen
yield the same answer if "a" is an integral type in C and a "integer" or
"char" in Pascal shouldn't tempt you into stretching the similarity past its
breaking point.

	Guy Harris



More information about the Comp.lang.c mailing list