More Assignment Operators: +=, −=, *=, /=, %=

C has several assignment operators. The most basic one, of course, is =, which simply assigns the value of the expression at its right to the variable at its left. The other assignment operators update variables. Each is used with a variable name to its left and an expression to its right. The variable is assigned a new value equal to its old value adjusted by the value of the expression at the right. The exact adjustment depends on the operator, for example,

scores += 20is the same asscores = scores + 20
dimes −= 2is the same asdimes = dimes − 2
bunnies *= 2is the same asbunnies = bunnies * 2
time /= 2.73is the same astime = time / 2.73
reduce %= 3is the same asreduce = reduce % 3

In the preceding ...

Get C Primer Plus®, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.