• 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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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??
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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??
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.