1 Operators
1 Operators
OPERATORS
A symbol that denotes an operation that can be performed on some data.
• Arithmetic Binary
• Arithmetic Unary
• Relational
• Logical
• Bitwise
• Conditional
• Assignment
ARITHMETIC BINARY
Assuming int a=10,b=20;
ARITHMETIC UNARY
1) Sign Operator: +, -
Example: +5, -5
2) Increment and Decrement by 1
++, --
a)Pre-increment: y=++x; (x=x+1; and y=x;)
b)Post-increment: y=x++; (y=x; and x=x+1;)
RELATIONAL OPERATORS
1) Operators used to compare two operands (variables, constants or
expressions)
2) Characters can be compared as well
3) These operators are used extensively with flow control statements.
4) Example: a!=b, a<=b
RELATIONAL OPERATORS
Operator What it does Example
Operator Example
= a=b or b=a
+= a += b or a = a+b
-= a -=b or a = a-b
*= a *= b or a = a*b
/= a /= b or a = a/b
%= a %= b or a = a%b
LOGICAL OPERATORS
In the C programming language, Logical operators are mostly used for decision
making. A logical operator returns either 0 or 1 whether the condition is true or
false.
Example:
num=10
!(num==10) 0
!(num!=10) 1
!(num>5) 0
!(num<5) 1
Bitwise AND operator - &
Example:
#include <stdio.h>
int main()
{
printf("Output = %d\n",~35); printf("Output = %d\n",~-12);
return 0;
}
Output
Output = -36
Output = 11
ONE’S COMPLEMENT:
~ 1 -> 00000001
~1 ->1111111110 (1’s complement)
How to find the decimal equivalent of 11111110?
Since the sign bit is 1 the number is negative. To get the decimal value of
negative (-) number get the 2’s complement:
2’s complement = - (1’s complement + 1)
1’s complement of 11111110 -> 00000001 +1
---------------
00000010
00000010 decimal value -> 2. Hence the number is -2.
SHIFT OPERATORS: >> AND <<
Miscellaneous Operators
Operator Description Example
i==1?”Y”:”N”; If
Conditional condition is true then
?:
expression. value Y otherwise
value N.
THANK YOU