Unary operator


Unary operator are consisting of three operators. Unary operator. These operator are -
1. ) Unary minus operator(-)
2. )  Increment operator(++)
3.)  Decrement operator(--)
Unary operator having high precedence  than binary operator.Let us know binary operator

Binary operator   

 

All the operator that operate on two operand is called binary operator example - all the
arithmetic operator,equality operator ,relational operator, all logical operator,assignment operator are examples of binary operator.

Unary minus operator(-)


Unary minus operator work as if number is positive then after applying it converted negative number,if number is negative it converted positive after applying.for ex:-suppose
X and Y two variables.
X,Y=101;
suppose we want to apply  unary minus operator on the Y and assign in X.
X= -(Y)
The value assign in X variable comes-101 after applying the unary operator that is negative 
same as  X, Y= -101  :-
X=  -(-Y);  negative  value assign in  in X variable come positive  100  .


Increment operator(++)


These operator works on the single operands.In this operator after the increment operator value increase by one.It have two variants post increment and  Pre increment .

Post increment 


In case of  post increment first value becomes assign then value increase by one. For example - two variables  declare in the program A and B.
int A,B=10;
A=B++;
Value of B firstly assign in A after that value of B increase by one. Let us see program example -

#include<stdio.h>
int main()
{
int A,B=20;
A=B++;
printf("%d",A);
printf("%d",B);
return 0;
}
Output :  2021
Run this code

Pre increment


In case pre increment value firstly increase  by one then value assign. For example - int    A,B=20;
A=++B;
Here value of B increase  by one in variable A then  value assign lets understand with program
example :-

 #include<stdio.h>
 int main()
{
int A,B=20;
A=++B;
printf("%d",A);
printf("%d",B);
return 0;
}
Output :  2121
Run this code

Decrement operator(--) 


These operator works on the single operands .In this operator after applying the decrenment
operator value decrease by one.It have two variants Post decrement and  Pre decrement.

Post decrement 


In case  of  post decrement first value becomes assign then value decrease by one .For example -two variables  declare in the program A and B.
int A,B=10;

A=B--;

Value of B firstly assign in A after that value of B decrease by one.Let  us see program example-

#include<stdio.h>
int main()
{
int A,B=20;
A=B--;
printf("%d",A);
printf("%d",B);
return 0;
}
Output :-  2019
Run this code

Pre decrement 


In case pre decrement value firstly decrease  by one then value assign .For example -int A,B=20;
A= --B;
Here value of B decrease  by one in variable A then value assign lets understand with
program
Example-

 #include<stdio.h>
 int main()
{
int A,B=20;
A= --B;
printf("%d",A);
printf("%d",B);
return 0;
}
Output :   1919
Run this code

Conditional operator 


In this operator operator condition becomes check.It is also called ternary operator .Let us see syntax of conditional operator. this operator is also called ternary operator.

result=expression 1? expression 2 : expression 3

At the expression 1 becomes written a condition  if condition is true then expression 2 execute otherwise  expression 3 executed and that variable assign . for example -
result=30<40?30:40;

#include<stdio.h>
int main()
{
int result;
result=30<40?30:40;
printf("%d",result);
return 0;
}
Output :  30
Run this code
here output is 30 because  condition is true.

#include<stdio.h>
int main()
{
int result;
result=40<30?30:40;
printf("%d",result);
return 0;
}
Output :-   40
Run this code
here output is 40 because  condition is false.

<<Prev                                  Next>>