Friday, November 25, 2011

Operators - A Brief Introduction

Operators play an important role in your program. Even if its a simple one.

The operators in C are classified as follows :
  1. Arithmetic Operators
  2. Bitwise Operators
  3. Logical Operators
  4. Relational Operators
  5. Assignment Operators
  6. Increment / Decrement Operators
  7. Conditional Operators
1. Arithmetic Operators :
These are the operators that work on the numbers. The arithmetic operators and their uses with examples are listed as below:

OperatorUsageExample
+ (addition)var1 + var25 + 2 = 7
6.3 + 5.5 = 11.8
- (subtraction)var1 - var213 - 6 = 7
20.6 - 14.7 = 5.9
* (multiplication)var1 * var24 * 8 = 32
3.4 * 5.5 = 18.7
/ (division)var1 / var28 / 2 = 4
47.6 / 8.5 = 5.6
% (modulo)var1 % var210 % 4 = 2

Unlike the other operators, the %(modulus) operator cannot be used for floating point numbers.

2. Bitwise Operators :
The Bitwise operators work on the most basic level. They work on the bits of the variables themselves. They only work for integers and characters. Not for floating point numbers.

OperatorUsageExample
& (Bitwise AND)var1 & var214 & 5 = 4.
14 : 0000 0000 0000 1110
05 : 0000 0000 0000 0101
Ans: 0000 0000 0000 0100 (04)
| (Bitwise OR)var1 | var2 14 | 5 = 15.
14 : 0000 0000 0000 1110
05 : 0000 0000 0000 0101
Ans : 0000 0000 0000 1111 (15)
^ (Bitwise XOR)var1 ^ var2 14 ^ 5 = 11.
14 : 0000 0000 0000 1110
05 : 0000 0000 0000 0101
Ans : 0000 0000 0000 1011 (11)
<< (Left Shift)var1 << var2 14 << 2 = 56
14: 0000 0000 0000 1110
Ans : 0000 0000 0011 1000 (56)
>> (Right Shift)var1 >> var2 53 >> 2 = 13
53: 0000 0000 0011 0101
Ans : 0000 0000 0000 1101 (13)

Here the left shifts value can be found as follows :
If x<<y , then the result would be x * 2y
Example : 14<<2 = 14 * 22 = 14 * 4 = 56

Similarly the right shifts value can be found as follows (only for positive values, for negative values, it has to be found manually):
If x>>y , then the result would be x / 2y
Example : 53>>2 = 14 / 22 = 53 / 4 = 13.25 = 13


3. Logical Operators
These operators deals with the logical state of the expressions.

OperatorUsageExample
&& (Logical AND)val1 && val212 && 15 = 1
6 && 0 = 0
|| (Logical OR)val1 || val212 || 15 = 1
6 || 0 = 1
0 || 0 = 0
! (Logical NOT)!val!12 = 0
!0 = 1
4. Relational Operators
The relational operators compare the expressions and return the boolean value.

OperatorUsageExample
< (Less than)expr1 < expr24 < 7 - true
6 < 3 - false
<= (Less than or Equals)expr1 <= expr24 <= 7 - true
6 <= 6 - true
8 <= 5 - false
> (Greater than)expr1 > expr214 > 6 - true
10 > 15 - false
>= (Greateer than or equal to)expr1 >= expr214 >=6 - true
15 >= 15 - true
10 >= 15 - false
== (Equals)expr1 == expr223 == 23 - true
13 == 15 - false
!= (Not Equal to)expr1 != expr213 != 15 - true
23 != 23 - false
5. Assignment Operators
The assignment operators are used to assign the resulting value of an expression to a variable. The following table gives the various assignment operators in C :
OperatorUsageExampleEquivalent
= (Simple Assignment)var = exprx = 5;-
+= (Addition Assignment)var += exprint x=3;
x += 5;
Ans: x = 8
x = x + 5;
x = 3 + 5;
Ans: x = 8
-= (Subtraction Assignment)var -= exprint x = 8;
x -= 6;
Ans: x = 2
x = x - 6;
x = 8 - 6;
Ans: x = 2
*= (Multiplication Assignment)var *= exprint x = 2;
x *= 8;
Ans: x = 16
x = x * 8;
x = 2 * 8;
Ans: x = 16
/= (Division Assignment)var /= exprint x = 16;
x /= 4;
Ans: x = 4
x = x / 4;
x = 16 / 4;
Ans: x = 4
%= (Modulo Assignment)var %= exprint x = 37;
x %= 7;
Ans: x = 2
x = x % 7;
x = 37 % 7;
Ans: x = 2
<<= (Left Shift Assignment)var1 <<= var2int x = 4;
x <<= 2;
Ans: x = 16
x = x << 2;
x = 4 << 2;
Ans: x = 16
>>= (Right Shift Assignment)var1 >>= var2int x = 66;
x >>= 3;
Ans: x = 8
x = x >> 3;
x = 66 >> 3;
Ans: x = 8
6. Increment/Decrement Operators
These are unary operators i.e. they operate on only single variable unlike the previously seen operators. They are used for integers and also on characters but cannot be used for real numbers. They are used to increment or decrement the variable's value by 1.
The postfix increment first uses the value of the variable and then increments the variable by 1.
Similarly the postfix increment uses the value of the variable and then decrements the variable by 1.
Example :
i=5;
printf("%d",i++);
printf("\n%d",i--);
printf("\n%d",i);

Here, initially, the value of i is 5. In the second line,the value of i i.e. 5 is printed on screen and then incremented to 6. In the third line, the value of i i.e. 6 is printed on the screen and then decremented to 5. Again the value of i i.e. 5 is printed on the screen. Thus the output is obviously as follows:

5
6
5

But, in the case of prefix increment/decrement operators, the value of the variable is first incremented/decremented and then is used in the statement.
Example :
i=5;
printf("%d",++i);
printf("\n%d",--i);
printf("\n%d",i);

Here, as usual the initial value of i is 5. In the second line the value of i is incremented to 6 and is then printed on the screen.In the third line the value of i is decremented to 5 and is then printed on the screen. Then again the value of i i.e. 5 is printed on the screen.
6
5
5

7. Conditional Operator :
C supports a conditional operator (ternary). It is of form


expr1 ? expr2 : expr3

Here, the expr1 is evaluated at first. If it is true, then expr2 is evaluated. Or else if expr1 is false, then expr3 is evaluated.
Example:
i = 5;
(i == 5) ? printf("Equal") : printf("Not equal");
i = 7;
(i == 5) ? printf("\nEqual") : printf("\nNot equal");   

Here, in this example, at first, the value of i is 5. In the next line, the expression i==5 is evaluated and true is returned. So, "Equal" is printed on the screen. In the third line, the value of i is changed to 7. In the next line expression i==5 is evaluated and false is returned. Hence, "Not equal" is displayed on the screen.
Output:
Equal
Not equal
8. Special Operators
C provides us with the following special operators :
  • , (Comma) operator
  • sizeof operator
  • & (address of) operator
  • * (value of) operator
  • . and -> (Member selection operators)

Both '&' and '*' will be discussed briefly in pointers. And the Member selection operators will be discussed while discussing about structures.

So, as for now, we will see about ',' and 'sizeof' operators.

, (Comma)
The comma operator links the related expressions together. And these expressions are evaluated from left to right and the value of the right most expression is the value of the entire combination of expressions.
Example:


result = (a=3,b=6,a*b);

Here at first, 'a' is assigned the value 3, b the value 6 and at last the right most expression a*b is evaluated ie 3*6 = 18 is assigned to the variable 'result'.

sizeof
The sizeof operator is used to return the number of bytes occupied by a variable or a constant. It returns the value at the compile time itself.
Example
x = sizeof(45);

Since every integer occupies two bytes as far as 16 bit compiler is concerned, 2 will be assigned to x.

No comments:

Post a Comment