Arithmetic Operators in C Programming
In C programming, arithmetic operators are fundamental tools used to perform basic mathematical operations on numeric values
These Operators allow you to carry out basic operations such as:
- Addition(+)
- subtraction(-)
- Multiplication(*)
- Division(/)
- Modulus(%)
Rules for Arithmetic operators:
- Precedence rule:If we have more than one operator then the question arises that which will be evalute first.
for example:
int z= 3+3*4/5-2
= 3+(3*4)/5-2// mul and division operators have higher precedence than add and sub and after that follow associative rule
= 3+12/5-2
= (3+2)-2// for arithmetic operators associativity is from left to right.
=5-2
=3
Precedence:
|
|
- Integer /Integer is always integer like 5/2 is 2 and not 2.5. we have to ignore float value and the output should be a floor value
- Associative rule: If more than one Operator is having same precedence in an expression then we have to follow associative rule.
which one we should follow first one or second one. that can be determine by associative rule.
"For All arithematic operators associativity is from left to right"
therefore, -7 is correct answer.
practice Questions:
- If int takes 2 bytes then, int x=300*300/300; , what is the value of x?
- int x=3/3/3/3; what is the value of x?
- evaluate the expression 3+3*4*3/6/4*3-2 and give final value.
- evaluate the expression 13/4*3%5 and give the final value.
What's Your Reaction?