C Arithmetic Operators: Beginner's Guide
If you are starting your journey in programming, learning C programming is one of the best decisions. C is known as a foundational language, meaning many modern programming languages are built based on it.
C is a general-purpose programming language used to build:
- Operating systems
- Embedded systems
- Software applications
It is powerful, fast, and widely used.
What Are Operators in C?
In simple words:
Operators are symbols that perform operations on values.
For example: 5 + 3
5and3are values (called operands)+is an operator
What Are Arithmetic Operators in C?
Arithmetic operators are used to perform basic mathematical calculations like addition, subtraction, multiplication, and division.
They are essential because:
- Every program needs calculations
- Used in billing, banking, games, etc.
List of Arithmetic Operators in C
| Operator | Name | Description | Example | Result |
|---|---|---|---|---|
+ | Addition | Adds two values | 5 + 3 | 8 |
- | Subtraction | Subtracts right from left | 5 - 3 | 2 |
* | Multiplication | Multiplies two values | 5 * 3 | 15 |
/ | Division | Divides left by right | 5 / 2 | 2 (int) or 2.5 (float) |
% | Modulus | Returns remainder (integers only) | 5 % 2 | 1 |
Explanation of Each Operator (Step-by-Step)
Addition Operator (+)
Definition:
Adds two values together.
Syntax:
Example:
#include <stdio.h>
int main() {
int a = 5, b = 3;
int sum = a + b;
printf("Sum = %d", sum);
return 0;
}Output: Sum = 8
Real-Life Analogy:
Adding apples: 5 apples + 3 a pples = 8 apples
Subtraction Operator (-)
Definition:
Subtracts one value from another.
Syntax: result = a – b;
Example:
int result = 10 - 4;
printf("%d", result);Output: 6
Real-Life Analogy:
You had ₹10, spent ₹4 → ₹6 left.
Multiplication Operator (*)
Definition:
Multiplies two values.
Syntax: result = a * b;
Example:
int result = 6 * 3;
printf("%d", result);Output: 18
Real-Life Analogy:
6 packets × 3 biscuits = 18 biscuits
Division Operator (/)
Definition:
Divides one value by another.
Syntax: result = a / b;
Example:
int result = 10 / 2;
printf("%d", result);Output: 5
Important:
If both numbers are integers → result is integer
int result = 5 / 2;
printf("%d", result);Output: 2
Real-Life Analogy:
10 chocolates shared between 2 people → each gets 5
Modulus Operator (%)
The modulus operator (%) in C returns the remainder after dividing one number by another.
Syntax: result = a % b;
Example:
int result = 5 % 2;
printf("%d", result);Output: 1
Real-Life Analogy:
5 chocolates divided among 2 people → 1 chocolate remains
Increment Operator (++)
The increment operator (++) increases the value of a variable by 1.
Syntax: a++;
Example:
int a = 5;
a++;
printf("%d", a);Output: 6
Real-Life Analogy:
Step counter: 5 steps → +1 → 6 steps
Decrement Operator (–)
Definition:
Decreases the value by 1.
Syntax: a—;
Example:
int a = 5;
a--;
printf("%d", a);Output: 4
Real-Life Analogy:
Battery level: 5 → drops to 4
Detailed Examples with Code
Example 1: Basic Calculator
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
return 0;
}Explanation:
#include <stdio.h>→ allows printingint main()→ starting pointprintf()→ displays output
Example 2: Increment & Decrement
#include <stdio.h>
int main() {
int a = 5;
printf("Original: %d\n", a);
a++;
printf("After Increment: %d\n", a);
a--;
printf("After Decrement: %d\n", a);
return 0;
}Operator Precedence & Associativity
What is Operator Precedence?
It defines which operation happens first
Example: int result = 5 + 3 * 2;
Output: 11
Because:
- Multiplication first → 3 × 2 = 6
- Then addition → 5 + 6 = 11
Order of Execution
| Priority | Operator |
|---|---|
| High | ++, — |
| Medium | *, /, % |
| Low | +, – |
Associativity
Direction of evaluation
Example: 10 – 5 – 2
Real-World Use Cases
Arithmetic operators are used everywhere:
Banking Apps
- Balance calculation
- Interest calculation
Billing Systems
- Total bill
- Tax calculation
Games
- Score updates
- Health reduction
Calculators
- Basic operations
Best Practices
| Tip | Reason |
|---|---|
Use (float) for accurate division | Prevents integer truncation |
| Avoid modulus with float types | % only works with integers |
| Check divisor before division | Prevent division by zero error |
Common Mistakes to Avoid
| Mistake | Problem |
|---|---|
5 / 2 = 2.5 (wrong) | Result is 2 if both are int |
5.0 % 2 (invalid) | Modulus only works on ints |
| Division by zero | Runtime error |
FAQs on C Arithmetic Operators
1. What are arithmetic operators in C?
Arithmetic operators are symbols used to perform mathematical operations like addition, subtraction, multiplication, and division.
2. What is the modulus operator in C?
It returns the remainder after division of two numbers.
3. What is the difference between ++a and a++?
++a→ increment firsta++→ increment later
4. Why does 5/2 give 2 in C?
Because both values are integers, so the result is also an integer.
5. Can modulus work with float values?
No, % only works with integers.