0
#include<stdio.h>
int main(){
int num,i=0;
num=-++i+ ++-i;
printf("%d",num);
return 0;
}

I am getting compilation error for above example. I understood like, unary (+) operator returns const value. but below example is not satisfying that condition.

#include<stdio.h>
int main(){
int x,i=1;
x=~-i;
printf("%d",x);
getchar ();
return 0;
}

Here I am getting '0' as answer.

Can anyone explain me, what exactly '+' operator does in c.

Rikesh
  • 26,156
  • 14
  • 79
  • 87
Nagarjuna
  • 46
  • 5
  • Before that what are you trying to achieve with your code? `x=~-i;`? – Gopi Feb 19 '15 at 07:26
  • Ignoring the fact that applying `++` twice in the same expression without a sequence point is undefined behavior, how would you expect the second `++` of `num=-++i+ ++-i;` to work? – Sergey Kalinichenko Feb 19 '15 at 07:31
  • 1
    For your question "Can anyone explain me, what exactly '+' operator does in c.", a `+` operator is used as it should be, to add numbers. `++` is used to increment the value by 1. For example `i++` increases the value of `i` by 1. – Arun A S Feb 19 '15 at 07:33
  • Also see http://stackoverflow.com/questions/949433/why-are-these-constructs-using-undefined-behavior – Lundin Feb 19 '15 at 07:44
  • @ArunA.S To confuse things a bit, there are actually two + operators in C, the binary one and the unary one. The unary plus is mildly useful though, mainly included to make the language consistent, as there is also the far more useful unary minus. – Lundin Feb 19 '15 at 07:46
  • @Lundin, I have pointed out both of these in my comment "a `+` operator is used as it should be, to add numbers.", that is binary `+` and " `++` is used to increment the value by 1", that is unary. – Arun A S Feb 19 '15 at 07:51
  • @ArunA.S I was speaking of binary `+` and unary `+`. Anyway, it was just a side note, not important :) – Lundin Feb 19 '15 at 07:57
  • @Lundin, thanks for notifying, you meant that I didn't say about unary `+`.( that is `+1`,`+2`, etc... the one used to specify that it is positive ) – Arun A S Feb 19 '15 at 08:00

0 Answers0