Operators
Operators
Operators
• The operators in C are the special symbols we use to perform various
functions.
• Operators perform operations on operands to produce a result.
• Operands are those data items on which we apply the operators.
• Examples: +, , *, /, % for arithmetic, =, ==, !=, <, > for comparison, &&,
|| for logical, etc.
Classification
• Two ways to classify
1. Based on the number of operands
2. Based on the functionality
Classification of Operators
(Operands)
1. Unary
• Operators that need only one operand
2. Binary
• Operators that need two operands
3. Ternary
• Operators that need three operands
Classification of Operators
(Functionality)
• Arithmetic
• Relational
• Logical
• Bitwise
• Assignment
• Miscellaneous
Arithmetic
Operation Operator No.of Operands Expression
Addition + 2 c=a+b;
Subtraction - 2 c=a-b;
Multiplication * 2 c=a*b;
Division / 2 c=a/b;
Modular Division
(For Remainder) % 2 c=a%b;
Increment a++;
(Prefix or Postfix) ++ 1 ++a;
Decrement a- -;
(Prefix or Postfix) -- 1
- -a;
Unary Plus
(Sign Retention) + 1 b=+a;
Unary Minus
(Sign Reversal) - 1 b=-a;
Relational
These are used to check the relation between two operands. The result is
always a Boolean value.
Operator No.of Expression Result of operation
Operands
== 2 a==b; True. If both operands are equal.
< 2 a<b; True if a is less than b
> 2 a>b; True if a is greater than b
<= 2 a<=b; True if a is less than or equal to b
>= 2 a>=b; True if a is greater than or equal to b
!= 2 a!=b; True. If both are not equal
Logical
• Used to combine Boolean expressions.
• The resultOperator
Operation is also aNo.of
Boolean expression.
Expression Result of operation
Operands
Logical And && 2 a&&b; True. If both operands are True.
Logical Or || 2 a||b; True if a is less than b
Logical Not ! 1 !a; True if a is False and Vice versa
Bitwise Operators
• These operators are used to perform bit-by-bit operations.
Operation Operator No. of Operands Result of the Expression
Bitwise
AND
& 2 Returns 1 for every position across the length if the corresponding bits of
both operands are 1
Bitwise OR | 2 Returns 1 for every position across the length if the corresponding bits of
either or both operands are 1
Bitwise
EXOR
^ 2 Returns 1 for every position across the length if the corresponding bits of
either operand are 1. However, it Returns 0 if both are 1.
One’s
Compliment
~ 1 Returns the 1’s complement of the operand.
Right Shift >> 2 Shifts the bits in the first operand to the right by the positions specified by
the second operand.
Left Shift << 2 Shifts the bits in the first operand to the Left by the positions specified by
the second operand.
#include <stdio.h>
int main() {
int a=13, b=15;
int c=a&b; int d=a|b; int e=a^b; int f=~a;
int g=a>>2; int h=b<<2;
printf("Bit W And %d\n",c);
printf("Bit W Or %d\n",d);
printf("Bit W XOR %d\n",e);
printf("Ones of %d is %d",a,f);
printf("%d R shifted by 2 is %d ",a,g);
printf("%d L shifted by 2 is %d ",b,h);
return 0;
}
Assignment Operator
• To assign values to variables
• The left operand is always an identifier.
• All Assignment operators are binary operators.
Operator Expression Result
= a=3; Assigns 3 to a
+= a+=3; Adds a to 3 and stores in a.
-= a-=3; Subtracts 3 from a and stores in a
*= a*=3; Multiplies a with 3 and stores in a
/= a/=3; Divides a with 3 and stores in a
%= a%=3; Stores the remainder when a divided by 3 and
stores in a
<<= a<<=3; Left shifts a by 3 bits and stores in a
>>= a>>=3; Right shifts a by 3 bits and store in a
&= a&=3; performs Bitwise and with 3 and stores in a
|= a|=3; performs Bitwise Or with 3 and stores in a
^= a^=3; performs Bitwise Ex-Or with 3 and stores in a
Miscellaneous
Operator No. of Operands Expression Result
?: 3 a>b?0.8*a+0.2*b:0.2*a+0.8*b;