-5

As far as I understand, a++ is postfix incrementation, it adds 1 to a and returns the original value. ++a is prefix incrementation, it adds 1 to a ad returns the new value.

I wanted to try this out, but in both cases, it returns the new value. What am I misunderstanding?

#include <stdio.h>
int main() {
  int a = 0;
  int b = 0;

  printf("%d\n", a); // prints 0
  printf("%d\n", b); // prints 0

  a++; // a++ is known as postfix. Add 1 to a, returns the old value.
  ++b; // ++b is known as prefix. Add 1 to b, returns the new value.

  printf("%d\n", a); // prints 1, should print 0?
  printf("%d\n", b); // prints 1, should print 1

  return 0;
}
tripleee
  • 175,061
  • 34
  • 275
  • 318
LoLei
  • 397
  • 2
  • 6
  • 17
  • 2
    No you get difference only if you have something like printf("%d\n", a++); printf("%d\n", ++b); – Srinath Mandava Dec 15 '14 at 09:47
  • The behavior is correct, your understanding of post-increment is wrong. The behavior you want would be observable if you would do `printf("%d\n", a++);`, it would print then would increment. In your case it increments and then you print it. – bitcell Dec 15 '14 at 09:48
  • you aren't using the returned value, so you shouldn't expect to see a difference – Tom Tanner Dec 15 '14 at 09:49
  • Not sure why the hoard of downvotes. This isn't a bad question in and of itself. There is definitely a huge culture of bad questions and this doesn't really fall into that category. – Qix - MONICA WAS MISTREATED Dec 15 '14 at 10:47
  • Thank you Qix. I guess I should have checked more thoroughly if a similar question already existed. – LoLei Dec 16 '14 at 16:52

4 Answers4

0

No a is also incremented by 1.

If you have something like

p = a++;

Here

p = 0

and if you have

p = ++a;

Here

p =1

So you can assign the value of your variable to some other as shown above and test what happens with post and pre incrementation.

Gopi
  • 19,784
  • 4
  • 24
  • 36
0

In case of prefix increment, the ++.. operator is evaluated and the increment is performed first, then that incremented value becomes the operand.

In case of post increment, the ..++ operator is evaluated and the increment is scheduled once the other evaluations including that operand are finished. It means, the existing value of the operand is used in the other evaluation and then the value is increased.

To understand it better, use two more variables, c and d, and check the values as below.

#include <stdio.h>
int main() {
  int a = 0;
  int b = 0;
  int c = 0;
  int d = 0;   

  printf("%d\n", a); // prints 0
  printf("%d\n", b); // prints 0

  c = a++; // a++ is known as postfix. Add 1 to a, returns the old value.
  d = ++b; // ++b is known as prefix. Add 1 to b, returns the new value.

  printf("%d\n", a);   // is 1
  printf("%d\n", b);    // is 1
  printf("%d\n", c);    // is 0; --> post-increment
  printf("%d\n", d);    // is 1  --> pre-increment

  return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • whoa!! Dear downvoter, any comments, please? – Sourav Ghosh Dec 15 '14 at 09:57
  • 1
    Answer to an obvious duplicate question, hence is not useful (in my opition) – MatthiasB Dec 15 '14 at 09:59
  • @MatthiasB well, right you are, but then, did you notice, the dupe-link _does not_ have an accepted answer? maybe, in process, that question can be linked to this as dupe, provided some better _accepted answer_ may be there. thanks. :-) – Sourav Ghosh Dec 15 '14 at 10:02
  • 1
    @MatthiasB You may downvote as you please, but usually you don't downvote answers on a dupe question. There's no point to. – Qix - MONICA WAS MISTREATED Dec 15 '14 at 10:16
  • A valid point. Maybe it would be an option to merge questions, but I don't know if there is a way to initiate this. @Qix I did some research on Meta and this seems to be the consensus, so my appologies. – MatthiasB Dec 15 '14 at 12:33
0

Remember, C and C++ are somewhat expressive languages.

That means most expressions return a value. If you don't do anything with that value, it's lost to the sands of time.

The expression

(a++)

will return a's former value. As mentioned before, if its return value is not used right then and there, then it's the same as

(++a)

which returns the new value.

printf("%d\n", a++); // a's former value
printf("%d\n", ++b); // b's new value

The above statements will work as you expect, since you're using the expressions right there.

The below would also work.

int c = a++;
int d = ++b;

printf("%d\n", c); // a's former value
printf("%d\n", d); // b's new value
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
0

a is incremented, but it returns the old value. But as it is alone on a line, the result is ignored. Try this to illustrate the difference:

#include <stdio.h>
int main() {
  int a = 0;
  int b = 0;

  printf("%d\n", a++);
  printf("%d\n", ++b);

  return 0;
}
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195