Postfix increment x++
What you'll learn: How to increase a variable's value by one while using its original value in a current expression.
The "Use now, pay later" operator
In C, the postfix increment operator x++ is a shorthand way to add 1 to a variable. However, it has a unique behavior: it performs the addition after the variable has been evaluated in the current line of code.
Think of it like a "Buy Now, Pay Later" scheme. If you are at a store and use a coupon that says "Get this item, then I'll charge you $1 more next time," you walk out with the item at its current price, but your tab increases for the future. With x++, the program uses the current value of x for whatever math or printing is happening right now, and only then bumps the value up by one.
Seeing it in action
Because the increment happens "post" (after) evaluation, the timing is everything. Look at how the value changes in this sequence:
#include <stdio.h>
int main() {
int cookies = 5;
// We print the value while using the postfix operator
printf("I have %d cookies.\n", cookies++);
// Now we check the value again on the next line
printf("Wait, now I have %d cookies!\n", cookies);
return 0;
}
If you run this, the first line will output 5. Even though the ++ is right there, the computer "reads" the 5, hands it to the printf function, and only adds the 1 once that task is finished. By the time the code moves to the second printf, the increment has finally settled, and the value is 6.
Why use it?
Postfix incrementing is extremely common when working with loops or processing lists. It allows you to say, "Give me the item at index 0, and as soon as I'm done with it, move the pointer to index 1 so I'm ready for next time." It keeps your code concise by combining a "read" and a "write" operation into one tiny symbol.
Key takeaway
The postfix operator x++ uses the current value of x in the expression first, then increments it by 1.