You are on page 1of 2

Unary plus (+) Operator

This operator does not make any effect on the operand value, it just returns operands value.

Unary minus (-) Operator


This operator makes the value negative. It makes positive value to negative and negative value to positive.

1. #include<stdio.h>
int main()
{
int x, y;
x = 5;
y = x++ / 2;
printf("%d", y);
return 0;
}

Output: 2 (The given expression is:- y=x++ / 2; x++ so post-increment, y=5/2=2. Hence, x=6 and y=2)

2. #include<stdio.h>
int main()
{
int a=4,b,c;
b = --a;
c = a--;
printf("%d %d %d",a,b,c);
return 0;
}

Output: 2 3 3
The first expression is b=–a; so, a becomes 3 (a=3) and b=3. Now, c=a–; so, c=3 and a=2. Finally, a=2, b=3 and
c=3

3. #include<stdio.h>
int main()
{
int a=5;
printf("%d", ++a++);
return 0;
}

Output: Compile-time error

4. #include<stdio.h>
int main()
{
int a=3, b=9;
printf("%d ", ++(a*b+1));
return 0;
}

Output: Compile-time error


The operand of increment and decrement operators must be a variable, not a constant or expression
5. #include<stdio.h>
int main()
{
int a=10, b=10,c;
c= ++a+b;
printf("%d %d %d",a,b,c);
return 0;
}

Output: 11 10 21 First value of a is incremented by 1 then it is added to the value of b.

6. #include<stdio.h>
int main()
{
int a=10, b=10,c;
c= ++a+b++;
printf("%d %d %d",a,b,c);
return 0;
}

Output: 11 11 21 First value of a is incremented by 1 then it is added to the value of b. After that value of b is
increase by 1.

7. #include<stdio.h>
int main()
{
int a=2,b=1,c=0;
c = a +++ b;
printf("%d %d %d",a,b,c);
return 0;
}

Output: 3 1 3 The expression c=a+++b; is treated as c=(a++)+b; so, a=3, b=1, c=3

8. #include<stdio.h>
#include<math.h>
int main()
{
int a=2,b;
b=a++ + a-- + ++a + --a;
printf("%d %d",a,b);
}

Output: 2 10

You might also like