Sie sind auf Seite 1von 2

Joe Zbiciak, Been programming since grade school

Answered Mar 31, 2018 · Upvoted by Quentin Lassalle, Master Computer Programming & Management,
Paris West University Nanterre La Defense (2018) and Dmitriy Genzel, PhD in CS

https://www.quora.com/What-is-the-difference-between-I-and-I-I-1-in-C-language
Short, simple, useful, but incomplete answer:

If you’re given two separate, complete C statements: i++; and i = i + 1;, both
statements will have the same net effect on the program. Both will add 1 to the value of i.
So, if you see a standalone i = i + 1; or i++ or even ++i;, all three have the same net
effect.
But, that’s not really what you asked.

All three are slightly different.

If you just consider these as expressions yielding a value, you get two unique behaviors out
of the three expressions:

 The expression i++ increments i, but returns the previous value of i.


 The expression ++i increments i, and returns the new value of i after assignment.
 The expression i = i + 1 adds 1 to i, writes it to i, and then returns the new
value of i after assignment, just like ++i.
The ‘after assignment’ bit is interesting. As it turns out, most integer computations end up
promoting toward C’s int type. But, both ++i and i = i + 1 appear to truncate to the
type of i. See for yourself: [Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ
The code:

1. #include <stdio.h>
2. #include <stdint.h>
3.
4. int main() {
5. uint8_t i;
6.
7. i = 255;
8. printf("i = 255; i++ = %d\n", i++);
9.
10. i = 255;
11. printf("i = 255; ++i = %d\n", ++i);
12.
13. i = 255;
14. printf("i = 255; (i = i + 1) = %d\n", i = i + 1);
15.
16. return 0;
17. }
The output:

1. i = 255; i++ = 255


2. i = 255; ++i = 0
3. i = 255; (i = i + 1) = 0
So really, i = i + 1 looks more like ++i than i++.
Now what about at the token level, especially in the context of macros? Now you’re in a
completely different realm. The ++ operator has very high precedence, while the = operator
has very low precedence.
So imagine you have this ugly bit of code:

1. #define MIN(a,b) a < b ? a : b


2.
3. int bad1(int i) {
4. return MIN(++i, 42);
5. }
6.
7. int bad2(int i) {
8. return MIN(i = i + 1, 42);
9. }
That’s horrible code no matter how you slice it. As you might guess, bad1 and bad2 expand
rather differently, with rather different outcomes.
How do they expand?

1. int bad1(int i) {
2. return ++i < 42 ? ++i : 42;
3. }
4.
5. int bad2(int i) {
6. return i = i + 1 < 42 ? i = i + 1 : 42;
7. }
In the bad1 case, I believe bad1 is weird, but at least does not invoke undefined behavior.
There is a sequence point between the evaluation of the condition for ?: and the evaluation
of the selected path through ?:.
In the bad2 case, I believe there is not a sequence point between the final evaluation on the
right-hand side and the final assignment to i on the left-hand side. That one takes you
into undefined behavior territory.
So, it’s a mess.

Ultimately, the two don’t differ much in practice. Use what’s most readable to you, or if you
have a set of coding guidelines you need to adhere to for class or $DAYJOB, just adhere to
that wherever it makes sense.

Das könnte Ihnen auch gefallen