1. What is operator ? State arithmetic operator. Explain the hierarchy of arithmetic operator, with suitable example.
  2. What is operator precedence ? Explain with an arithmetic expression.
  3. Explain with example arithmetic operators and state its precedence.

 C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform a certain mathematical or logical manipulation. Operators are used in programs to manipulate data and variables.

C Arithmetic Operators

An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).

C supports all the basic arithmetic operators. The following table shows all the basic arithmetic operators.

Operator

Description

+

adds two operands

subtract second operands from first

*

multiply two operand

/

divide numerator by denominator

%

remainder of division

Hierarchy Of Operations

In a programming language, when an expression involves multiple operations, the hierarchy of operations instructs the compilers and interpreters on the order in which the expression has to be executed. Usually, the expression consists of operations like, addition, subtraction, multiplication, division, exponents, and parentheses.

The hierarchy of operations is as follows:

  • Parentheses ()
  • Exponents
  • Multiply or Divide
  • Addition or Subtraction

Parentheses

Execution of the expressions inside the parentheses starting from the innermost parenthesis. Other than parentheses the special characters like brackets [ ], braces { } and square root also fall under this category.

Exponents

After solving the operations in the parentheses, choose the exponents for execution.

 Multiplication or Division

The order of multiplication and division in an expression has the third priority. Perform the multiplication or division operation in the left to right order.

Addition or Subtraction

The order of addition or subtraction in an expression has the last priority. Perform the addition or subtraction operation in the left to right order.

Example:

Solve the following expression:

a = 3*2/2+2/2+6-1+5/8

a = 3*2/2+2/2+6-1+5/8

a = 6 / 2 + 2 /2 + 6 – 1 + 5 / 8

operation: *

a= 3 + 2 / 2 + 6 – 1 + 5 / 8

operation: /

a = 3 + 1+ 6 – 1 + 5 / 8

operation: /

a = 3 + 1 + 6 – 1 + 0

operation: /

a = 4 + 6 – 1 + 0

operation: +

a = 10 – 1 + 0

operation: +

a = 9 + 0

operation: –

a = 9

operation: +