[–] scandalous-goat ago (edited ago)
I prefer x += 1 for an incrementation statement, as the intent is, IMO clearer. But I'm sure plenty would disagree with me.
I didn't compare the compiler's output for all three syntax, though.
I just did a test. The compiler produces exactly the same code for all three syntaxes if they are used as a statement.
int postfix()
{
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 10 sub $0x10,%esp
int x = 1;
6: c7 45 fc 01 00 00 00 movl $0x1,-0x4(%ebp)
x++;
d: 83 45 fc 01 addl $0x1,-0x4(%ebp)
return x;
11: 8b 45 fc mov -0x4(%ebp),%eax
}
14: c9 leave
15: c3 ret
00000016 <_prefix>:
int prefix()
{
16: 55 push %ebp
17: 89 e5 mov %esp,%ebp
19: 83 ec 10 sub $0x10,%esp
int x = 1;
1c: c7 45 fc 01 00 00 00 movl $0x1,-0x4(%ebp)
++x;
23: 83 45 fc 01 addl $0x1,-0x4(%ebp)
return x;
27: 8b 45 fc mov -0x4(%ebp),%eax
}
2a: c9 leave
2b: c3 ret
0000002c <_statement>:
int statement()
{
2c: 55 push %ebp
2d: 89 e5 mov %esp,%ebp
2f: 83 ec 10 sub $0x10,%esp
int x = 1;
32: c7 45 fc 01 00 00 00 movl $0x1,-0x4(%ebp)
x += 1;
39: 83 45 fc 01 addl $0x1,-0x4(%ebp)
return x;
3d: 8b 45 fc mov -0x4(%ebp),%eax
}
40: c9 leave
41: c3 ret
This is one of the things I think makes programming interesting, the different styles there is. The most interesting example I think of is Fox-Toolkit's style, the author has an, I may say, interesting style...
[–] RevanProdigalKnight ago (edited ago)
Just in case you didn't already know, there is actually a difference between
counter++and++counter, as the first increments after reading the value and the second before reading the value, e.g.:Most people use
counter++because of this behavior being closer to the logical alternative ofcounter += 1.