Special
Assignment Operators ++ and --
C has some special operators which
cut down on the amount of typing involved in a program. This is a subject in
which it becomes important to think in C and not in other languages. The simplest
of these perhaps are the increment and decrement operators:
++
increment: add one to
--
decrement: subtract one from
These attach to any variable of
integer or floating point type. (character types too, with care.) They are used
to simply add or subtract 1 from a variable. Normally, in other languages, this
is accomplished by writing:
variable
= variable + 1;
In C this would also be quite valid,
but there is a much better way of doing this:
variable++;
or
++variable;
would do the same thing more neatly.
Similarly:
variable
= variable - 1;
is equivalent to:
variable--;
or
--variable;
Notice particularly that these two
operators can be placed in front or after the name of the variable. In some
cases the two are identical, but in the more advanced uses of C operators,
which appear later in this book, there is a subtle difference between the two.
0 comments:
Post a Comment