A little bit of whitespace might help clarify this for you.
When you write i=+1
, what is actually happening is i = +1
. This is because there is no =+ operator in C. The =
is treated as its own operator, and the +
is a unary operator acting on the constant 1
. So the evaluation of this statement starts on the right hand side of the =
operator. +1
will evaluate to 1
and the =
operator will then assign that value to the variable i
.
+=
is it's own operator in C, which means "add the value of the expression on the right side of this operator to the variable on the left side, and assign it to that variable, so something like the following:
i = 3;
i += 2;
would evaluate to 5
because the +=
operator will evaluate the right side of the operator (in this case 2
, and will add it to the left side (in this case, i has a value of 3) and will assign it to the variable on the left. In essence, this becomes i = (2 + 3)
. Therefore, the variable i
will have a final value of 5
.
If you're just adding the value 1
to an integer variable, you can use the ++
operator instead. Adding this operator after a variable (i.e. i++
) will cause the current line of code to execute before incrementing the variable by one. Prefixing the operator in front of the variable will cause the statement to be executed after the variable is incremented.
e.g.:
i = 5;
j = i++;
will result in i = 6
and `j = 5', whereas
i = 5;
j = ++i;
will result in i = 6
and j = 6
.
Similarly, the --
operator can be used to decrement (decrease) the variable by one, similar to how ++
will increment (increase) the variable by one. The same rules regarding positioning the operator before or after the variable apply to both operators.
Hope this clears things up a bit.