Arithmetic Operators:
There are five basic arithmetic operators in ‘C’. They are:
Relational Operators:
Relational operators check relationship between two operands. If the relation is true, it returns value 1 and if the relation is false, it returns value 0.
For example: a>b
Here,, > is a relational operator. If a is greater than b, returns 1, if not then it returns 0.
Relational operators are used in decision making in conditional statements and loops.
Operator
|
Meaning of operator
|
Example
|
==
|
Equal to
|
5==3 returns false (0)
|
>
|
Greater than
|
5>3 returns true (1)
|
<
|
Less than
|
5<3 returns false (0)
|
!=
|
Not equal to
|
5!=3 returns true (1)
|
>=
|
Greater than or equal to
|
5>=3 returns true (1)
|
<=
|
Less than or equal to
|
5<=3 returns false (0)
|
Logical Operators:
Operator
|
Meaning of operator
|
example
|
&&
|
Logical AND
|
If c=5 and d=2 then ((c==5) && (d>5)) returns false.
|
||
|
Logical OR
|
If c=5 and d=2 then ((c==5) || (d>5)) returns true.
|
!
|
Logical NOT
|
If c=5 then !(c=5) returns false.
|
Increment and Decrement operators:
In C, ++ and – are called increment and decrement operators respectively. Both of these operators are unary operators, i.e., used on a single operand. ‘++’ adds ‘1’ to operand and ‘- -‘from the operand respectively.
For example
Let a=5
a++; //a becomes 6
a--; //a becomes 4
++a; //a becomes 6
--a; //a becomes 4
Difference between ++ and - - operator as postfix and prefix
When ++ is used as prefix (like: ++var), ++var will increment the value of var and then returns it.
But, if ++ used as postfix (like: var++), operator will return the value of the operand first and then only increment it.
For example:
#include <stdio.h>
Int main()
{
int c=2,d=2;
printf(“%d\n”, c++); // this statement displays 2 then only c incremented by 1 to 3.
printf(“%d”, ++d); // this statement displays 1 to 3 then, only c is displayed.
Return 0;
}
Conditional Operator:
Conditional operator (Ternary operator) takes three operands and consists of two sysmbols ? and :
Conditional operators are used for decision making in ‘C’.
For example:
d=(c>0)?10: -10;
If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.
Assignment operators:
The most common assignment operator is ‘=’. This operator assigns the value from the right side to the left side.
For example:
Age=5; // 5 is assigned to age
Old age=age; //value of age is assigned to old age
5=age; //Error! 5 is a constant. Constant cannot be changed
Two operators can be used in conjunctions as like”+ =”
Operator
|
Example
|
Same as
|
=
|
a=b
|
a=b
|
+=
|
a+=b
|
a=a+b
|
-=
|
a-=b
|
a=a-b
|
*=
|
a*=b
|
a=a*b
|
/=
|
a/=b
|
a=a/b
|
%=
|
a%=b
|
a=a%b
|
Bitwise Operators:
A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming.
Operator
|
Meaning of operators
|
&
|
Bitwise AND
|
|
|
Bitwise OR
|
^
|
Bitwise exclusive OR
|
~
|
Bitwise complement
|
<<
|
Shift left
|
>>
|
Shift right
|
Example:
C program for bitwise operator is:
#include<stdio.h>
main()
{
int x=7,y=9,and,or,xor,right_shift,left_shift;
and=x&y;
or=x|y;
xor=x^y;
left_shift=x<<1;
right_shift=y>>1;
printf(%d AND %d=%d\n",x,y,and);
printf(%d OR %d=%d\n",x,y,or);
printf(%d XOR %d=%d\n",x,y,xor);
printf(Left shifting %d by 1 bit=%d\n",x,left_shift);
printf(Right shifting %d by 1 bit=%d\n",y,right_shift);
return 0;
}
OUTPUT:
7 AND 9 =1
7 OR 9 =15
7 XOR 9 =14
Left shifting 7 by 1 bit =14
Right shifting 9 by 1 bit = 4