-5

I'm writing a C program and want to increment an integer and print it. I tried to use a less simplistic syntax and integrate the printing and incrementing steps.

printf("%i\n", age++);

I ran the code and the original value of age was printed, not the incremented form! I debugged and saw that age actually is incremented after the print line is run. If the incrementation operation is executing, why is the old value printed?

kims
  • 85
  • 5
Logan Walker
  • 9
  • 1
  • 1
  • 1
    Because that is how postfix increment is defined to work. Why did you expect a different result? If you want to get the value *after* incrementing you need to use prefix increment – UnholySheep Nov 26 '21 at 15:21
  • look at the topic postfix and prefix operator – Abhilekh Gautam Nov 26 '21 at 15:30
  • https://stackoverflow.com/questions/24853/what-is-the-difference-between-i-and-i/24858#24858 – Nit_esh Nov 26 '21 at 15:45
  • Does this answer your question? [Why are these constructs using pre and post-increment undefined behavior?](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior) – Mocanu Gabriel Nov 26 '21 at 15:49
  • @MocanuGabriel Thats the answer to a different question. If Logan Walker had asked about `printf("%d %d\n", age++, ++age);`, that would have been the answer. But Logan's question does not involve undefined behavior: `printf("%i\n", age++)` is perfectly well-defined. – Steve Summit Nov 26 '21 at 15:53
  • ' I tried to use a less simplistic syntax' well, that's where you stsrted to go wrong:( Such 'optimizations' are the enemy of testing/debugging. – Martin James Nov 27 '21 at 03:37

3 Answers3

3

The postfix ++ operator evaluates to the current value of its operand and increments that operand as a side effect. That's why you're seeing the original value.

What you want is the prefix ++ operator, which evaluates to the incremented value of its operand.

printf("%i\n", ++age); 
dbush
  • 205,898
  • 23
  • 218
  • 273
1

The incrementation operation does get executed, but it's the return value from that operation that is printed. ++ is what is called a unary operator, meaning it takes only one operand: age in this case. The operator does two things: update the value of age, and return a value. The new value of age and the return value are not necessarily the same: such as in this instance. When the ++ operator follows the operand (as opposed to leading it), the return value becomes the operand's old value. If you want the return value to be the new value of age, you need to use the operator before the operand, like so: prinf("%d", ++age);

Amir Kooshky
  • 49
  • 1
  • 6
1

Pre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression. for example

a = ++x;

if the value of ‘x’ is 5 then value of ‘a’ will be 6 because the value of ‘x’ gets modified before using it in the expression.

Post-increment operator: A post-increment operator is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.for example

a = x++;

suppose the value of ‘x’ is 8 then value of variable ‘a’`` will be 8 because old value of ‘x’ is used.

In your case printf("%i\n", age++); you are using post increment operater.so, the value will be same.

vegan_meat
  • 878
  • 4
  • 10