Arithmetic operators in C are used to perform basic mathematical operations on variables and values. These operators work on numeric data types such as int, float, double, and long.
C provides five primary arithmetic operators:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
Let’s explore each one with examples.
1. Addition Operator (+)
The addition operator adds two values or variables.
Syntax
a + b
Example
#include <stdio.h>
int main() {
int x = 10, y = 20;
int sum = x + y;
printf("Sum = %d", sum);
return 0;
}
Output
Sum = 30
2. Subtraction Operator (-)
The subtraction operator subtracts one value from another.
Example
int a = 15, b = 5;
int diff = a - b; // diff = 10
3. Multiplication Operator (*)
Used to multiply two numbers.
Example
int a = 6, b = 4;
int product = a * b; // product = 24
4. Division Operator (/)
The division operator divides one number by another.
⚠️ Important:
If both operands are integers, integer division occurs (fractional part is discarded).
Example 1: Integer Division
int a = 7, b = 2;
int result = a / b; // result = 3 (NOT 3.5)
Example 2: Floating-point Division
float a = 7, b = 2;
float result = a / b; // result = 3.5
5. Modulus Operator (%)
The modulus operator returns the remainder when one integer is divided by another.
✔ Works only with integers.
Example
int a = 10, b = 3;
int rem = a % b; // rem = 1
6. Combined Example
#include <stdio.h>
int main() {
int a = 20, b = 6;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Modulus: %d\n", a % b);
return 0;
}
7. Unary Arithmetic Operators
C also has unary arithmetic operators that work on a single operand.
Increment (++)
Increases a value by 1.
int a = 5;
a++; // a becomes 6
Decrement (–)
Decreases a value by 1.
int a = 5;
a--; // a becomes 4
8. Operator Precedence
When multiple arithmetic operations appear in an expression, C follows precedence rules:
| Precedence | Operators |
|---|---|
| Highest | *, /, % |
| Lowest | +, - |
Example
int result = 10 + 6 * 2; // result = 22
9. Real-World Example
Suppose we want to calculate the total marks and percentage of a student:
int m1 = 85, m2 = 90, m3 = 80;
int total = m1 + m2 + m3;
float percentage = (total / 3.0);
printf("Total: %d\n", total);
printf("Percentage: %.2f", percentage);
Conclusion
Arithmetic operators are fundamental in C programming. They allow you to perform mathematical operations, calculate values, and build logic for complex programs. Understanding how they behave — especially integer vs. floating-point division — is essential for accurate results.
🔗 View other articles about C Programming:
https://savanka.com/category/learn/c-programming