-29

Why does num1++ not increment in the printf()?

int num1 = 1;
printf("num1=%d", num1++);

Why does this print num1=1 instead of num1=2?

user16217248
  • 3,119
  • 19
  • 19
  • 37
4thSpace
  • 43,672
  • 97
  • 296
  • 475

5 Answers5

9

The ++ does increment the operand ... but in its postfix form, it evaluates to the value before incrementing.

++num1 instead would evaluate to the value after incrementing.

Siyual
  • 16,415
  • 8
  • 44
  • 58
6

Because the expression

num1++

evaluates to num1.


You may want to do:

++num1

which evaluates to num1 + 1.


Note however that both expressions increment num1 by one.

Evaluating num1 in the next statement evalutes to the incremented value.


In short

In C, why doesn't num1++ increment in the printf()?

num1++ does increment num1 but it evaluates to num1 and that evaluation is what you are passing to printf().

JFMR
  • 23,265
  • 4
  • 52
  • 76
  • Yes, but why? I mean, I know why, but if it was so obvious OP wouldn't have asked. – Federico klez Culloca May 25 '17 at 15:34
  • @FedericoklezCulloca because both operators are useful in different cases? – Quentin May 25 '17 at 15:39
  • @Quentin not "why does it exist", why `n++` evaluates to `n` if I didn't know `++n` was different (or even existed). I was asking Neroku to better detail their answer. – Federico klez Culloca May 25 '17 at 15:43
  • @FedericoklezCulloca This might be me being stupid, but I don't see any other answer to that than "because C was designed that way"... – Quentin May 25 '17 at 15:45
  • @Quentin I updated my last comment. I hope it's more clear now. – Federico klez Culloca May 25 '17 at 15:45
  • I guess I'm not following. If you do num1++, it will increment. If you do it in printf, it will not. You are saying num1++ evaluates to num1. I don't know what that means since it increments the value but not when you do it in printf. – 4thSpace May 25 '17 at 15:45
  • @FedericoklezCulloca, for example, you can use `*p++` and `*++p`. It depends on when do you want to obtain the incremented value. – JFMR May 25 '17 at 15:47
  • @4thSpace both `++` operators have the *side-effect* of incrementing their operand. But they also have a (*evaluate to a*) value, which is what you pass to `printf`. They differ in what their value is. – Quentin May 25 '17 at 15:47
  • @Neroku, I know C, I'm not asking for myself. But as you see 4thSpace is still confused, what I'm saying is that your answer is not clear for someone who's new to these kind of things. – Federico klez Culloca May 25 '17 at 15:49
  • I understand now. A better answer would simply have been: with num1++, you'll see the updated result on the next line. With ++num1, you'll see the result on the current line. – 4thSpace May 25 '17 at 15:51
  • 1
    And that answer would be wrong. C doesn't have a notion of "lines" in this meaning. It has a notion of "sequence points".. – Eugene Sh. May 25 '17 at 15:53
  • @EugeneSh. I've never talked about "lines" – JFMR May 25 '17 at 15:54
  • @Neroku Sorry for not prefixing, but I've commented to the chronologically previous comment of the OP – Eugene Sh. May 25 '17 at 15:55
  • @FedericoklezCulloca after *side effects* (either for `++num1` or `num1++`) are over it is guaranteed that `num1` evaluates to the incremented value. Which operator you choose depends on your intention. – JFMR May 25 '17 at 15:57
  • Actually, another interesting question would be: ***when** does this increment take place?* Or *when is it guaranteed have been done?* – JFMR May 25 '17 at 16:05
3

It comes from the fact that the ++ is after the variable, this will solve your problem

printf("num1=%d", ++num1);

The way you did your variable will be incremented after printing out its content, so if you do another printf on this variable you should have the right value, by putting it in the prefix way, it will increment the variable before outputting it

Tylones
  • 117
  • 10
3

The postfix ++ operator evaluates to the current value of the operand, then it increments it. If you call printf again with num1 as the argument, you would see the effect of the increment.

From section 6.5.2.4 of the C standard:

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).

If you used the prefix ++ operator, i.e. ++num1, the increment would be reflected in the output.

dbush
  • 205,898
  • 23
  • 218
  • 273
3

Because having the ++ at the end of the variable causes the increment to happen after the operation. Adding the ++ before the variable will do the addition before the operation.

George Wilson
  • 39
  • 1
  • 6
  • Short answers such as this one are generally better suited as comments. You can support your response with a short code example. – 101010 Feb 13 '18 at 19:13
  • This is literally the simplest and best understandable answer under this post. If you're explaining simple things to a beginner programmer it's generally better to use everyday language and not phrases like postfix, prefix, evaluate, etc etc. – Andor Németh Feb 17 '20 at 13:46