C++ Syntax: Assignment: = op=

Description

The assignment operator, as in FORTRAN, takes the value of the expression on the right hand side and stores it in the variable on the left hand side. For example:- sum = sum + count; The above construction is so common that C++ has a shortcut for it:- sum += count; and the same idea works for any binary operator e.g.:- weight *= scale; i.e. by following a binary operator directly by an =.

Unlike FORTRAN, the assignment operator, like all other C++ operators, returns a value (its the value of the right hand side). This means that the assignment is itself just a value that can be assigned so that:-

a = ( b = 0); sets both a and b to zero. The above brackets are unnecessary:- a = b = 0; is the same as the order in which assignment operators work is right to left (so a gets a 0 not what was in b before it got a zero). See operator precedence

Other Uses for = op=

None.

Usage Notes

Be careful not to confuse = with ==
Go Back to the The C++ Crib Top Page


If you have any comments about this page please send them to Nick West