In C programming, arithmetic operators are used to perform basic mathematical operations. These operators take numerical values (either constants or variables) and return a single numerical value as a result.
List of Arithmetic Operators
Operator | Description | Example |
---|---|---|
+ | Addition | a + b adds a and b |
- | Subtraction | a - b subtracts b from a |
* | Multiplication | a * b multiplies a and b |
/ | Division | a / b divides a by b |
% | Modulus (remainder of division) | a % b gives remainder of a/b |
1. Addition Operator (+
)
The addition operator adds two numbers.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int sum = a + b;
printf("Sum: %d\n", sum);
// Output: Sum: 30
return 0;
}
Explanation:
In this example, the values of a
and b
are added together, and the result (30) is stored in sum
.
2. Subtraction Operator (-
)
The subtraction operator subtracts the second number from the first.
Example:
#include <stdio.h>
int main() {
int a = 30, b = 15;
int difference = a - b;
printf("Difference: %d\n", difference);
// Output: Difference: 15
return 0;
}
Explanation:
In this case, the value of b
is subtracted from a
, and the result (15) is stored in difference
.
3. Multiplication Operator (*
)
The multiplication operator multiplies two numbers.
Example:
#include <stdio.h>
int main() {
int a = 5, b = 4;
int product = a * b;
printf("Product: %d\n", product);
// Output: Product: 20
return 0;
}
Explanation:
Here, a
and b
are multiplied, and the result (20) is stored in product
.
4. Division Operator (/
)
The division operator divides one number by another.
Example:
#include <stdio.h>
int main() {
int a = 20, b = 5;
int quotient = a / b;
printf("Quotient: %d\n", quotient);
// Output: Quotient: 4
return 0;
}
Explanation:
The value of a
is divided by b
, and the result (4) is stored in quotient
.
Note:
- Integer Division: If both operands are integers, the result is an integer, and any fractional part is discarded.
- Example:Â
7 / 2
 will result inÂ3
, notÂ3.5
.
5. Modulus Operator (%
)
The modulus operator returns the remainder of a division operation.
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
int remainder = a % b;
printf("Remainder: %d\n", remainder);
// Output: Remainder: 1
return 0;
}
Explanation:
10 % 3
 gives the remainder when 10
 is divided by 3
, which is 1
.
Note:
- The modulus operator is only used with integer types.