0

According to this:

The expression ++n increments n before its value is used, while n++ increments n after its value has been used.

why this source code:

int j = 0;
j = j++;
printf("j: %d\n",j);
j++;
printf("j (again): %d\n",j);

prints this:

enter image description here

instead of printing this:

j: 1
j (again): 2

I thought that it should be something like the following:

First, put to j the value of itself, which is 0
Then, increase the j by 1 and now j will be 2 from now on

What did I miss here?

LiTTle
  • 1,811
  • 1
  • 20
  • 37
  • That quote is wrong. – pmg Oct 25 '19 at 13:10
  • 1
    @LiTTle This statement j = j++; has undefined behavior. Moreover in this expression j++ there is not used an unary operator. There is used a postfix operator. – Vlad from Moscow Oct 25 '19 at 13:12
  • @VladfromMoscow this cannot be happening to me! I am coming from Java and I am falling into undefined behaviors the whole time while I am trying to experiment with C! – LiTTle Oct 25 '19 at 13:13
  • 1
    @LiTTle Don't blame yourself. The choice to make `i++` and `++i` expressions rather than statements was a terrible language choice (imo) that has made thousands of people ask questions about it here – byxor Oct 25 '19 at 13:14
  • 2
    The root of the problem is that there is never a reason to write code such as `j = j++;`. Neither in C nor in Java. – Lundin Oct 25 '19 at 13:47
  • @byxor If they were statements, how would you write things like `*p++ = 0` and `a[i++] = b[j++]` and all the other useful, well-defined uses of `++` (and `--`)? – Steve Summit Oct 25 '19 at 13:51
  • @LiTTle Just learn these two rules: Within a single expression, (a) don't try to modify the same variable twice, and (b) don't try to modify a variable in one part of the expression and then use its value in another part of the expression. (And if these rules seem strange, it's because in ordinary programming, you don't actually end up in situations where you'd violate them very often. It's like making a rule "Don't step on your head.") – Steve Summit Oct 25 '19 at 13:54
  • @SteveSummit Something like `*p; p++;` (if that does the same thing, I'm not entirely sure tbh and don't have time to check). It's useful syntax, but is it really worth all the confusion it causes on here? – byxor Oct 25 '19 at 14:01
  • @byxor: You weren’t there. The pre- and post-increment operators were a splendid innovation at the time. Programming used to be hard (relative to the amount accomplished, by today’s standards), and you have a lot of people to thank for the work put into it and the tools you have today. – Eric Postpischil Oct 25 '19 at 14:02
  • @EricPostpischil I agree that they're useful, but it doesn't change the fact that they cause a lot of confusion. Thankfully golang turned it into a statement for this very reason. I'm grateful for these operators regardless of my criticism – byxor Oct 25 '19 at 14:10
  • My qview is that ++ and +=, and the rest not only produce shorter statements that are hopefully easier to read, but they also encapsulate the idea that you "just want to increment". There is no ambiguity: If I write "a = a + 1" another programmer can come along and ask "did she mean +2?" "did she mean "s = a + 1?", but no, it is clear that I meant "increment a". I might have been wrong, but that is what I intended to do! Much review time is saved. – Gem Taylor Oct 25 '19 at 15:05
  • @byxor Here's the thing about all the confusion caused by `++` and `--`. Yes, it's truly remarkable -- and somewhat alarming -- how impossibly many beginning C programmers have problems with these operators, and how many identical questions they ask about them here. By the same token, every human had to suffer a few scrapes and bruises while they were learning how to walk, and then run. We could say, "Walking and running are too dangerous, we should all satisfy ourselves with crawling, and with pads to protect our knees." But then we'd never have the Olympics, and stuff. – Steve Summit Oct 25 '19 at 15:09
  • @SteveSummit I like the analogy, but the difference in effectiveness between crawling and running is much larger than the difference between increment-expressions and increment-statements. A closer analogy would be "I'd prefer if you ran with jogging shoes rather than rocket-powered shoes" :) But yes, the operators have a lot of power when used properly, you just have to pray that you're smart enough to use them properly – byxor Oct 25 '19 at 15:19
  • Stated another way, the problem is not that every beginning C programmer has trouble with `++` and `--`. No, the problem is that when they ask about their problems here, they're apt to be mocked and belittled, as if there's something wrong with them. These questions are 100% completely and totally reasonable -- and the fact they get asked so often proves that. (Now, it's true, it'd be nice if, having imagined the question, the questioners could get good answers *without* having to ask the questions over and over again publicly, but that's a different problem.) – Steve Summit Oct 25 '19 at 16:00
  • Thank you every body for the support. Continuing the conversation I have another example here (`int i = 0; a[i] = i++; printArray(a); // 10, 0, 12, 13, 14`). In this one, imo, it behaves different. It seems that the `i` is stored (it changes `a[1]` instead of `a[0]`). I have never used these features in Java, nor I have seen them be used. And I am not going to use them in C soon, because even me will not understand my writings at the future! – LiTTle Oct 26 '19 at 14:24

0 Answers0