Arithmetic operators are used in C to perform mathematical operations such as addition, subtraction, multiplication, division, and mod. The arithmetic operators are applied to two operands. The operators will add, subtract, multiply, and divide the values. The second operand will perform the arithmetic operation on the first operand.
There are five arithmetic operators in the C programming language. The arithmetic operators are the basic operators. The additional operator will combine two or more values. If there are three or more values, it will add the first two and use the result to multiply by the third. The addition will be performed in the same manner for the remaining values. The subtraction will be the second value subtracted from the first. If three or more values are found, the subtraction will be performed using the result of the previous subtraction.
Multiplication, division, and mod operators take precedence over addition and subtraction. When an expression contains both addition and multiplication, the multiplication is done first, followed by the addition. If you need to perform addition before multiplication, use parenthesis for the additional operators.
Arithmetic Operators in C
The table below lists all of the arithmetic operators provided by the C programming language. Addition, subtraction, multiplication, division, and Mod operators are the arithmetic operators. The division and mod operators differ in that the division operator returns the coefficient while the mod operator returns the remainder of the division operation.
Name | Operator | Example |
Addition | + | a + b |
Subtraction | – | a – b |
Multiplication | * | a * b |
Division | / | a / b |
Mod | % | a % b |
Addition in C ( + )
Arithmetic addition operator is ‘+’. Arithmetic addition operator is used to perform basic mathematical addition operation between two operands. To find the sum of two numbers, use the additional operators. If there are three or more numbers in the addition, the first two are added first. The result of adding the first two numbers is used to multiply by the third number. The remainder of the numbers will be added in the same manner. To group values in the addition operator, no parentheses are required.
#include <stdio.h>
int main(int argc, char** argv) {
int a = 8;
int b = 2;
int c = 0;
c = a + b;
printf("a value is : %d\n", a);
printf("b value is : %d\n", b);
printf("\n");
printf("(a + b) is : %d\n", c);
return 0;
}
Output
a value is : 8
b value is : 2
(a + b) is : 10
Subtraction in C ( – )
Arithmetic subtraction operator is ‘-‘. Arithmetic subtraction operator is used to perform basic mathematical subtract operation between two operands. To subtract the second value from the first, use the subtraction operator. As a result, the first value will have a lower value. If the subtraction operator is used with three or more values, the second value is subtracted from the first. The third value will be deducted from the end outcome. The subtraction will be applied to the remaining values in the expression.
#include <stdio.h>
int main(int argc, char** argv) {
int a = 8;
int b = 2;
int c = 0;
c = a - b;
printf("a value is : %d\n", a);
printf("b value is : %d\n", b);
printf("\n");
printf("(a - b) is : %d\n", c);
return 0;
}
Output
a value is : 8
b value is : 2
(a - b) is : 6
Multiplication in C ( * )
Arithmetic multiplication operator is ‘*’. Arithmetic multiplication operator is used to perform basic mathematical multiply operation between two operands. The multiplication operator is used to multiply a given number multiple times. The multiplication will add the value for the number of times specified. If you specify three or more values in the multiplication time. The product of the first two numbers will be multiplied by the third value. The remaining values will continue to multiply from the previous multiplication result.
#include <stdio.h>
int main(int argc, char** argv) {
int a = 8;
int b = 2;
int c = 0;
c = a * b;
printf("a value is : %d\n", a);
printf("b value is : %d\n", b);
printf("\n");
printf("(a * b) is : %d\n", c);
return 0;
}
Output
a value is : 8
b value is : 2
(a * b) is : 16
Division in C ( / )
Arithmetic division operator is ‘/’. Arithmetic division operator is used to perform basic mathematical dividing operation between two operands. The division operator is used to divide a given number by the number of times it has been divided. The second number is used to divide the first number by the number of times it has been divided. The divided value of the first number to the second number is not equal to the divided value of the first number to the second number. The numbers in the division operator cannot be swapped. When you divide one integer number by another, the result is also an integer. The remaining portion will be truncated. When you divide one float number by another, the result is a float number with the remainder in the fraction value.
#include <stdio.h>
int main(int argc, char** argv) {
int a = 8;
int b = 2;
int c = 0;
c = a / b;
printf("a value is : %d\n", a);
printf("b value is : %d\n", b);
printf("\n");
printf("(a / b) is : %d\n", c);
return 0;
}
Output
a value is : 8
b value is : 2
(a / b) is : 4
Mod in C ( % )
Arithmetic mod operator is ‘%’. Arithmetic mod operator is used to perform basic mathematical mod operation between two operands which returns the remainder. The mod operator is not commonly used in conventional arithmetic operations. The second number is used to divide the first number by the number of times it has been divided. If any value remains after the division, it is referred to as the mod value. The remainder value of dividing the second number by the first number is the mod value. The mod value will always be less than the second value. If the first value is divisible by the second, the mod value is zero. Otherwise, the mod value is greater than zero and less than the second value.
#include <stdio.h>
int main(int argc, char** argv) {
int a = 8;
int b = 2;
int c = 0;
c = a % b;
printf("a value is : %d\n", a);
printf("b value is : %d\n", b);
printf("\n");
printf("(a % b) is : %d\n", c);
return 0;
}
Output
a value is : 8
b value is : 2
(a % b) is : 0