Arithmetic operator in C.

Hello coder, welcome back!

From this tutorial will look into operators use in C.

1) Arithmetic operator 


It is a binary operator that means it requires two operands.
Let's look into the code- 


We get the required result. Similarly, you can use any of the operators. Before that let's look into precedence rule.

Precedence means if we use all the arithmetic operator which operator is given the first priority that is known as precedence.


  • Always *, /, %, (multiplication, division, modulo) these operators has the highest priority. Then +, - has the priority or you can call the lowest priority. 
  • What if all the operators are present in the same equation? Then comes the associativity. Always from left to right whichever operator lies that operation is performed. 

Let's look into the code to understand precedence rule-


Here in this code, result1 and result2 have operators that have the same priority, so associativity comes into the picture and it follows left to the right operation.
result3 is a bit tricky. Many of you commit make a mistake here, the majority of them first perform division, then subtraction and Multiplication. NO! it's completely WRONG. let me show you how it's actually represented-
(a/b)-(c*a) this is how it is represented, always arithmetic operators are performed between two operands. You cant divide and multiply the result with 'a' it gives you the wrong answer. Always go with precedence rule and perform the operation between two operands.

Hope so you got a clear idea about the arithmetic operator and its precedence and associativity rules. If any doubts drop a comment.

Happy learning!

Comments

Popular post