#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
a=10;
b=++a; //b=11,a=11
c=a++; //c=11,a=12
d=--a; //d=11,a=11
e=a--; //e=11,a=10
f=--a; //f=9,a=9
g=--a; //g=8,a=8
h=++a; //h=9,a=9
printf("\n a= %d",a);
printf("\n b= %d",b);
printf("\n c= %d",c);
printf("\n d= %d",d);
printf("\n e= %d",e);
printf("\n f= %d",f);
printf("\n g= %d",g);
printf("\n h= %d",h);
getch();
}
----------------------------------------------------------------------
The unary operators requires only one operand to perform different kind of operations such as increasing/decreasing a value, negating an expression, or inverting a boolean value. These operators can not be used with finalvariables. There are different types of unary operators mentioned in the table given below:
Symbol Name of the Operator Operation Example
+ Unary plus operator indicates positive value (however, numbers are positive without this) int number = +1;
- Unary minus operator negates an expression number = - number;
++ Increment operator increments a value by 1 number = ++ number;
-- Decrement operator decrements a value by 1 number = -- number;
! Logical compliment operator inverts a boolean value
I. Unary Plus (+) Operator
Unary plus operator (+) indicates positive value. This (+) operator is used to perform a type conversion operation on an operand. The type of the operand must be an arithmetic data type i.e. if a value of the integer operand is negative then that value can be produced as a positively applying unary plus (+) operator. For example, lets see the expressions shown as:
int x = 0;
int y = (-25);
x = (+y);
In this expression, a negative value is assigned to the variable "y". After applying unary plus (+)operator on the operand "y", the value becomes 25 which indicates it as a positive value.
However a number is positive without using unary plus (+) operator, if we have initially assigned it positively into the operand in the program.
II. Unary minus (-) Operator
Unary minus operator (-) indicates negative value and differ from the unary plus operator. This (-) operator is also used to perform a type conversion operation on an operand. If a value of the integer operand is positive then that value can be produced as a negatively applying unary minus (-) operator. For example, lets see the expressions shown as:
int x = 0;
int y = 25;
x = (-y);
In this expression, a positive value is assigned tothe variable "y". After applying minus plus (-)operator on the operand "y", the value becomes "-25" which indicates it as a negative value. This behavior represents the number in two's complement format
No comments:
Post a Comment