What is the difference between "++" and "+= 1 " operators?
num += 1
is rather equivalent to ++num
.
All those expressions (num += 1
, num++
and ++num
) increment the value of num
by one, but the value of num++
is the value num
had before it got incremented.
Illustration:
int a = 0;
int b = a++; // now b == 0 and a == 1
int c = ++a; // now c == 2 and a == 2
int d = (a += 1); // now d == 3 and a == 3
Use whatever pleases you. I prefer ++num
to num += 1
because it is shorter.
prefix and postfix operations are perfect candidates for exam questions.
a = 0;
b = a++; // use the value and then increment --> a: 1, b: 0
a = 0;
b = ++a; // increment and then use the value --> a: 1, b: 1
+=
operation and its sister -=
are more general solutions mostly intended to be used with different numbers. One might even say they are redundant when used with 1
. When used with 1
they mostly act as a prefix operation. In fact on my machine they produce the same machine code. You can try this by using an example program such as:
void foo() {
int a, b;
a = 0;
// use one of these four at a time
b = a++; // first case (different)
b = ++a; // second case
b = (a += 1); // third case
b = (a = a + 1); // fourth case
}
int main() {
foo();
return 0;
}
and disassembling in gdb
which would give:
first case (a++
) (different)
(gdb) disassemble foo
Dump of assembler code for function foo:
0x00000000004004b4 <+0>: push %rbp
0x00000000004004b5 <+1>: mov %rsp,%rbp
0x00000000004004b8 <+4>: movl $0x0,-0x8(%rbp)
0x00000000004004bf <+11>: mov -0x8(%rbp),%eax
0x00000000004004c2 <+14>: mov %eax,-0x4(%rbp)
0x00000000004004c5 <+17>: addl $0x1,-0x8(%rbp)
0x00000000004004c9 <+21>: pop %rbp
0x00000000004004ca <+22>: retq
End of assembler dump.
second case (++a
)
(gdb) disassemble foo
Dump of assembler code for function foo:
0x00000000004004b4 <+0>: push %rbp
0x00000000004004b5 <+1>: mov %rsp,%rbp
0x00000000004004b8 <+4>: movl $0x0,-0x8(%rbp)
0x00000000004004bf <+11>: addl $0x1,-0x8(%rbp)
0x00000000004004c3 <+15>: mov -0x8(%rbp),%eax
0x00000000004004c6 <+18>: mov %eax,-0x4(%rbp)
0x00000000004004c9 <+21>: pop %rbp
0x00000000004004ca <+22>: retq
End of assembler dump.
third case (a += 1
)
(gdb) disassemble foo
Dump of assembler code for function foo:
0x00000000004004b4 <+0>: push %rbp
0x00000000004004b5 <+1>: mov %rsp,%rbp
0x00000000004004b8 <+4>: movl $0x0,-0x8(%rbp)
0x00000000004004bf <+11>: addl $0x1,-0x8(%rbp)
0x00000000004004c3 <+15>: mov -0x8(%rbp),%eax
0x00000000004004c6 <+18>: mov %eax,-0x4(%rbp)
0x00000000004004c9 <+21>: pop %rbp
0x00000000004004ca <+22>: retq
End of assembler dump.
fourth case (a = a + 1
)
(gdb) disassemble foo
Dump of assembler code for function foo:
0x00000000004004b4 <+0>: push %rbp
0x00000000004004b5 <+1>: mov %rsp,%rbp
0x00000000004004b8 <+4>: movl $0x0,-0x8(%rbp)
0x00000000004004bf <+11>: addl $0x1,-0x8(%rbp)
0x00000000004004c3 <+15>: mov -0x8(%rbp),%eax
0x00000000004004c6 <+18>: mov %eax,-0x4(%rbp)
0x00000000004004c9 <+21>: pop %rbp
0x00000000004004ca <+22>: retq
End of assembler dump.
As you can see they produce the same machine code even without compiler optimizations turned on except the first case which has addl
after mov
s. This means that you should be using whichever you like as a user and let the compiler guys do the rest.
And lastly, note that cousin operators *=
and /=
have no postfix and prefix counterparts.
Both the operators increase the value of n by 1. The difference between them exists when you use the operators along with the assignment operator.
For example:
First Case --Post-Increment operator
int n=5;
int new_var;
new_var=n++;
print("%d",new_var);
Output=5
Second Case
int n=5;
n+=1;
new_var=n;
print("%d",new_var);
Output =6
This is very similar to what pre-increment operator would result in.
Second Case using Pre-increment operator
int n=5;
new_var=++n;
print("%d",new_var);
Output =6
The ++
prefix or postfix operators change the variable value.
int a = 0;
int b = a++; // b is equal to 0, a is equal to 1
Or prefix:
int a = 0;
int b = ++a; // b = 1, a = 1
If used like this, they are the same:
int a = 0;
++a; // 1
a++; // 2
a += 1; // 3