This afternoon, I really don't know what I was doing with Operators and C. Eventually, I wrote some code which I was thinking wouldn't compile, But I don't know how it worked.
The code is:
#include <stdio.h>
int main()
{
int n=2;
int sum = n + - + - + - + n; /* This line */
printf("%d\n", sum);
return 0;
}
And the output is:
0
I am completely confused how the code compiled and what is happening behind the scene.
How does the line int sum = n + - + - + - + n;
work?
is equivalent to:
or simply
n + (-n)
Note that unary operators bind more tightly than binary operators in
C opeartor precedance table
and associativity of unary operator+-
is from right to left while of binary+-
operators in from left to right.