C - Operators and Expression
C supports various operators that can perform different operations. An operator is a symbol that is used to perform the mathematical and logical operations. By using an operator, we can link the numbers, variables and constants to form an expression. The following terms are used in c programming:
- Operator: Operators are those which operate on one or more operands. Example: +, -, /, *, < ..etc.
- Operand: data item on which operators perform the operations.
- Operation: action which was carried out on the operands by the operator.
- Expression: simply the statement that contains any string of operators, variables and numbers.
|a+d-e||a, d, & e||+ -||Addition & Subtraction|
|k%4 +h/b||k, 4, h, and b||% + /||Division, Modulus & Addition|
|a>b||a, b||>||Comparison to check a Greater than condition|
|a<b && x<z||a, b, x, z||< &&||Comparison to check multiple conditions|
|a=b||a, b||=||value of b is assigned to a|
|x=(a>b)?10:5||x,a,b,10,5||= > ?:||if a is greater than b, then 10 is stored in x. Otherwise, 5 is stored in x|
Type of operators
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Conditional Operator
- Comma Operator
In next pages, we will discuss each operator in detail.
Let us consider an expression.
This above expression can be evaluated in two different ways.
- To perform addition and then multiplication,we will get 28 as a result.
- To perform multiplication and then addition, now we will get 22 as result.
Finally, two kinds of result came from those evaluation. Now we cannot say that which one is correct. We are in confusion stage. In order to resolve this confusion, we can assign different priory for different operators. This is known as Operator Precedence. Operator precedence defines that some operators are having highest priory than other and that highest priority operators will be evaluated first . Therefore the correct result of an above expression is 22.
The associativity defines the operator direction in which the expression is to be evaluated. That means, the expressions are evaluated from left to right or right to left. This is also allow us to group the operators by using parentheses.
The following table shows the operators precedence from highest priority at the top to lowest priority at the bottom.
|( ) [ ] -> .||Parentheses, square brackets, arrow and dot||left to right||for expression grouping|
|! ++ -- - + (cast) * & sizeof||Logic NOT, increment, decrement, minus, plus,type cast, pointer, address and sizeof||right to left||all unary|
|* / %||Multiplication Division and Modulus||left to right||binary|
|+ -||Addition and Subtraction||left to right||binary|
|<< >>||shift||left to right||binary|
|< <= > >=||Relational||left to right||binary|
|== !=||Equality||left to right||binary|
|&||Bitwise AND||left to right||binary|
|^||Bitwise XOR||left to right||binary|
||||Bitwise OR||left to right||binary|
|&&||Logic AND||left to right||binary|
|||||Logic OR||left to right||binary|
|? :||Conditional||right to left||unusual|
|= += and all combined assignment||Assignment||right to left||binary|
|,||comma||left to right||binary|