Introduction
Arithmetic operators are used to perform mathematical calculations in C. They allow you to add, subtract, multiply, divide, and find the remainder of numbers. These operators are among the most frequently used operators in C programming and are essential for solving computational problems.
Whether you are calculating a total, averaging scores, incrementing a counter, or performing scientific calculations, arithmetic operators play a fundamental role in almost every C program.
What Are Arithmetic Operators?
An arithmetic operator performs a mathematical operation on one or more operands and produces a numeric result.
Syntax
operand operator operand
For example, in the expression a + b, a and b are operands, while + is the arithmetic operator.
List of Arithmetic Operators
| Operator | Name | Description | Example |
|---|---|---|---|
+ |
Addition | Adds two operands. | a + b |
- |
Subtraction | Subtracts the right operand from the left operand. | a - b |
* |
Multiplication | Multiplies two operands. | a * b |
/ |
Division | Divides the left operand by the right operand. | a / b |
% |
Modulus | Returns the remainder after integer division. | a % b |
Addition Operator (+)
The addition operator adds two numeric values together.
Example
#include <stdio.h>
int main(void)
{
int a = 12;
int b = 8;
printf("%d\n", a + b);
return 0;
}
Output
20
Subtraction Operator (-)
The subtraction operator subtracts the second operand from the first operand.
Example
#include <stdio.h>
int main(void)
{
int a = 20;
int b = 7;
printf("%d\n", a - b);
return 0;
}
Output
13
Multiplication Operator (*)
The multiplication operator multiplies two operands.
Example
#include <stdio.h>
int main(void)
{
int width = 5;
int height = 4;
printf("%d\n", width * height);
return 0;
}
Output
20
Division Operator (/)
The division operator divides one operand by another. The result depends on the data types of the operands.
Integer Division
When both operands are integers, the fractional part is discarded.
Example
#include <stdio.h>
int main(void)
{
printf("%d\n", 10 / 3);
return 0;
}
Output
3
Floating-Point Division
If at least one operand is a floating-point value, the result includes the decimal part.
Example
#include <stdio.h>
int main(void)
{
printf("%.2f\n", 10.0 / 3);
return 0;
}
Output
3.33
Modulus Operator (%)
The modulus operator returns the remainder after integer division.
Example
#include <stdio.h>
int main(void)
{
printf("%d\n", 17 % 5);
return 0;
}
Output
2
The modulus operator can only be used with integer operands.
Example
if (number % 2 == 0)
{
printf("Even\n");
}
else
{
printf("Odd\n");
}
Unary Plus and Unary Minus
Besides their binary forms, the + and - operators can also be used as unary operators.
| Operator | Description | Example |
|---|---|---|
+ |
Indicates a positive value. | +a |
- |
Negates the operand. | -a |
Example
int a = 10;
printf("%d\n", -a);
Output
-10
Operator Precedence
Arithmetic operators follow a predefined order of evaluation called operator precedence.
| Precedence | Operators | Associativity |
|---|---|---|
| Highest | + - (Unary) |
Right to Left |
* / % |
Left to Right | |
| Lowest | + - (Binary) |
Left to Right |
For example:
Example
int result = 5 + 4 * 3;
The multiplication is performed first.
Output
17
Example
int result = (5 + 4) * 3;
Common Beginner Mistakes
- Expecting integer division to return a decimal value.
- Using the modulus operator with floating-point numbers.
- Ignoring operator precedence.
- Dividing by zero, which results in undefined behavior.
- Using integer types when floating-point calculations are required.
Complete Example
The following program demonstrates all arithmetic operators.
Example
#include <stdio.h>
int main(void)
{
int a = 20;
int 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("Remainder: %d\n", a % b);
return 0;
}
Summary
Arithmetic operators perform basic mathematical operations and are used in nearly every C program.
| Operator | Operation |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus (Remainder) |
Understanding how arithmetic operators work—including integer division, the modulus operator, unary operators, and operator precedence—provides a solid foundation for writing calculations, algorithms, and more advanced C programs.