[Courses] [C] Chapter 4d

KWMelvin kwmelvin at intrex.net
Fri Oct 11 18:04:00 EST 2002


Greetings,

Thank you for trying that and sharing it with us.

From: King's _C Programming a modern approach_ W.W.Norton, 1996.

At first glance, the increment and decrement operators are
simplicity itself: ++ adds 1 to its operand, while  -- subtracts 1.
Unfortunately, this simplicity is misleading--the increment and
decrement operators can be tricky to use.  One complication
is that ++ and -- can be used as PREFIX operators (++i and --i,
for example) or POSTFIX operators (i++ and i--).  The correctness
of a program may hinge on picking the proper version.

Another complication is that, like the assignment operators, ++ and
-- have  side effects: they modify the values of their operands.
Evaluating the expression ++i (a "pre-increment") yields i + 1
and--as a side effect--increments i:

i = 1;
printf("i is %d\n", ++i);   /* prints "i is 2" */
printf("i is %d\n", i);     /* prints "i is 2" */

Evaluating the expression i++ (a "post-increment") produces the
result i, but causes i to be incremented afterwards:

i = 1;
printf("i is %d\n", i++);   /* prints "i is 1" */
printf("i is %d\n", i);     /* prints "i is 2" */

The first printf() shows the original value of i, before it is
incremented. The second printf() shows the new value. As these
examples illustrate, ++i means "increment i immediately," while
i++ means "use the old value of i for now, but increment i later."
How much later?  The C standard doesn't specify a precise time,
but it's safe to assume that i will be incremented before the
next statement is executed.

The -- operator has similar properties:

i = 1;
printf("i is %d\n", --i);   /* prints "i is 0" */
printf("i is %d\n", i);     /* prints "i is 0" */

i = 1;
printf("i is %d\n", i--);   /* prints "i is 1" */
printf("i is %d\n", i);     /* prints "i is 0" */

When ++ or -- is used more than once in the same expression, the
result can often be hard to understand. Consider the following
statements:

i = 1;
j = 2;
k = ++i + j++;

What are the values of i, j, and k after these statements are
executed?  Since i is incremented BEFORE its value is used, but
j is incremented AFTER it is used, the last statement is equivalent
to

i = i + 1;
k = i + j;
j = j + 1;

so the final values of i, j, and k are 2, 3, and 4 respectively.
In contrast, executing the statements

i = 1;
j = 2;
k = i++ + j++;

will give i, j, and k the values 2, 3, and 3, respectively.

For the record, the postfix versions of ++ and -- have higher
precedence than unary plus and minus and are left associative.
The prefix versions have the same precedence as unary plus and
minus and are right associative.

From: King's _C Programming a modern approach_ W.W.Norton, 1996.

On Fri, Oct 11, 2002 at 05:09:57PM -0400, Morgon Kanter wrote:
> 
> >Can anyone write an easy-to-read program that would show the difference?
> >(I'm trying to get YOU actively involved in this "course"... here's
> >a hint: declare your variable and initialize it, then printf() the
> >value. increment the variable using prefixed `++', then printf()
> >the variable again... do the same for postfix and for decrement).
> >Show us your source! (Use the source Luke, use the source!)
> >May the source be with you!
>  
> Here goes.
> ------------------Start program--------------------
> #include <stdio.h>
> 
> int main(void) {
>         int x = 1;
> 
>         printf("%d\n", x);
> 
>         x++;
>         printf(%d\n", x);
> 
>         x = 1;
>         ++x;
>         printf("%d\n", x);
> }
> -----------End Program-------------
> 
> The results:
> 1
> 2
> 2
> 
> So, x++ and ++x are the same.
> 
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
> 



More information about the Courses mailing list