OPERATORS IN C
Operators in COperators are the foundation of programming language. C language provide different kind of operators. Different types of operators in C are : 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and decrement operators 6. Conditional operators 7. Bitwise operators 8. Special operators | ||
|
Note : C language has no operator for exponentiation. The function pow(x,y) which exists in header file math.h of standard library and returns Xy Following are some examples of arithmetic operators : x+y, x-y, x*y, x/y, x%y, x*y Here x and y are operands that can take any value but % operator cannot be used on floating point data type. |
Arithmetic ExpressionsAn expression consisting of numerical values(either any number, variable or even some function call) joined together by arithmetic operators is known as an arithmetic expression. For example , consider the following expression : (x-y)*(x+y)/5 Here x,y and 5 are operands and the symbols -,*,+,/ are operators. The precedence of operators for the expression evaluation has been given by using parenthesis which will over rule the operators precedence. If x=25 and y=15,then the value of this expression will be 80. |
EXAMPLE:Consider the following expression : 3*((a%4)*(5+(b-2)/(c+3))) Where a,b,c are integer variables and if a,b ,c have values 9 ,14 ,16 respectively then above expression would be evaluated as 3 * ((9%4) * (5 + (14 – 2) / (6 +3 ))) = 3*(1*(5+(12/9))) |
Arithmetic Operators Precedence* / % + - In C, the arithmetic operators have the priority as shown below: First priority >Second priority >Third priority The sequence of operations in evaluating an arithmetic expression is also known as hierarchy of operations. This is necessary to avoid any doubt while evaluating an expression. The following precedence rules are followed in expression evaluation : |
Arithmetic Operators Precedence
(iii)Operators in the same expression with the same priority evaluated from left to right. |
For example : consider the following expression for checking the operators precedence.15 * 7 / ( 2 – 3 * 5 / 7 + 4 ) – 7 * 9 % 4 = 15*7/(2–15/7+4)–7*9%4 |
Increment and Decrement OperatorC language has two useful operators called increment(++) and decrement (--) that operate on integer data only. The increment (++) operator increments the operand by 1, while the decrement operator (--) decrements the operand by 1. There are two type of increment (++) operator : pre and post ,Similarly There are two type of decrement (--) operator : pre and post, for example : int a ,b; a = 10; b = a++ ; printf(“ %d %d “, a, b); OUTPUT: 11 10 . First a is assigned to band then a is incremented by 1 i.e.,post-increment takes place |
Increment and Decrement Operatorfirst a is incremented by 1 and then If we have: int a, b; OUTPUT : 21 now, consider the example for (--) operator : int a,b; a=10; OUTPUT : 9 10. first a is assigned to b then a is decremented by 1. i.e.,post decrement takes place |
Decrement OperatorIf we have : I = 20; OUTPUT : 19 19. first i is decremented by 1 and then assignment take place i.e., pre-decrement of i. Note : on some compilers a space is required on both sides of ++i or i++ , i-- or --i |
Relational OperatorsThese are used to compare two variables or constants and return true or false . C has the following relational operators : !=. Not Equals < Less than > Greater than <= Less than or equal to >= Greater than or equal to |
Logical Operators! , && , || In C, we can have simple conditions (single) or compound conditions(two or more). The logical operators are used to combine conditions. The notations for these operators is given below : Operator NOTNotation in C OR || The notation for the operator OR is given by two broken lines. These follow the same precedence as in other language. NOT(!) is evaluated before AND(&&) which is evaluatedbefore OR(||). Parenthesis( ) cab be used to change this order. |
Logical Operators
&& : returns true when all the condition under the consideration are true and returns false when anyone or more than one condition is false.
|| : returns true when one or more than one condition under the consideration are true and returns false when all the conditions are false
! : NOT operator is used to complement the condition under the consideration
Returns true when condition is false and returns false when condition is true
Precedence of Relational Operators and Logical OperatorsEach operator in C has a precedence of its own. It helps in evaluation of an expression. Higher the precedence of the operator, earlier it operates. The operators having same precedence are evaluated either from left to right or from right to left, depending on the level, known as the associativity of the operator. ! , < , <= , > , >=, ==, !=, ==, !=, &&, || |
The Conditional OperatorThis operator ? And : together forms a ternary operator called as the conditional operator. Syntax : (test-expression) ? T-expr : F-expr ; Let us see example program : |
The conditional operator#include<stdio.h> main() clrscr(); scanf(“%d”,&n); (n%2==0)? Printf(“n is even”):printf(“n is not even”); getch(); |
Bitwise Operators These are used to perform bitwise operations such as testing the bits, shifting the bits to left to right, one’s complement of bits etc. these operations can be applied only on int and char data types but not on float and double data types. Various bitwise operators in C language are : ~ Bitwise (1’s) complement) << >> & ^ | shift left |
Special Operator |
The Comma Operator The comma operator (,) has the lowest precedence. The comma operator is mainly used in for statement. For example : int i,j; printf(“%d\n”, i+j ) ; The initial value of i is 1 and that of j is 400 and every time the value of i is incremented by 1 and that of j is divided by 2 after execution of the body of the for loop . The distinct expression on either side of the comma operator are evaluated from left to right. The associativity of comma operator is from left to right . |
The sizeof Operator It is a unary operator which provides the size , in bytes, of the given operand. The syntax of sizeof operator is : sizeof(operand) Here the operand is a built in or user defined data type or variable. The sizeof operator always precedes its operand. For example, sizeof (float) returns the value 4 . The sizeof operator mainly used in dynamic memory allocation for calculating the number of bytes used by some user defined data type. |
Precedence of operators among themselves and across all the sets of operators.
The TURBO C operators are divided into the following 16 categories : these are ordered from the highest precedence to the lowest precedence. The operation within each category have equal precedence.
Category | Operator | What it does ? |
1. Highest precedence | () | Function call |
[] | Array subscript | |
-> | C indirect component selector | |
2.Unary | ! | NOT |
~ | Bitwise(1’s) component | |
+ | Unary plus | |
- | Unary minus | |
3.Member acces | .* | Dereference |
->* | Dereference | |
4.Multiplication | * | Multiply |
/ | Divide | |
% | Remainder (Modulus) | |
Precedence of operators among themselves and across all the sets of operators.
Category
5.Additive
6.Shift
7.Relational
8.Equality
9.Bitwise AND
10.Bitwise XOR
11.Bitwise OR
+ Binary plus
- Binary minus
<< Shift left
>> Shift right
< Less than
<= Less than or equal to
> Greater than
>= Greater than equal to
== Equal to
!= Not equal to
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
The Associativity of Operators In C,the operators having the equal precedence are evaluated either from left to right or from right to left, depending on the level. It is known as associativity property of the operator. |
Category Operator Associativity |
1.Highest precedence ( ) Left to Right |
Associativity of the Operator
Category | Operator | Associativity |
3.Member access | .* | Left to Right |
->* | ||
4.Multiplication | * | Left to right |
/ | ||
% | ||
5.Additive | + | Left to Right |
- | ||
6.Shift | << | Left to Right |
>> | ||
7.Relational | < | Left to Right |
<= | ||
> | ||
>= |
Operator Associativity
== Left to Right
!= Left to right
^ Left to Right
| Left to Right
&& Left to Right
|| Left to Right
?: Right to Left
= Right to Left
*= Right to left
/= Right to left
%= Right to left
+= Right to left
-= Right to left
Comments
Post a Comment