Saturday, 11 October 2014

C Operator Types

What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and 5 are called operands and + is called operator. C language supports following type of operators.
  • Arithmetic Operators
  • Logical (or Relational) Operators
  • Bitwise Operators
  • Assignment Operators
  • Various Other Operators
Let’s have a look on all operators one by one.


Arithmetic Operators:
There are following arithmetic operators supported by C language:
Assume variable A holds 10 and variable B holds 20 then:
Logical (or Relational) Operators:
There are following logical operators supported by C language
Assume variable A holds 10 and variable B holds 20 then:
Bitwise Operators:
Bitwise operator works on bits and performs bit by bit operation. Assume if A = 60; and B = 13; Now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
There are following Bitwise operators supported by C language
Assignment Operators:
There are following assignment operators supported by C language:
Short Notes on L-VALUE and R-VALUE:
x = 1; takes the value on the right (e.g. 1) and puts it in the memory referenced by x. Here x and 1 are known as L-VALUES and R-VALUES respectively L-values can be on either side of the assignment operator whereas R-values only appear on the right.
So x is an L-value because it can appear on the left as we’ve just seen, or on the right like this: y = x; However, constants like 1 are R-values because 1 could appear on the right, but 1 = x; is invalid.

Various Other Operators:
There are few other operators supported by C Language.
Operators Categories:
All the operators we have discussed above can be categorised into following categories:
  • Postfix operators, which follow a single operand.
  • Unary prefix operators, which precede a single operand.
  • Binary operators, which take two operands and perform a variety of arithmetic and logical operations.
  • The conditional operator (a ternary operator), which takes three operands and evaluates either the second or third expression, depending on the evaluation of the first expression.
  • Assignment operators, which assign a value to a variable.
  • The comma operator, which guarantees left-to-right evaluation of comma-separated expressions.

Precedence of C Operators: 
Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:
For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher precedenace than + so it first get multiplied with 3*2 and then adds into 7. Here operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedenace operators will be evaluated first.

No comments:

Post a Comment