Operators play an important role in your program. Even if its a simple one.
The operators in C are classified as follows :
- Arithmetic Operators
- Bitwise Operators
- Logical Operators
- Relational Operators
- Assignment Operators
- Increment / Decrement Operators
- 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:
- 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.
- 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
Operator | Usage | Example |
---|---|---|
+ (addition) | var1 + var2 | 5 + 2 = 7 6.3 + 5.5 = 11.8 |
- (subtraction) | var1 - var2 | 13 - 6 = 7 20.6 - 14.7 = 5.9 |
* (multiplication) | var1 * var2 | 4 * 8 = 32 3.4 * 5.5 = 18.7 |
/ (division) | var1 / var2 | 8 / 2 = 4 47.6 / 8.5 = 5.6 |
% (modulo) | var1 % var2 | 10 % 4 = 2 |
Operator | Usage | Example |
---|---|---|
& (Bitwise AND) | var1 & var2 | 14 & 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) |
- 3. Logical Operators
- These operators deals with the logical state of the expressions.
Operator | Usage | Example |
---|---|---|
&& (Logical AND) | val1 && val2 | 12 && 15 = 1 6 && 0 = 0 |
|| (Logical OR) | val1 || val2 | 12 || 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.
Operator | Usage | Example |
---|---|---|
< (Less than) | expr1 < expr2 | 4 < 7 - true 6 < 3 - false |
<= (Less than or Equals) | expr1 <= expr2 | 4 <= 7 - true 6 <= 6 - true 8 <= 5 - false |
> (Greater than) | expr1 > expr2 | 14 > 6 - true 10 > 15 - false |
>= (Greateer than or equal to) | expr1 >= expr2 | 14 >=6 - true 15 >= 15 - true 10 >= 15 - false |
== (Equals) | expr1 == expr2 | 23 == 23 - true 13 == 15 - false |
!= (Not Equal to) | expr1 != expr2 | 13 != 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 :
Operator | Usage | Example | Equivalent |
---|---|---|---|
= (Simple Assignment) | var = expr | x = 5; | - |
+= (Addition Assignment) | var += expr | int x=3; x += 5; Ans: x = 8 | x = x + 5; x = 3 + 5; Ans: x = 8 |
-= (Subtraction Assignment) | var -= expr | int x = 8; x -= 6; Ans: x = 2 | x = x - 6; x = 8 - 6; Ans: x = 2 |
*= (Multiplication Assignment) | var *= expr | int x = 2; x *= 8; Ans: x = 16 | x = x
* 8; x = 2 * 8; Ans: x = 16 |
/= (Division Assignment) | var /= expr | int x = 16; x /= 4; Ans: x = 4 | x = x /
4; x = 16 / 4; Ans: x = 4 |
%= (Modulo Assignment) | var %= expr | int x = 37; x %= 7; Ans: x = 2 | x = x % 7; x = 37 % 7; Ans: x = 2 |
<<= (Left Shift Assignment) | var1 <<= var2 | int x = 4; x <<= 2; Ans: x = 16 | x =
x << 2; x = 4 << 2; Ans: x = 16 |
>>= (Right Shift Assignment) | var1 >>= var2 | int 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