Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between += and =+ in C++

Tags:

c++

operators

While programming in C++, I often confuse both "+=" and "=+", the former being the operator I actually mean. Visual Studio seems to accept both, yet they behave differently and is a source for a lot of my bugs. I know that a += b is semantically equivalent to a = a+b, but what does "=+" do?

like image 318
spurra Avatar asked Oct 17 '13 12:10

spurra


People also ask

What is difference between || and && in C?

A more detailed explanation on how these work is this: OR ( || ) - If EITHER or BOTH sides of the operator is true, the result will be true. AND ( && ) - If BOTH and ONLY BOTH sides of the operator are true, the result will be true. Otherwise, it will be false.

What is the difference between and || operator in C?

The | operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand. The conditional logical OR operator ||, also known as the "short−circuiting" logical OR operator, computes the logical OR of its operands.

What does <= mean in C?

Less than or equal to operator is a logical operator that is used to compare two numbers.

Why is && used in C?

The && (logical AND) operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int .


1 Answers

=+ is really = + (assignment and the unary + operators).

In order to help you remember +=, remember that it does addition first, then assignment. Of course that depends on the actual implementation, but it should be for the primitives.

like image 100
Daniel A. White Avatar answered Oct 02 '22 12:10

Daniel A. White