开发者

How do Unary operators work in C, you cannot pre increment zero

开发者 https://www.devze.com 2023-03-15 16:26 出处:网络
I am confused about how Unary Operators work in C for Ones Complement, Logical Negation and preincrementing.

I am confused about how Unary Operators work in C for Ones Complement, Logical Negation and preincrementing.

Ones complement works against 0 like this:

int main()
{
    int a;
    a = ~0;          // Ones complement
    printf("%d",a);  // prints as -1.
}

And logical negation works against 0 like this:

int main()
{
    int a;
    a = !0;          // Logical negation
    printf("%d",a);  // prints as 1.
}

But Preincrement against 0 generates a compiler error:

int main()
{
    int a;
    a = ++0;        //pre incrementing 0.  error: non-lvalue in incremen开发者_如何学JAVAt
    printf("%d",a); 
}

Why don't all three work considering they are all Unary Operators?


The increment (++) and decrement (--) operators modify the thing that follows them. You can't modify a literal or a constant. In contrast, the ! and ~ operators merely operate on a value, they don't then assign the result anywhere.

Loosely speaking, ++n means n = n + 1; n. That is, "take the value of n, add one to it, write that value back to n, and return the new value as the value of the expression." So ++0 would mean 0 = 0 + 1; 0: "take the value of 0, add one to it, write that back to 0, and return the new value as the result of the expression." Literals and constants cannot be left-hand values (you can't assign to them).

In contrast, ~n means "take the value of n and apply a bitwise NOT operation to it, return the result as the result of the expression". n is unchanged, ~ doesn't write back the updated value to its operand.

So for example:

int n = 0;
int a;
a = ~n;
printf("a = %d, n = %d\n", a, n); // "a = -1, n = 0" -- `n` is unchanged

vs.

int n = 0;
int a;
a = ++n;
printf("a = %d, n = %d\n", a, n); // "a = 1, n = 1" -- `n` is changed

Increment (++) and decrement (--) are just different in that way than for ! or ~ (or, I think, any other unary operator — at least, I can't immediately think of any others that modify their operand).


++ and -- don't just apply an operation to a value, they change the value itself. This behavior wouldn't make much sense on a literal.

The other unary operators that you refer to -- namely ~ and ! -- do not change the value of their operand, they just perform an operation on its value.


You're trying to increment a literal value. Since operation x++; is a synonym for x=x+1; this means you are trying to set a new value for 0, which is not a variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消