2

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 increment
    printf("%d",a); 
}

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

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
jack
  • 145
  • 1
  • 9

3 Answers3

12

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).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • but how ~ its modifying the constant? – jack Jun 28 '11 at 06:27
  • @jack: no, `~` is **not** modifying the constant. It's taking the value, operate on the value and return a new value. It doesn't change the thing it operates on itself. – Joachim Sauer Jun 28 '11 at 06:31
  • 1
    @jack: It isn't. `~n` means "take the value of `n` and apply `~` to it, return the result as the result of the expression" (`n` is unchanged). In contrast, `++n` means "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." It's that bit that writes the result back to the operand that's different for `++` and `--`. – T.J. Crowder Jun 28 '11 at 06:32
  • yeah i understood now so it performs these operations on RAM and store them back i really understood now after that line ++n=n+1 and ++0=0+1. thank you guys you both are great – jack Jun 28 '11 at 06:48
  • @crowder can i add you on my msn or facebook is that allowed ? i got some good friends here i hope they help me to get me the problems i face through in c. – jack Jun 28 '11 at 06:51
  • i want to be in contact with them can i add ? – jack Jun 28 '11 at 06:51
  • @Jack, you still are missing something crucial: ++ *cannot* be used on constants or literals, therefore, what you said "++0=0+1" is not valid. It would been correct to say something like "int x = 0; ++x == 0 + 1". – Coleman S Jun 28 '11 at 07:54
  • @Coleman: He said he understands now. @jack: In terms of staying in touch, just post further questions here on StackOverflow. There are people here who are *dramatically* more knowledgeable than I about C (and even more so about C++, if you're going to go that route). – T.J. Crowder Jun 28 '11 at 07:58
  • gosh i just made it you know i was hurry to leave so it just happened people here worry so much let me change it i was busy moms calling out for lunch so wait a minute i will change it – jack Jun 28 '11 at 08:00
  • why people keep on giving me downvote did i do anything wrong ? – jack Jun 28 '11 at 08:01
  • 1
    @jack: I don't know why people are downvoting. I will say that it helps when you use punctuation, capitalization, and complete sentences, both in questions and in comments. I mean *"gosh i just made it you know i was hurry to leave so it just happened people here worry so much let me change it i was busy moms calling out for lunch so wait a minute i will change it"* is pretty...stream-of-consciousness. :-) Also note that StackOverflow is not a discussion site. It's a Q&A site. See [the FAQ](http://stackoverflow.com/faq) for the distinction. – T.J. Crowder Jun 28 '11 at 08:03
  • cool ok i understand but truly and honestly and frankly I text to my friends on mobile and chat on facebook with friends. so I just got habitutated to it.i hope you guys understand it anyways but still i love it because they are people who gives me the answers sorry sorry i wont comment nomore sorry again i stop my discussion – jack Jun 28 '11 at 08:08
4

++ 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.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
2

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.