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 standard arithmetic operators such as addition, subtraction, multiplication, and division. The following 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 accept two operands 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 only to floating-point numbers such as 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, it 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:
++variable_name;
Code language: C++ (cpp)
In the postfix form, you place the ++ after the operator as follows:
variable_name++;
Code language: C++ (cpp)
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 operators:
#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 difference between the postfix and prefix is the order of using and incrementing the value. It’s more evident 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 assigns the result to y.
int y = x++;
Code language: C++ (cpp)
In this example, the increment operator assigns x to y and adds one to x.
The following program illustrates the difference between pre-increment and post-increment 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:
--variable_name;
Code language: C++ (cpp)
and
variable_name--;
Code language: C++ (cpp)
The decrement operator subtracts one from the variable and returns the result, while the post-decrement operator returns the variable’s value before subtracting one from it.
The following program illustrates the decrement operators and the difference between the pre-decrement and post-decrement 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 afterward. The result will be 30.
The precedences of the arithmetic operators from high to low:
Operator | Precedence |
---|---|
++, — ( both prefix and 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 (%) is only relevant to integers.
- The increment/decrement operators add to / subtract one from an operand. They support both prefix and postfix forms.