“From today on, study hard and make progress every day”
Repetition is the best method for memory; spend one minute each day to remember the basics of C language.
“Essential Knowledge Points for C Language Beginners: 100 Articles Series“
17. Arithmetic Operators: Addition, Subtraction, Multiplication, Division, and Modulus are Simple, but There are 2 Common Pitfalls for Beginners to Avoid
1. Basic Arithmetic Operators
The C language provides 5 basic arithmetic operators:
| Operator | Name | Example | Mathematical Equivalent |
| + | Addition | a + b | a + b |
| – | Subtraction | a – b | a – b |
| * | Multiplication | a * b | a × b |
| / | Division | a / b | a ÷ b |
| % | Modulus | a % b | a mod b |
2. Examples of Operator Usage
1. Basic Operations
int a = 10, b = 3;
printf("%d + %d = %d\n", a, b, a + b); // 13
printf("%d - %d = %d\n", a, b, a - b); // 7
printf("%d * %d = %d\n", a, b, a * b); // 30
printf("%d / %d = %d\n", a, b, a / b); // 3
printf("%d %% %d = %d\n", a, b, a % b); // 1
2. Mixed Type Operations
int x = 5;
double y = 2.0;
printf("%d / %.1f = %.1f\n", x, y, x / y); // 2.5 (automatic type conversion, unified to floating-point calculation)
3. Common Issues with Integer Division
1. Truncation
int result = 5 / 2; // Result is 2, not 2.5
2. Forced Type Conversion for Correct Results
double quotient = (double)5 / 2; // 2.5 (forced type conversion)
4. Rules of Modulus Operation
1. Basic Properties
printf("%d %% %d = %d\n", 10, 3, 10 % 3); // 1
printf("%d %% %d = %d\n", -10, 3, -10 % 3); // -1 (sign same as dividend)
2. Common Uses
- 1. Check for Even or Odd:
if (num % 2 == 0) { /* Even number */ } - 2. Loop Cycle:
int pos = i % array_size; // Ensure index is within array bounds
5. Operator Precedence
The precedence of arithmetic operators from high to low:
- 1. Parentheses
<span>()</span> - 2. Unary Plus and Minus
<span>+a</span>,<span>-a</span> - 3. Multiplication, Division, Modulus
<span>*</span>,<span>/</span>,<span>%</span> - 4. Addition, Subtraction
<span>+</span>,<span>-</span>
Example:
int result = 5 + 3 * 2; // 11 (not 16)
int correct = (5 + 3) * 2; // 16
6. Compound Assignment Operators
| Operator | Equivalent Expression |
| a += b | a = a + b |
| a -= b | a = a – b |
| a *= b | a = a * b |
| a /= b | a = a / b |
| a %= b | a = a % b |
Example:
int x = 10;
x += 5; // x = 15
x /= 3; // x = 5
7. Common Errors
1. Integer Overflow
int big = 2000000000;
printf("%d\n", big * 2); // Overflow!
Correct approach: Use a larger type (long long)
long long big = 2000000000LL;
printf("%lld\n", big * 2);
2. Division by Zero
int a = 5, b = 0;
printf("%d\n", a / b); // Runtime error!
Correct approach: Check if the divisor is zero.
if (b != 0) {
printf("%d\n", a / b);
} else {
printf("Divisor cannot be zero\n");
}
8. Some Calculation Tricks
1. Swap Variable Values (without Temporary Variable)
a = a + b;
b = a - b;
a = a - b;
2. Check for Divisibility
if (a % b == 0) { /* a is divisible by b */ }
3. Get Each Digit of an Integer
int num = 123;
int units = num % 10; // 3
int tens = (num / 10) % 10; // 2
int hundreds = num / 100; // 1
9. Floating Point Precision Issues
1. Example of Precision Loss
float f = 0.1f;
printf("%.20f\n", f); // 0.10000000149011611938
2. Comparing Floating Point Numbers
#include <math.h>
if (fabs(a - b) < 1e-6) { /* Considered equal */ }
Some students contacted me, wanting to have a study and exchange group. Previously, I hesitated to create a group due to concerns about advertisements, but I think having a group would indeed be convenient, so I will try to create one this time.
If you need it, hurry up and join, valid for 7 days.

———- End ———-
[Special Statement: All articles in this public account are original or authorized by the author, some content and images are sourced from the internet and AI, please feel free to consume, opinions are for learning reference only~~]


“If you like C, please like it”
Click the bottom right corner to see
“