Wednesday, February 3, 2016

C Operator Precedence and Associativity Table with Examples

Operator Precedence
It usually means, if an expression has multiple operators in it, which operator get the precedence over others. To understand what's meant by operator precedence, let's take an arithmetic expression as an example.

Consider, int a = 2 + 3 * 4. Value of 'a' would be 14, not 20. This is because operator '*' is executed before '+', like the BODMAS rule. So we can say that operator '*' has precedence over operator '+' and the expression would be grouped as int a = (2 + (3 * 4)).

Operator Associativity

Associativity specifies how the operators are grouped for evaluation. For example, consider the expression 2 + 3 + 4Note: In this case (+ operator), the result of different grouping doesn't matter. But that might not be the case with other operators.

can you tell how the expression will be grouped? ((2 + 3) + 4) or (2 + (3 + 4)). This is determined by the associativity of operators. From the above example we can understand that, associativity comes into picture only when expression has operators of same precedence.

Different Types of Operator Associativity

There are two types of associativity possible known as 'Right to Left' associativity and 'Left to Right' associativity. L->R (left to right) associativity means operator on the left hand side will get priority over right hand operator and R->L (right to left) associativity means right most operators will get more priority. To understand the associativity in detail, lets consider two example expressions.
  1. a = b = c : '=' has right to left associativity. So the expression will be grouped as (a = (b = c))
  2. a + b + c : '+' has left to right associativity. So the expression will be grouped as ((a + b) + c)


Tuesday, February 2, 2016

C Operators - Bitwise Operators and Conditional Operators

As the name suggests, bitwise operator operate at bit level. There are six bitwise operators and they are all binary operators, except '~'. These operators can be applied on operands of type intand char. Bitwise operators do not work on float or double. Different bitwise operators are explained in the table given below.
SymbolOperator
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~One's compliment/ Bitwise NOT
<<Left Shift Operator
>>Right Shift Operator

Bitwise AND Operator

Bitwise "AND" operator will take bit by bit from two operands and generate the AND output of those two bits. For example, (4&5) generates output as 4. Let's see how that Bitwise AND Operator works. "AND" operation is applied on binary representation of 4 (0000 0100) and 5 (0000 0101) as given below to produce the result 4 (0000 0100).
  0000 0100 (4)
  0000 0101 (5)
-------------
  0000 0100 (4)

Bitwise OR Operator

Bitwise OR operator will take bit by bit from two operands and generate the OR output of those two bits. For example, (5|6) will generate output: 7. That is, "OR" operation is applied on corresponding bits of binary representation of 5 (0000 0101) and 6 (0000 0110) to produce the result 7 (0000 0111) as given below.

Monday, February 1, 2016

Logical operator

There are three logical operators in C language, &&, ||, !
OperatorNameDescriptionExample
&&'Logical AND' Operator'AND' operator returns true if both the operands it operates on evaluates to true (non-zero), else return falsea && b returns true if both a and b are non-zero
||'Logical OR' Operator'OR' operator returns true if any of the operands it operates on evaluates to true (non-zero), else return falsea || b returns true if either a or b are non-zero
!'Logical NOT' Operator'NOT' operator is a unary operator (it operates only on one operand). It returns true if the operand is false and returns false if the operand is true!a returns true if a is false. Else returns false

Examples of Logical Operators

  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. int a = 5, b = 6;
  5. printf("(a == 5) && (b == 7) : %d\n", (a == 5) && (b == 7));
  6. printf("(a == 5) || (b == 7) : %d\n", (a == 5) || (b == 7));
  7. printf("!(a == 5) : %d", !(a == 5));
  8. return 0;
  9. }
  10.  
  11. Output:
  12. (a == 5) && (b == 7) : 0
  13. (a == 5) || (b == 7) : 1
  14. !(a == 5) : 0