# C and C++ - Operators, Precedence 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 is carried out on the operands by the operator.
- Expression: simply the statement that contains any string of operators, variables and numbers.

## C Operators - Examples

Expression | Operand | Operators | Operation |
---|---|---|---|

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 |

## C Programming - 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.

## C Programming - Operators Precedence in Expressions

Let us consider an expression.

X = 2 + 5 * 4

This above expression can be evaluated in two different ways.

- if we perform addition and then multiplication, we will get 28 as a result.
X = 7 * 4 = 28

- if we perform multiplication and then addition, now we will get 22 as result.
X = 2 + 20 = 22

Two different results came from the above evaluation. Now, we cannot say that which one is correct. In order to resolve this confusion, we can assign different priory for different operators. The way in which operators are given priority in C programming language is known as **Operator Precedence**. Operator precedence defines that some operators have highest priory than the other and that highest priority operators will be evaluated first. Therefore, the correct result of an above expression in C programming 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. C language allow us to group the operations by using parentheses.

The following table shows the operators precedence from highest priority at the top to lowest priority at the bottom.

**Note: On Mobile devices, Please scroll the table to the right to read**

Operator | Operations | Associativity | Notes |
---|---|---|---|

( ) [ ] -> . | Parentheses, square brackets, arrow and dot | left to right | used 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 |

= += -= *= ; /= %= |= ; &= ^= | Assignment | right to left | binary |

, | comma | left to right | binary |