YTread Logo
YTread Logo

Arithmetic Operators in C

Mar 16, 2024
In this lecture we are going to talk about

arithmetic

operators

in C. These are the different

operators

available in C language. The first one is called addition operator. The second is called the subtraction operator. The third is called the multiplication operator. The fourth is called the division operator. And the last one is called modulo operator. Apart from this, they are all binary operators. It means that two operands are required to perform the operation. Good? For example, if you want to perform addition, we always perform addition between two numbers. This is the first operand. And this is the second operand.
arithmetic operators in c
Op1 and Op2. Op simply means operand. OK. If you want to perform addition, we need to have two operands. That's why addition is called a binary operator. And similarly, subtraction is also a binary operator, multiplication is also a binary operator, division is also a binary operator and modulo is also a binary operator. Now let's consider an example. Here you can see, I have declared two variables a and b. With the help of this printf function, you could ask the user to enter the first number. With the help of this scanf function, I will be able to accept inputs from the user.
arithmetic operators in c

More Interesting Facts About,

arithmetic operators in c...

With the help of this printf function, I will be able to ask the user to enter the second number. With the help of this scanf function, I will be able to accept the second input from the user. Now, I'm simply going to print the result of the division as well as the modulo. The division will result in the quotient. And the module will result from the rest. Let's run the code. Suppose I enter 9 as the first number. Then I enter 3 as the second number, so this would be the result. Result of the division of these two numbers 9 and 3, that is, when 9 is divided by 3 this gives us 3 as a result, which is the quotient.
arithmetic operators in c
And when 9 is divided by 3, the remainder would be equal to 0. And we already know this. So there is no problem in understanding this concept. OK. Now, let's talk about operator precedence and associativity. Suppose you have an expression like a + b * c / d. Precedence simply means which operation will be performed first on that expression and which will be performed last. Here you can see that multiplication, division and module have the same priority. Suppose if these operators are present in your expression, then they all have the same priority. It is not the case that division is done first, then multiplication, and then modulo.
arithmetic operators in c
They all have the same precedence. On the other hand, when we have addition and subtraction, these two operations also have the same priority. Suppose we have an expression like a + b - c, then we cannot say that subtraction is going to be done first and then addition. Both have the same precedence. Now, an important point. Suppose we have an expression like a/b + c. In that case, division is done first and then addition. That means a/b is done first and then the sum is done because the precedence of this set is the highest and the precedence of this set is the lowest.
Multiplication, division, and modulus are done first in the expression, followed by addition and subtraction. Now that we know this, let's assume that in our expression we have multiplication, division and module only. As we know, the precedence of these operators is the same. Now, in that case, who will act first? If the precedence of the operators is the same, then associativity comes into the picture. The associativity of these operators is from left to right. And for these operators it is also from left to right. Associativity is used only when two or more operators have the same precedence.
For example, we have + and - Right? These two operators we know have the same precedence. Therefore, we use associativity which is from left to right. Suppose we have an expression like a + b - c, in that case the addition will be done first because it is on the left side. And we know this: the associativity for addition and subtraction is from left to right. Therefore, the leftmost symbol, which is addition, will be executed first and then subtraction. Now let's consider an example of coding. Here you can see that I have declared 4 variables, a, b, c and d.
And I assign them the values ​​2, 3, 4 and 5 respectively. Good? In this printf function, I am trying to print this expression along with the result of this expression. which is a*b/c. This %d will be replaced by the result of this expression. Similarly, this %d will be replaced by the result of this expression. And this %d will be replaced by the result of this particular expression. If we evaluate this expression a * b / c, then this is equivalent to 2 * 3 / 4. As you know, multiplication and division have the same priority. Therefore, associativity comes into the picture. And the associativity of multiplication, division and module is from left to right.
Good? Therefore, multiplication is going to be done first and then division. a*b will be evaluated first, that means 2*3 which is equal to 6. Then 6/4 which is equal to 1.5. As we are trying to print an integer, therefore the decimal part is truncated and the result is 1. For this expression we also know that addition will be done first and then subtraction because they have the same precedence and the associativity is left-handed to the right. Therefore, addition is done first and then subtraction is done. a + b which is equal to 2 + 3 which is equal to 5 and c is equal to 4.
Therefore, 5 - 4 is equal to 1. Therefore, this printf function will also print 1. In the third printf function, we need to know two things. First of all, I put two % here. Because I need to print a % on the screen. If you just type a %, then the compiler would think of %A as a format specifier which is not what we want. Therefore, to eliminate the confusion we are putting two % to print a % on the screen. OK. Now let's evaluate this expression. This is our expression. We know that multiplication and module have higher priority compared to addition and subtraction. Therefore, they will be executed first.
This parenthesis simply means that multiplication and modulus will be executed first compared to addition and subtraction. After replacing the values, this expression looks like this. a + (3 * 5) - (4 % 2). 3 * 5 is equal to 15 and 4 % 2 is equal to 0. As we know, when you divide 4 by 2, the remainder would be 0. Therefore, the new expression would be a + 15 - 0. Let's substitute the value of a now it becomes 2 + 15 - 0. And finally, we know which 2 + 15 is going to be performed first since addition and subtraction have the same precedence, associativity came into the picture and associativity is from left to right. to the right. Therefore, first the addition will be done and then the subtraction.
That means 2 + 15 equals 17 and then 17 - 0 finally equals 17. What is our final result. Therefore, we can say that the result of the first expression is 1. The result of the second expression is 1. And the result of the last expression is 17. Please note here. This is % The compiler would consider it a format specifier if you put a % here, which we don't want. Well friends, that's all for now. Thank you for watching this conference.

If you have any copyright issue, please Contact