sujai M J
sujai M J

Reputation: 1291

Difference between "+=" and normal addition

What is the difference in += and normal add

a = a + b;
a += b;

what is the different in above two lines?
Is there any increase in CPU cycles for "+=" operator?
Which would be the preferable method of coding.?

Upvotes: 4

Views: 424

Answers (7)

Sai Surya Palthi
Sai Surya Palthi

Reputation: 1

So, all the above answers given are accurate and cool, but what I want to add is for large or looping operations like for instance(refer the snippet) will give you tle(occurs very rarely but good to know). Especially if the array or the element we are adding to are of long/double/BigInteger format. Then it is better to use normal addition.

for(int i = 0; i < n; i++) {
    a += arr[0] * arr[i];
}
// for such cases just use
 for(int i = 0; i < n; i++ ) {
    a = a + arr[0] * arr[i];
}

Upvotes: 0

Grzegorz Szpetkowski
Grzegorz Szpetkowski

Reputation: 37914

Beside single evaluation of first operand, there is second difference, that occurs when b is an expression, involving operators with lower precedence. For instance:

int a = 1;
a += 0 || 1;

yields 2, while:

int a = 1;
a = a + 0 || 1;

stores 1 into a. The equivalent of the former statement would be:

a = a + (0 || 1);

Upvotes: 3

ilent2
ilent2

Reputation: 5241

When I compile the following two programs,

int main(int argc, char** argv)
{
  int a = 5, b = 10;

  a = a + b;

  return 0;
}

and

int main(int argc, char** argv)
{
  int a = 5, b = 10;

  a += b;

  return 0;
}

with gcc file.c -S -O0, I get the following assembler output (this is just the relevant section)

movl  $5, -4(%rbp)                     # Assign 5 to a
movl  $10, -8(%rbp)                    # Assign 10 to b
movl  -8(%rbp), %eax                   # Move b to eax
addl  %eax, -4(%rbp)                   # Add eax to a, store in a

This same output is produced with both implementations.

However, although everything works out nicely with integers and a single addition, there are cases where you may get different results, consider for double a,b the operation a *= b*b and a = b*a*b. If the result of the particular multiplication can't be exactly represented then the two operations will produce different results.

Upvotes: 2

haccks
haccks

Reputation: 106012

There is a difference in between them and it is explained in the C standard:

C11: 6.5.16.2 Compound assignment (p3):

Acompound assignment of the form E1 op= E2 is equivalent to the simple assignment expression E1 = E1 op (E2), except that the lvalue E1 is evaluated only once, and with respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation.

Upvotes: 3

rock321987
rock321987

Reputation: 11032

Here is a good explanation..Quoting from the source

x += y means

  • Find the place identified by x
  • Add y to it

But x = x + y means:

  • Evaluate x+y

  • Find the place identified by x

  • Copy x into an accumulator
  • Add y to the accumulator
  • Store the result in x
  • Find the place identified by x
  • Copy the accumulator to it

Upvotes: 0

pmg
pmg

Reputation: 108978

Difference is only in source. The output binary (executable) is exactly the same (with normal current compilers).

You might like one or the other better in different conditions.

I like the short form better, especially when a is complicated

vector[savedpos] = vector[savedpos] + 1000;
vector[savedpos] += 1000;

Upvotes: 1

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

Both the expressions are same. Second one is short hand assignment operator. Which will help us to reduce the reptitive coding.

a = a + b; // Here a will come twice.

a += b;

Here the repitition of code will be avoided.

Upvotes: 0

Related Questions