r/C_Programming • u/deadinstatic • 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
33
u/Kriemhilt Jul 01 '26
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
Yes, that's how variables work.
10
u/sciencekm Jul 02 '26
Here is the equivalent of your code:
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 ++.
5
u/RealisticDuck1957 Jul 01 '26
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
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
1
u/CarlRJ Jul 02 '26
++xcan be read as "increment x and use x's new value (in whatever expression)" - essentially "increment and read".x++can be read as "use x's current value (in whatever expression) and increment x afterwards" - essentially "read and increment".1
u/gm310509 Jul 02 '26
Both increment the target variable.
The difference is whether the value used in the bigger expression (I.e. the + 5 part) is taken before the increment or after.
So, if x starts out as 5, x++ +5 will take the value of x for use in the rest of the expression which is to add 5 to it resulting in 10. It will separately increment x to make it 6.
In the case of ++x + 5, x will be incremented to make it 6, then this post increment value will be passes to the rest of the expression I.e. the +5 bit resulting in 6 + 5 = 11. Upon completion x will be 6 as a result of the increment.
1
u/burlingk Jul 02 '26
So, the 'effect' of both of the same. They increment the variable.
x++ and ++x both mean the same as x = x + 1.
The difference is the 'side effect.'
That is, if you use them directly in another statement, ++x will 'return' the new value, while x++ will 'return' the old value.
1
u/lmarcantonio Jul 02 '26
Simply because the increment/decrement are assignment and change the variable.
The first statement increment x (setting x to 6), computes x+5 (which is 11)
The second statement prints 6 because x was changed by the statement.
If you don't want it to persist just say... (x+1)
By the way the usage of them in expression is somewhat deprecated because they are not clear. Also never, ever use more of one of these in one expression because there are unspecified behaviours (unless you know about sequence points which are a topic waaaay more advanced)
1
u/Classic_Department42 Jul 02 '26
If you replace ++x by x+1 you have the behaviour younwere thinking about. If you replace it by x=x+1 you have the current behavious
1
u/SmokeMuch7356 Jul 02 '26
The result of
++xis the current value ofxplus 1, which is what gets added to5and passed toprintf. As a side effect,xis incremented. There is a sequence point after all the function arguments are evaluated, but before control transfers to the function;xwill be updated somewhere between its evaluation and this sequence point. It's very roughly equivalent to writingexcept the update to
xhappens beforeprintfis actually called.works the same way;
y++evaluates to the current value ofy, which is what gets added to5and passed toprintf; as a side effect,yis incremented, and again this increment will happen sometime before the function executes; again very roughly equivalent towith the same caveat as above as to when
yis updated.1
u/theNbomr Jul 03 '26
In the expressions x++ and ++x, there are two things going on. The two cases have those two things happening in opposite order. The two things are :
In the pre-increment case, the evaluation of the expression returns the incremented value. In the post-increment case, the evaluation of the expression occurs before the variable is incremented.
This effect can be exploited to optimize the relationship between loop counting variables and their use as index variables for things like string and other array manipulations.
1
u/spellstrike Jul 01 '26
moreso, you cannot apply the increment operator (
++) directly to a constant. Doing so will result in a compilation error because a constant value is read-only and cannot be modified after its initialization.an increment, increments.
-3
u/mlt- Jul 01 '26
The funny part begins when you put either or both in the same expression
if (x++==5) print(x++==++x);