Summary: in this tutorial, you’ll learn about the arithmetic operators in C for performing arithmetic calculations.
Introduction to the C arithmetic operators
C supports common arithmetic operators such as addition, subtraction, multiplication, and division. The folllowing table illustrates some arithmetic operators in C:
Arithmetic Operators | Operation | Example |
---|---|---|
+ | Addition | 5 + 3 = 8 |
– | Subtraction | 5 – 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 5 / 3 = 1 (for integer) |
% | Modulo (return the remainder of a division of two integers) | 5 % 3 = 2 |
All arithmetic operators above accepts two operand and return the result.
The following example uses the above arithmetic operators on integers:
#include <stdio.h>
int main()
{
int x = 5, y = 3;
printf("%d + %d = %d\n", x, y, x + y);
printf("%d - %d = %d\n", x, y, x - y);
printf("%d * %d = %d\n", x, y, x * y);
printf("%d / %d = %d\n", x, y, x / y);
printf("%d %% %d = %d\n", x, y, x % y);
return 0;
}
Code language: C++ (cpp)
Output:
5 + 3 = 8
5 - 3 = 2
5 * 3 = 15
5 / 3 = 1
5 % 3 = 2
Code language: C++ (cpp)
It’s important to note that the % operator is relevant to the floating-point numbers only suchas float, double, and long double.
The followning example applies the arithmetic operators to floats:
#include <stdio.h>
int main()
{
float x = 5, y = 3;
printf("%f + %f = %f\n", x, y, x + y);
printf("%f - %f = %f\n", x, y, x - y);
printf("%f * %f = %f\n", x, y, x * y);
printf("%f / %f = %f\n", x, y, x / y);
return 0;
}
Code language: C++ (cpp)
Output:
5.000000 + 3.000000 = 8.000000
5.000000 - 3.000000 = 2.000000
5.000000 * 3.000000 = 15.000000
5.000000 / 3.000000 = 1.666667
Code language: C++ (cpp)
Increment operators in C
The increment operator adds one to an operand. Unlike the +, -, *, /, and % operators, the increment operator accepts one operand.
The increment operator has two forms: prefix and postfix:
In the prefix form, you place the ++ in front of an operand like this:
Code language: C++ (cpp)++variable_name;
And in the postfix form, you place the ++ after the operator as follows:
Code language: C++ (cpp)variable_name++;
Both forms add one to the variable_name
. And they’re equivalent to the following:
variable_name = variable_name + 1;
Code language: C++ (cpp)
The ++variable_name is called a pre-increment and variable_name++ is called post-increment.
The following program illustrates how to use the increment opertors:
#include <stdio.h>
int main()
{
int x = 0, y = 1;
// prefix
++x; // add 1 to x
printf("%d\n", x); // 1
// postfix
y++; // add 1 to y
printf("%d\n", y); // 2
return 0;
}
Code language: C++ (cpp)
Output:
1
2
Code language: C++ (cpp)
The difference between ++x and x++
The different between the postfix and prefix is the order of using the value and increment it. It’s more obvious if you use the preincrement and post-increment in an expression.
int y = ++x;
Code language: C++ (cpp)
In this example, the increment operator adds one to x and assign the result to y.
int y = x++;
Code language: C++ (cpp)
In this example, the increment operator assign x to y and add one to x.
The following program illustrates the difference between preincrement and postincrement operators:
#include <stdio.h>
int main()
{
int x, y;
// prefix
x = 1;
y = ++x;
printf("y = %d\n", y); // 2
printf("x = %d\n", x); // 2
// postfix
x = 1;
y = x++;
printf("y = %d\n", y); // 1
printf("x = %d\n", x); // 2
return 0;
}
Code language: C++ (cpp)
Output:
y = 2
x = 2
y = 1
x = 2
Code language: C++ (cpp)
Decrement operators in C
Like the increment operators, C also has decrement operators in both prefix and postfix forms:
Code language: C++ (cpp)--variable_name;
and
Code language: C++ (cpp)variable_name--;
The predecrement operator subtract one from the variable and returns the result while the postdecrement operator returns the value of the variable before subtracting one from it.
The following program illustrates the decrement operators and the difference between the predecrement and postdecrement operators:
#include <stdio.h>
int main()
{
int x, y;
// prefix
x = 5;
y = --x;
printf("y = %d\n", y); // 4
printf("x = %d\n", x); // 4
// postfix
x = 5;
y = x--;
printf("y = %d\n", y); // 5
printf("x = %d\n", x); // 4
return 0;
}
Code language: C++ (cpp)
C Arithmetic operator’s precedence and associativity
Let’s discuss the arithmetic operators’ precedence and associativity.
Arithmetic operator precedence
When you use multiple arithmetic operators in an expression, C evaluates the expression based on the order of precedence of the operators. It works like Math. For example:
5 + 2 * 3
Code language: C++ (cpp)
C will evaluate the multiplication operator before the addition operator. The result will be 11.
To change the order of evaluation, you use parentheses like this:
(5 + 2) * 3
Code language: C++ (cpp)
Now, C evaluates the addition operator first due to the parentheses and the multiplication operator after that. The result will be 30.
The precedences of the arithmetic operators from high to low:
Operator | Precedence |
---|---|
++, — ( both prefix, suffix forms) | 3 |
*, /, % | 2 |
+,- | 1 |
Arithmetic operator associativity
The associativity of an operator determines how C groups the operators of the same precedence when the expression doesn’t have parentheses.
The arithmetic operators such as addition, subtraction, multiplication, and division are left-associative. It means that C group operations from the left.
Summary
- The arithmetic operators such as +, -, *, /, and % take two operands and return the result.
- The modulo operator (%) only relevant to integers.
- The increment / decrement operators add one to / subtract one from an operand. They support both prefix and postfix forms.