Arithmetic operators in C are used to perform mathematical calculations on numeric values such as integers and floating-point numbers. They enable operations like addition, subtraction, multiplication, division, and modulus, making them essential for performing calculations, processing data, and solving computational problems in C programs.
- Support both integer and floating-point calculations.
- Classified into binary and unary arithmetic operators.
#include <stdio.h>
int main() {
// Calculate the area of the triangle
int sum = 10 + 20;
printf("%d", sum);
return 0;
}
Output
30
Explanation:In this example, the addition operator (+) adds the values 10 and 20. The result 30 is stored in the variable sum and displayed using printf().
Arithmetic operators can be classified into two types based on the number of operands
Binary Arithmetic Operators
Binary arithmetic operators work on two operands and perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.
- Perform basic mathematical calculations.
- Return a numeric result.
- Work with both variables and constants.
The binary arithmetic operators work on two operands. C provides 5 such operators for performing arithmetic functions which are as follows:
| Name | Operator | Arithmetic Operation | Syntax |
|---|---|---|---|
Addition | + | Add two operands. | x + y |
Subtraction | - | Subtract the second operand from the first operand. | x - y |
Multiplication | * | Multiply two operands. | x * y |
Division | / | Divide the first operand by the second operand. | x / y |
% | Calculate the remainder when the first operand is divided by the second operand. | x % y |
#include <stdio.h>
int main()
{
int a = 10, b = 4, res;
// Addition
res = a + b;
printf("a + b is %d\n", res);
// Subtraction
res = a - b;
printf("a - b is %d\n", res);
// Multiplication
res = a * b;
printf("a * b is %d\n", res);
// Division
res = a / b;
printf("a / b is %d\n", res);
// Modulus
res = a % b;
printf("a %% b is %d\n", res);
return 0;
}
Output
a + b is 14 a - b is 6 a * b is 40 a / b is 2 a % b is 2
Note: All arithmetic operators can be used with float and int types except the modulo operator that can only be used with integers.
Unary Arithmetic Operators
Unary arithmetic operators work on a single operand and are mainly used to increase, decrease, or change the sign of a value.
- Include increment (++) and decrement (--) operators.
- Support unary plus (+) and unary minus (-).
- Useful for counters and loop control.
The unary arithmetic operators work with a single operand. In C, we have four such operators which are as follows:
| Name | Operator | Description | Syntax |
|---|---|---|---|
| Decrement | -- | Decreases the integer value of the variable by one. | --a or a-- |
| Increment | ++ | Increases the integer value of the variable by one. | ++a or a++ |
| Unary Plus | + | Returns the value of its operand. | +a |
| Unary Minus | - | Returns the negative of the value of its operand. | -a |
#include <stdio.h>
int main()
{
int a = 10, res;
// post-incrementing a res is assigned 10, a is not updated yet
res = a++;
printf("a is %d, res is %d\n", a, res);
// post-decrementing res is assigned 11, a is not updated yet
res = a--;
printf("a is %d, res is %d\n", a, res);
res = ++a;
// a and res have same values = 11
printf("a is %d, res is %d\n", a, res);
res = --a;
// a and res have same values = 10
printf("a is %d, res is %d\n", a, res);
printf("+a is %d\n", +a);
printf("-a is %d", -a);
return 0;
}
Output
a is 11, res is 10 a is 10, res is 11 a is 11, res is 11 a is 10, res is 10 +a is 10 -a is -10
Explanation: In the given example, postfix operators (a++, a--) first assign the current value and then update the variable. Prefix operators (++a, --a) update the variable first and then use the updated value. Unary plus returns the value unchanged, while unary minus returns its negative value.
Multiple Operators in a Single Expression
An expression can contain multiple arithmetic operators. In such cases, C follows operator precedence and associativity rules to determine the order in which operations are performed.
#include <stdio.h>
int main(){
int var;
// expression with multiple operators
var = 10 * 20 + 15 / 5;
printf("%d", var);
return 0;
}
Output
203
Explanation: In the expression 10 * 20 + 15 / 5, multiplication and division are performed first because they have higher precedence than addition. Therefore, the expression is evaluated as (10 * 20) + (15 / 5), resulting in 200 + 3 = 203.