MAP

OPERATORS IN C


Operator is symbol, which represents, a particular operation that can be performed on some data. The data itself (which can be either a variable or a constant) is called the “operand”. The operator thus operates on operand. They are classified as
(1)        Arithmetic                   (2)        relational                     (3)        logical
(4)        Assignment                 (5)        increment/decrement
(6)        Conditional                 (7)        bitwise                         (8)        special

Arithmetic operators: -

There are five arithmetic operators in “C”. They are:

            OPERATOR
PURPOSE
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Remainder after integer division


Examples (Suppose A=10 and B=3)

Expression
Values
A+b
13
a-b
7
A*b
30
A/b
3
A%b
1

Relational Operators: -

There are four relational operators in “C”. They are:
            <                      less than
            <=                    less than or equal to
            >                      greater than
            >=                    greater than or equal to
The associatively of these operators is left-to-right. Closely associated with the relational operator are the following two equality operators:
            = =                   equal to
! =                    not equal to

Suppose that i, j and k integer variable whose values are 1,2 and 3 respectively, Seven logical expressions involving these variables are shown below. :

Expression
Interpretation
Value
i<j
True
1
(i+j)>=k
True
1
(j+k)>(I+5)
False
0
K!=3
False
0
J = = 2
True
1


  Logical Operators:

       ‘C’ provides three logical operations. They are:

Operator                                              Meaning
   &&                                                   AND
    ||                                                       OR
     !                                                      NOT

These operators are referred to as logical AND logical OR and logical NOT respectively.
The following table shows the result of the combined expressions depending on the value of the expression:

Operands
Results
Exp1
Exp2
Exp1 && exp2
Exp1 || exp2
0
0
0
0
0
Non-zero
0
1
Non-zero
0
0
1
Non-zero
Non-zero
1
1

The associatively us left-to-right.

Assignment Operators:

Assignment operators are use to form assignment expressions, which assign the value of an expression to an identifier. The most commonly used assignment operator is =. Assignment expression that make use of this operator are written in the form


Identifier = Expression
a= a+1                                                 a+=1
a=a-1                                                   a-=1
a=a*( c + d)                                         a*=(c + d)
a=a/(n-1)                                              a/=(n-1)
a=a%10                                               a%=10

 

Increment and Decrement Operators:

 “C” offers two special operator ++ and – called increment and decrement operators, respectively. There are “unary” operators since they operator on only one operand. The operand has to be a variable and not a constant. Thus the expression a++ is valid as 6++ is invalid. The use of these operates resulting incrementing or decrementing value of the variable is 1. So the expression a++ increment the value of a by 1 and the expression a- - decrements a by 1. These operators can be used either before or after their operand. (i.e. in either “prefix” or “postfix” position) so we can have a++(postfix) and ++a (prefix). Prefix and postfix have some effect if they are used in an isolated statement. For example, the effect of the following statements would be same:
a++;        ++a;
However prefix and postfix operators have different effects when used in association with some other operator in “c” statement. For example, if we assume the value of a variable to be 5 and then assign that new value to b. The effect is exactly same. If the following two statements have been executed :
b = a++; b=++a;
On the other had, execution of the statement b = a++; will be first set the value of b to 5 and then increases the value of a to 6. The effect is now same as if the following statement had been executed :
b = a;   b= a+1;
 [ Note : First precedence : R->L associatively)

Conditional operators:

Simple conditional operations can be carried out with the conditional operator (?:) An expression that makes the use of the conditional operator is called a conditional expression. Conditional operators are also known as turnery operators.
Syntax:
(condition) ?  true : false
Exp1          ?  Exp2 : Exp3
Here if condition is evaluated as true then value of exp2 will be return and if condition is evaluated as false then exp3 will be return.
For example
T=(I<0) ? 0 : 100
If I variable value is less then 0 then condition is evaluated as true and T will be assigned with 0 but if I variable value is greater then 0 then condition is evaluated as false and 100 will be assign to variable T.  For example
Printf(“%d”, (I<0) ? 0:100);

 

Sizeof Operator:

The size of a compile time operator and when used with an operand, it returns the number of bytes the operand occupies. The operand may be variable, a constant or a data type qualifies.
            M = sizeof(sum);
             N= sizeof(long int);
             K= sizeof(253L);
The size of operator is normally used to determine the length of array and structures, when their sizes are not known to the programmer. It is used to allocate memory space dynamically to variable during execution of a program.

Bitwise operators:

Some applications require the manipulation of individual bits within a word of memory. Assembly language or machine language is normally required for operations of this type. However C contains several special operators that allow such bitwise operations to be carried out easily and efficient. These bitwise operators can be divided into three general categories: the one’s complement operator, the logical bitwise operators, and the shift operators. “C” also contains several operators that combine bitwise operations with ordinary assignment.

The One’s Complement operator:

The one’s complement operator (~) is a unary operator that causes the bits of its operand to be inverted (i.e. reversed) so that is become so a so become is. This operator always precedes its operand. The operand must be an integer type quantity (including integer, long, short, unsigned, or char).(Linearly the operand will be an unsigned octal or an unsigned hexadecimal quantity, though this is not a firm requirement.
Suppose int a=16 with 2 byte=16 bit pattern
 it will be à  00000000 00010000
b=~a
the b will have 16 bit pattern like à 11111111 11101111

Logical bit wise operators :

There are three logical bitwise operators : bitwise and ($), bitwise exclusive or(^) and bitwise or(!). Each of these operators requires two integer type operands. The operations are carried out independently on each pair of corresponding bits within the two operands will be. Thus the least significant bits( i.e. the rightmost bits) within the two operands will be compared then the least significant bits. And so on. Until these comparisons are :

B1
B2
B1&b2
B1^b2
B1 | b2
1
1
1
0
1
1
0
0
1
1
0
1
0
1
1
0
0
0
0
0
The associatively for each bitwise operator is left-to-right.

Shift operators:

The two bitwise shift operator is shift left (<<) and shift right(>>). Each operator requires two operands. The first is an integer-type operand that represents the bit pattern to be shifted. The second is an unsigned integer that indicates the number of displacements (i.e. whether the bits in the first operand will be shifted by 1 bit position, 2 bit position, 3 bit positions and so on). This value cannot exceed the number of bits associated with the word size of the first operand.
The left-shift operator causes the left by the bits in the first operand to be shifted to the left by the number of position indicated by the second operand. The leftmost bit positions that become vacant will be tilled with us.
A = 01101101 10110111
A<<6 = 0110 1100 0000 = 0x5dc0
 All the bits originally assigned to a are shifted to the left six places, as the arrows indicate. The leftmost 6 bits (originally 011011) are lost. The rightmost 6 bits positions are filled with 00 0000.
The right shift operator causes all the bits in the first operand to be shifted to the right by the number of positions indicated by the second operand. The rightmost bits(i.e. the underflow bits) in the original bit pattern will be lost. If the bit pattern being shifted represents an unsigned integer, then the leftmost bit positions that become vacant will be filled with 0s. Hence, the behavior of the right-shift. When the first operand is an unsigned integer.
A= 0110 1101 1011 0111
A>>6 = 000 0001 1011 0110 = 061bc
We see that all the bits originally assigned to a are shifted to the right six places, as the arrows indicate. The rightmost 6 bits ( originally 11 011) are lost. The leftmost 6 bits positions are filled with 00 0000.

Comma Operator:

The comma operators are use primarily in conjunction with the for statement. This operator permits two different expressions to upper in situation where only one expression would ordinarily be used. For example, it is possible to write
For (expression la, expression lb; expression2; expression3) Statement
Where expression la and lb are the two expression, separated by the comma operator, where only one expression would normally appear. These two expression would typically initialize two separate indices that would be used simultaneously within the for loop.
Similarly, a for statement might make use of the comma operator in the following manner.
For (expression 1a; expression2;expression3a, expression3b) statement;
Here expression 3a and expression 3b separated by the comma operator, appear in place of the usual single expression. In this application the two separate expressions would typically be used to alter the two different indices used simultaneously within the loop. For example, one index might count forward while the order counts backward. In for loops.
            For(n=1,m=10; n<=m; m++, n++)

 

 

 

 

















 

Precedence and Associativity Table

Precedence is a priority of the operator for evaluation or execution when there are more then 1 operator in a single expression or statement. If there are operators of same precedence then the expression will evaluated either from LEFT-TO-RIGHT or RIGHT-TO-LEFT. This is called an associativity of an operator.


Description
Operator
Associativity
Precedence
Function expression
( )
L TO R

1
Array expression
[ ]
L TO R
Structure operator
->
L TO R
Structure operator
.
R TO L
Unary minus
-
R TO L




2
Increment/Decrement
++ --
R TO L
One’s complement

R TO L
Negation
!
R TO L
Address of
&
R TO L
Value of address
*
R TO L
Type cast
(type)
R TO L
Size in bytes
Size of
R TO L
Multiplication
*
L TO R

3
Division
/
L TO R
Modulus
%
L TO R
Addition
+
L TO R
4
Subtraction
-
L TO R
Left Shift
<< 
L TO R
5
Right Shift
>> 
L TO R
Less than
< 
L TO R


6
Less than equal to
<=
L TO R
Greater than
> 
L TO R
Greater than or equal to
>=
L TO R
Equal to
= =
L TO R
7
Not equal to
!=
L TO R
Bitwise AND
&
L TO R
8
Bitwise exclusive OR
^
L TO R
9
Bitwise inclusive OR
|
L TO R
10
Logical AND
&&
L TO R
11
Logical OR
||
L TO R
12
Conditional
? :
R TO L
13
Assignment
= *,= ,/=, %=
+=,-=,^=,|=,&=
<<=,>>=
R TO L
14


Comma
,
R TO L
15

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More