0% found this document useful (0 votes)
16 views

Operators

Uploaded by

AjithaTeja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Operators

Uploaded by

AjithaTeja
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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;

sizeof() 1 sizeof(a); Returns the number of bytes occupied


by the operand
& 1 p=&a; Returns the starting location of the
operand a and stores in p.
* 1 *p; Returns the contents referred to by the
pointer p
. 2 x.attr=3; It is used to access the attr a member of
structure named x
-> 2 p->attr=3; It is used to access the attr a member of
structure referred to by p
[] 2 Array[2]=3; To access the element stored in Array
with index value of 2.
() 1 Function call

, d=(a++,b--,c) A gets incremented, b gets decremented


but only c will be assigned to d.
Precedence and Associativity
• If in an expression, two or more operators are present.
• Precedence dictates the order in which operators are to be
implemented.
• If we have multiple operators with the same precedence, they shall
be implemented according to their associativity.
1. Parentheses
• This has the highest priority over all.
• Parentheses can be used to alter the order in which the operators are
implemented.
2. Access Operators
• Array Access, Member access of structures
• If there are multiple Access operators:
• They will be implemented from left to right.
3. Unary Operators
• Unary Prefix operators have higher precedence over postfix operators.
• In Prefix Operators, the order of operators is:
• Increment/Decrement
• Plus/Minus
• Logical Negation
• Bitwise complement
• Address of
• Indirection
• Typecast
• The Associativity of Prefix operators is Right to Left
• The Associativity of Postfix operators is left to Right
4. Multiplication, Division & Modular
Division
• All the operators have the same precedence.
• The Associativity of operators is left to right.
5. Addition, Subtraction
• The Associativity is left to right
6. Bitwise Shift Operators
• Right-shift and left-shift operators have the same precedence
• The operators are evaluated from left to right
7. Relational Operators
• The relational operators have the following order of precedence.
1. Less than/Greater Than
2. Less Than or Equal to/ Greater than or Equal to
3. Equal to or Not Equal to

• The associativity of operators is left to right.


8. Bitwise Operators (Other than
shift)
• The precedence amongst bitwise operators (other than shift
operators) is:
1. Bitwise And
2. Bitwise Or
3. Bitwise Ex-or
• The associativity of the bitwise operators is left to right.
9. Logical Operators
• The precedence of binary logical operators is:
1. Logical And
2. Logical Or
• The associativity of Logical operators is also from left to right.
10. Ternary Operator
• Also Called a conditional operator.
• The associativity of the conditional operators is right to left
11. Assignment Operators
• All assignment operators have the same precedence.
• In an expression, The assignment operators are generally
implemented with the least precedence.
• The associativity of assignment operators is right to left.
12. Comma Operator
• The operator with the lowest precedence in C programming.
• The associativity is left to right

You might also like