• C

Postfix and Prefix confusion


I tried the preceding program on Linux(gcc).

#include <stdio.h>

int main()
      int x = 10;
      int sum = x++ + ++x;
      printf("%d %d\n", sum, x);
      x = 10;
      sum = x++ + x++ + ++x;
      printf("%d %d\n", sum, x);
      x = 10;
      sum = x++ + ++x + x++ + ++x;
      printf("%d %d\n", sum, x);
      return 0;

The output I got was as below,

22 12
31 13
45 14

The output was same on VC++ 6 too.

Can someone please explain the output??

This is not a homework question.  It is just out of curiosity.
Who is Participating?
I cannot tell it with quotes but the idea is as in the following example (taken from here http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm):

EXAMPLE 7 The  grouping  of  an  expression  does  not
       completely  determine  its  evaluation.   In  the  following

               #include <stdio.h>
               int sum;
               char *p;
               /* ... */
               sum = sum * 10 - '0' + (*p++ = getchar());

       the expression statement is grouped as if it were written as

               sum = (((sum * 10) - '0') + ((*(p++)) = (getchar())));

       but  the actual increment of p can occur at any time between
       the previous sequence point and the next sequence point

The first one:
you have one ++x so x becomes 11 (preceeding increment is done before the variable is taken in the other operation)
after that is executed the summing x + x, so the sum becomes 22
and finally is executed x++ so x becomes 12.

The second one:
the first two x are summed up (they are with postfix inc so they are still 10)
at this point the sum is 20
after that this sum has to be summed up with the thirs x, but there we have prefix inc so x becomes 11, and the sum becomes 20+11=31
after that the postfix increment of the first two x is done so x becomes 13.

The third one:
The first two x are summed up, the one of them is wirh prefix inc, so x becomes 11 and the sum at this point is 22. After that the thirs x comes, it is with postfix inc so the x is still 11 and the sum becomes 33. After that the 4th x comes, it is with prefix inc so x becomes 12 and the sum becomes 45. after that the two postfix increments are done and the x becomes 14.

Hopes that helps!
chandru_inAuthor Commented:
In C the increment operator has a higher precedence and has a right to left associativity.

So shouldn't the prefix increments be done before any addition??
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

chandru_inAuthor Commented:
In fact java has same precedence and associativity as C.  I get a different result under java.  I also, feel the the resut in Java adheres to the precedence.
In java this does not work. I've tried it years ago.
For example
i = 1;
i = i++ + ++i;
will make i equal to 5 in C but equal to 4 in Java.
Yes, the increment operator has higher precedence but the associativity of + is left to right.
Yo can look at sum = x++ + ++x + x++ + ++x as sum = (((x++ + ++x) + x++) + ++x)
In fact these things are not defined well in any C standard, so although probably the most compilers will produce similar code, it is not good to rely on them in practice. They have to be used in such way just for some puzzling.
chandru_inAuthor Commented:
Thank you WelkinMaze.  Could u please explain what C standard says about it??
int sum = x++ + ++x;
violates the requirement that
  Between the previous and next sequence point an object shal have its stored value modified
at most once by the evaluation of an expression.
which means the behaviour is undefined.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.