Friday, June 5, 2020

All Operators Explained - C (with Example)



 Here is list of all Types of Operators with Explanation and Example!!
Lets Start....


1. Arithmetic operators:- These are the symbols used to indicate the operations on operands.Here is a table which shows the operators under this section.

Operator
Meaning
Example
+
Addition
a+b;
-
Subtraction
a-b;
*
Multiplication
a*b;
/
Division
a/b;
%
Modulus
a % b;

     
Operation of modulus operator:- 4/2=2 (here quotient is taken)  but in modulus operator, 4%2=0 (here remainder is taken).





2. Relational operator:- These are the symbols used to show the relation between operands.

Operator
Meaning
Example
Less than
a < b;
<=
Less than or equal to
a <= b;
Greater than
a > b;
>=
Greater than or eual to
a >= b;
==
Checking Equal to
a == b;
!=
Checking Not equal to
a != b;
  




3.Logical operator:- These are the symbols used to evaluate the conditions and expressions.

Operator
Meaning
Example
&&
And
a=6 and  b=3;

(a<10 && b>1)
||
Or
a=12 ,b=3;

(a==12 || b==5)
!
Not
a=6 , b=3
 !(a == b)



4. Assignment operator:- 


Operator
Meaning
Example
=
Equals to
x=6



5.Compound Assignment Operator:-


Operator
Example
Equivalent

Assume  c = 10
+=
c + =10
c=20
c= c + 10 ;
==
c - =5
c=5
 c = c- 5 ;
*=
c * =2
c=20
 c = c* 2;
/=
c / = 5
c = 2
c = c/ 5 ;




6. Ternary operator:-  

This operator evaluates the expression or a condition .If condition is true then Value 1 is considered or else the Value 2 is considered.

  { Expression ? Value 1 : Value 2 ;}     

   x = (a = =10)  5 : 15 ;    .........where the value of  a=10
  
Since the given expression in the bracket  (a = =10) becomes true, the value 1 will be returned to the user. Hence, the value of x will be 5.

 x = (a = =10)  5 : 15 ;    .........where the value of  a=3

Since the given expression in the bracket  (a = =10) becomes false, the value 2 will be returned to the user. Hence, the value of x will be 15.



!!  HERE IS A LINK FOR A PROGRAM BASED ON  Ternary / conditional operator  !!





7. Bitwise operator:- 
                                          
Short form of binary language is bit ..... for example the language of 0's and 1's ..
When we have 8 bits we have 1 byte i.e., 1 byte = 8 bits.

Bitwise  operators are used to manipulate the data at bit level. These operators operate on each bit of the data.They are used for complimenting or shifting bits . They may not be applied on float or double types.


Operator
Meaning
~
One’s complement
>> 
Right shift
<< 
Left shift
&
Bitwise AND
|
Bitwise OR
^
Bitwise XOR(exclusive OR)



Working of the operators:-
These operators work on two integer type operands.They work on their operands bit by bit starting from LSB to MSB . Here is their example:-  

Most            - - - - - - -      Least
Significant +   - - - - - - -    Significant
Bit                                        Bit
(MSB)                                    (LSB)



The three most commonly used bitwise operators are :-

1.     Bitwise AND
2.     Bitwise OR
3.     Bitwise XOR
Use and their working:-  Lets take example of milk and tea considering operand1 and operand 2 to be milk and tea respectively.

operand 1= OP1 = milk
operand 2 = OP2 = tea

Operand 1
(milk )
(OP1)
Operand 2
(tea)
(OP2)
 OP1 & OP2
both OP1  (milk) and OP2 (tea) should be available
OP1
/OP2
Any one OP1 (milk) or OP2 (tea) should be available
OP1^
OP2
Highest number will be taken .  Equal inputs will give zero
0
0
(not available)
0
(Equal inputs will give zero)
0
1
0  (both not available)
1
1
1
0
0  (both not available)
1
1
1
1
1  (both available)
1
 (Equal inputs will give zero)



Programs:-

For many other Language Programs of microprocessor 8086, C++ , C , Data Structures visit our  BLOG and search for topic u want .....

No comments:

Post a Comment