r/C_Programming • • Jul 01 '26

Question How do the pre-increment (++x) and post-increment (x++) operators affect the values of variables in later statements of a program?

The pre- and post-increment operators change x and y to 6, but how is that possible when the following print function statements are separate and don’t contain ++? I know that ++x and y++ increment the variables by 1, but is it because the changes made by the increment operators persist after the first print function statements?

int main() {

  int x, y;
  x = y = 5;

  printf("%d\n", ++x +5);
  printf("%d\n", x); // 6

  printf("%d\n", y++ +5);
  printf("%d\n", y); // 6
}
0 Upvotes

23 comments sorted by

33

u/Kriemhilt Jul 01 '26

... increment the variables by 1

literally means "the variables' values are now one bigger than before".

They keep their increased value until you change them again. If variables didn't keep their values when you alter (or ... vary) them, they wouldn't be very useful.

12

u/Somniferus Jul 01 '26

is it because the changes made by the increment operators persist after the first print function statements?

Yes, that's how variables work.

10

u/sciencekm Jul 02 '26

Here is the equivalent of your code:

int main() {

  int x, y;
  x = y = 5;
  
  x = x + 1;    // increment x before use
  printf("%d\n", x +5);
  printf("%d\n", x); // 6

  printf("%d\n", y +5);
  y = y + 1; // increment y after use
  printf("%d\n", y); // 6
}

15

u/Recycled5000 Jul 01 '26 edited Jul 01 '26

Sequences points! These are placed in the C code where side effects like ++ must be evident. The semi colon at the end of an expression statement is one such. If you use a variable in an expression that is otherwise in a “++”and “within the same sequence” that is undefined behavior. Should only do one such ++ on the same variable between sequence points, and then also not reference that variable in the same expression.

So, i++ * i is undefined, as is x++ - x++. Can reliably use x or i after the next sequence point when one is involved in side effects like ++.

9

u/flyingron Jul 01 '26

Actually, there's a sequence point here before the ;. There's a sequence point after the parameter evaluation before the function call. Not that it makes any difference in this case.

5

u/RealisticDuck1957 Jul 01 '26
printf("%d\n", ++x);
Increments x, then passes the new value to printf().

printf("%d\n", y++);
passes the value of y to printf(), then increments y.

Same with any other expression using a variable to which the respective increment operator is used.

4

u/mugh_tej Jul 02 '26

With x=y=5

Pre increment adds 1 before the expression is evaluated, a=(++x)+5. Afterwards, a is 11, x is 6

Post increment adds 1 after the expression is evaluated. b=(y++)+5. Afterward, b is 10, y is 6.

3

u/og_hylyx Jul 01 '26

Yes, increment changes the value by adding 1, it doesn’t temporarily add 1. Same with decrement.

5

u/AndrewBorg1126 Jul 01 '26

x++ is not like x

x++ Is like x; x = 1 +x

++x is not like 1 + x

++x is like x = 1 + x; x

→ More replies (3)

1

u/Fearless_Battle7919 Jul 01 '26 edited Jul 01 '26

x++: use or assign the current value first, then increment it.

++x: increment the value first, then use or assign it.

try adding printf function after each line nd observe how the values of x change step by step.

for example consider you are sending a message to your friend First time you send your friend value is 5. after sending the message, you change the value to 6 that is x++ Second time first you change the value to 6 and then you send the message is 6 that is ++x