[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 361
  • Last Modified:

Postfix and Prefix confusion

Hi,

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.
0
chandru_in
Asked:
chandru_in
  • 5
  • 3
1 Solution
 
WelkinMazeCommented:
Hi,

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!
0
 
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??
0
 
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
WelkinMazeCommented:
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.
0
 
WelkinMazeCommented:
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)
0
 
WelkinMazeCommented:
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.
0
 
chandru_inAuthor Commented:
Thank you WelkinMaze.  Could u please explain what C standard says about it??
0
 
WelkinMazeCommented:
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
       fragment

               #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
0
 
ozoCommented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now