Arithmetic Operators
C Programming Series - Article 2

π Hey, I'm Shreya, a Senior Software Engineer in an Leading MNC. Join me on this blog as I share insights, tips, and experiences. Let's explore these fascinating worlds together! ππ» #CodeAndSecure
In the C language, mathematical operations follow specific rules that differ slightly from standard algebra, especially regarding data types and operation order. Below is a detailed compilation of notes covering Arithmetic Operators, derived directly from class examples.
1. Basic Arithmetic Operators
C provides the standard set of operators for calculation:
+: Addition (Sum)-: Subtraction (Difference)*: Multiplication (Product)/: Division (Quotient)%: Modulus (Remainder)
While addition, subtraction, and multiplication are straightforward, Division and Modulus have special behaviour in C.
2. The Division Operator (/)
The behaviour of the division operator depends on the data types of the operands (Int vs. Float).
Rule 1: Integer vs. Float Division
Int / Int = Int: The result is the quotient only. The decimal part is truncated.
Int / Float = Float: The result includes the decimal part.
Examples from Notes:
a = 5 / 2;β Result is 2 (The0.5is discarded).a = 5.0 / 2;β Result is 2.5 (Float division).a = 5 / 2.0;β Result is 2.5.
Rule 2: Sign of the Quotient
For division, the sign of the result follows standard algebraic rules:
+ / + = ++ / - = -- / + = -- / - = +
Examples:
a = -5 / 2;β Result: -2a = 5 / -2;β Result: -2a = -5 / -2;β Result: 2a = 2 / -5;β Result: 0 (Since 2 < 5, quotient is 0).
3. The Modulus Operator (%)
The modulus operator returns the remainder of the division. The notes highlight four critical rules for using %.
Rule A: The Sign Rule
The Rule: In C, the result of a modulus operation always takes the sign of the Numerator (the left operand). The sign of the denominator is ignored.
Derivation:
- Formula:
Dividend = Divisor * Quotient + Remainder
Examples:
a = 5 % 2;β 1 (Standard positive)a = 5 % -2;β 1 (Sign of 5 is +)a = -5 % 2;β -1 (Sign of -5 is -)a = -5 % -2;β -1 (Sign of -5 is -)
Rule B: The "Small Numerator" Rule
If the numerator is smaller than the denominator, the result is equal to the numerator itself.
Examples:
18 % 25β 1815 % 30β 152 % 5β 2
Rule C: The Float Restriction
The modulus operator is only valid for integers.
Invalid:
a = 5.0 % 2;β Compilation Error (Illegal use of floating point).Invalid:
a = 5.0 % 2.0;β Compilation Error.
Rule D: Extracting the Unit Digit
Using modulo 10 is a standard trick to get the last digit of a number.
Examples:
13 % 10β 378 % 10β 8129 % 10β 9
Note:
x % y = 0only whenxis a multiple ofy.
4. Operator Precedence and Associativity
When multiple operators appear in one expression, C uses a Priority Table to decide the order.
Priority Levels
High Priority:
*,/,%Low Priority:
+,-
Associativity (The Tie-Breaker)
If operators have the same priority (e.g., * and /), they are evaluated from Left to Right (L β R).
Complex Evaluation Examples
1. Left-to-Right Evaluation (/ and *)
Expression:
a = 13 / 2 * 5;Step 1:
13 / 2= 6 (Integer division).Step 2:
6 * 5= 30.Result:
30.
Expression:
a = 13 * 2 / 5;Step 1:
13 * 2= 26.Step 2:
26 / 5= 5.2 β 5 (Truncated).Result:
5.
2. Multiple Divisions
Expression:
a = 21 / 5 / 2;Step 1:
21 / 5= 4.Step 2:
4 / 2= 2.Result:
2.
3. Mixed Priorities
Expression:
a = 2 + 3 * 5;*has higher priority.Step 1:
3 * 5= 15.Step 2:
2 + 15= 17.Result:
17.
Expression:
a = 3 * 5 * 2 % 7;All are high priority. Evaluate L β R.
3 * 5= 15.15 * 2= 30.30 % 7= 2.Result:
2.
Expression:
a = 6 - 4 + 2 - 3;All are low priority. Evaluate L β R.
6 - 4= 2.2 + 2= 4.4 - 3= 1.Result:
1.
5. Real-World Application: ATM Logic
The modulus operator is essential for checking divisibility.
Scenario: An ATM needs to check if the withdrawal amount is valid for 100-rupee notes.
Total in Account: 8930
Withdrawal Request: 310
Logic:
if (310 % 100 == 0) {
// This block runs if remainder is 0
give_money();
}
else {
// This block runs because 310 % 100 = 10 (Not 0)
printf("Error: Please enter cash in multiples of 100");
}
π Navigate the Series
Previous Article: https://shreyashree.hashnode.dev/syntax-and-constants
Next Article: (Link to be added once published)
π Whole Series Link: https://shreyashree.hashnode.dev/series/c-course


