What do alternating plus and minus operators do?

Viewed 109

For some reason, this program compiles in C:

int x;
x = 3+-+-5+-5;
printf("%d\n",x);

And in general, alternating "+" and "-" compiles. It seems like if there are odd "-" then it'll subtract, otherwise, add.

What in the world is this?

1 Answers

In cases such as these, the first + or - to the right of the left-hand operand indicates the binary operation to perform; the other + and - after that are the unary + and - operators applied to the right-hand operand. The unary + operand does nothing and - changes the sign. This gives rise to the behavior you see: an odd number of - is either addition of a negative quantity (+ followed by +s and an odd number of -s) or subtraction of a positive quantity (- followed by +s and an even number of -s).

Related