3
#include <stdio.h>

int main(void){
  int sum , x;
  x = 1;
  sum = 0;

  while(x <= 10){
    sum = sum + x;
    ++x;
   }

  printf("The sum is: %d\n", sum);
  return 0;
}

I am learning C and putting together some examples using post increment and pre increment statements. In this example, the output is 55. When I use post increment x++; I also get 55. I was actually expecting to get a different answer. Can someone explain why I am getting the same answer. and how is this code any different from putting the post/pre increment above the sum = sum +x; expression

muhe
  • 109
  • 7
  • 2
    There's no difference if you don't assign the result anywhere. – Barmar Dec 21 '18 at 01:00
  • 1
    Usually, it takes less instructions to perform a ++x as compared to x++. However, the circumstances of where/how it is used can also have an affect on the resulting efficiency – user3629249 Dec 21 '18 at 01:13
  • 1
    x++; and ++x have the same side effect. The only difference is what they evaluate to (i.e., x before the increment or x after the increment). If you throw that value away, there's no difference. The standard says that expressions statements (such as `++x;`) and the last expression in `for` are evaluated as `void` expression. That means the value is discardes, and then there's no difference. – Petr Skocik Dec 21 '18 at 01:24
  • Possible duplicate of [What is the difference between ++i and i++?](https://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i) – roschach Dec 21 '18 at 08:56

5 Answers5

3

x++ and ++x both add 1 to x. The only difference between them is the value of the expression itself, e.g. if you do:

y1 = x++;

or

y2 = ++x;

y1 will get the old value of x, while y2 will get the new value of x. See What is the difference between i++ and ++i?.

Since you don't assign the result of the expression to anything, the difference is irrelevant in your program. If you had written:

sum = sum + ++x;

you would get a different result than

sum = sum + x++;

since now you're using the value of the expression, and the result matters.

Regarding your second question, statements are executed in order. So if you put ++x; before the assignment, then you'll be adding the incremented values of x to sum instead of the original values. Instead of adding 1, 2, 3, ... 10, you'll add 2, 3, 4, ..., 11. You can see this difference if you put:

printf("Adding %d + %d\n", sum, x);

before the assignment.

Putting the increment statement before or after the assignment is similar to using the increment expression in the assignment itself, and choosing between pre-increment and post-increment. I.e.

++x; // or x++;
sum = sum + x;

is the same as

sum = sum + ++x;

Conversely,

sum = sum + x;
++x; // or x++;

is the same as

sum = sum + x++;
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

In C, not unless you write something complex, such as for ( int i = 1; sum < bound; sum += i++ ).

In C++ with overloaded operators, there can be. ++instance is generally more efficient than instance++, which needs to make a temporary copy.

Davislor
  • 14,674
  • 2
  • 34
  • 49
0
#include<stdio.h>

int main() {
    int x = 5;
    int y = x++;
    int z = ++x;
    printf("%d, %d, %d\n", x, y, z);
}

returns

7, 5, 7

They all increment x. The difference is the value that's returned.

++x returns x before the increment

x++ returns x after the increment

Donovan Jenkins
  • 133
  • 1
  • 13
0

A pre-increment operator ++x increases the value of a variable before using it in a expression.In Post-Increment operator x++, value is first used in a expression and then incremented. if you want to see the difference you can try using printf() inside while loop for ++x and x++

while(x <= 10){
    sum = sum + x;
    printf("The sum is: %d\n", ++x);
}

This will print from number 2 to 11

while(x <= 10){
    sum = sum + x;
    printf("The sum is: %d\n", x++);
}

This wil print from 1 to 10

Another differences are in precedence and associativity. Precedence of postfix ++ is more than prefix ++. Associativity of postfix ++ is left to right and associativity of prefix ++ is right to left.

Elish
  • 486
  • 3
  • 12
-6
sum=sum + ++x; 
sum=sum + x++;

This is what you need

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    Why does he need this? He asked a question about how it works, not how to get a specific result. – Barmar Dec 21 '18 at 01:10
  • Only in this case,the sum’s value are difference, There is not difference in sum = sum + x; and then call x++or++x – ASHION.SHEN Dec 28 '18 at 22:06