In this section, we will learn what the arithmetic operators are and how to use them in C.
Note: we’re assuming you already read the operators section.
What is Arithmetic Operator in C?
Arithmetic operators are those that we use for basic mathematical operations like addition, subtraction and others that you can see in the table below.
Arithmetic operators list in C
This is the list of arithmetic operations in C:
Operator | Symbol |
Addition Operator | + |
Subtraction Operator | – |
Multiplication Operator | * |
Division Operator | / |
Modulus Operator | % |
Increment Operator | ++ |
Decrement Operator | — |
Addition `+` Operator:
Using this operator, we can add together two values (constants or variables) and get the result.
Addition `+` Operator Syntax
LeftOperand + RightOperand;
Addition `+` Operator Example:
#include <stdio.h> int main() { int age = 55; int secondAge = 80; int totalAge = age + secondAge; return 0; }
In this example, we have `age`, `secondAge` and `totalAge` variables.
The values of `age` and `secondAge` are constant, but the value of `totalAge` variable is the result of the addition operation on the values of `age` and `secondAge`.
Note: as mentioned in operator section, the values involved in the arithmetic operation are called `operand`. So `age` and `secondAge` are the operands of the addition operation.
C Subtraction `-` Operator:
Using this operator, we can subtract two values (constants or variables) from each other and then return the result.
Subtraction `-` Operator Syntax:
LeftOperand - RightOperand;
Subtraction `-` Operator Example:
#include <stdio.h> int main() { int age = 55; int secondAge = 30; int difference = age - secondAge; return 0; }
In the example above, we’ve subtracted the values assigned to `width` and `height` variables and then assigned the result of this subtraction to the `difference` variable.
C Multiplication `*` operator:
Using this operator, we can multiply two values and return the result.
Multiplication `*` operator Syntax:
LeftOperand * RightOperand;
Multiplication `*` operator Example:
#include <stdio.h> int main() { int width = 55; int height = 30; int result = width * height; return 0; }
In the example above, we’ve multiplied the values assigned to the `width` and `height` variables and then assigned the result of this multiplication operation to `result` variable.
C Division `/` operator:
Using this operator, we can divide two values and return the result.
Division `/` operator Syntax:
LeftOperand / RightOperand;
Division `/` operator Example:
#include <stdio.h> int main() { int width = 55; int result = width / 2; return 0; }
In the example above, we’ve divided the value assigned to the `width` variable by constant 2 and then assigned the result of this multiplication to the `result` variable.
C Modulus `%` Operator:
How many 5 are in 13? If you think about it, there are 2!
Because if we multiply 5 to 3 we get 15 and that is greater than the value 13. So there’re only 2 packs of 5 is in the value 13. Now 2*5 is 10 and if we subtract 13 from 10 we get 3 and that’s the remainder.
Modulus is all about finding that remainder.
Modulus `%` Operator Syntax:
LeftOperand % RightOperand;
Modulus `%` Operator Example:
#include <stdio.h> int main() { int valueOne = 13; int valueTwo = 5; int result = valueOne % valueTwo; printf("Result: %d", result); return 0;
Output:
Result: 3
C Increment `++` Operator:
This operator only applies to `variables` not constants, and it’ll increase the value assigned to a variable by one.
This operator can sit on the left as well as the right side of a variable.
Increment `++` Operator Syntax:
++variable; Variable++;
Increment `++` Operator Example:
#include <stdio.h> int main() { int valueRight = 13; int valueLeft = 13; int left = ++valueLeft; int right = valueRight++; printf("left-variable: %d , right-variable: %d, valueRight: %d\n",left, right, valueRight); return 0; }
Output:
left-variable: 14 , right-variable: 13, valueRight: 14
Difference: if the `++` operator appears on the left side of a variable, it means increase the value of the variable by 1 before doing anything else and then proceed to the rest of instructions.
In the example above, the value assigned to `left` variable will be 14 because we set `++` to the left side of the `valueLeft` variable and this increased the value of `valueLeft` by 1 so it became 14. Now the value of `valueLeft` variable, which is 14, will be assigned to the `left` variable.
On the other hand, if the `++` operator appeared to the right side of a variable, it is only guaranteed that the next time that variable is called, its value will be increased by one. This means, when we put the `++` operator on the right side of a variable, the value of that variable won’t increase right at that moment.
In the example above, the value assigned to `right` variable will not be 14 because we set `++` to the right side of the `valueRight` variable and this will not increase the value of `valueRight` right at that moment. That’s why we got the value 13 for the `right` variable.
But the next time we called the `valueRight` variable, the value of this variable turned into 14.
C Decrement `–` Operator:
This operator only applies to `variables` and not constants, and it’ll decrease the value assigned to a variable by one.
This operator can sit on the left as well as the right side of a variable.
Note: the same rules that applied to `++` operator apply to `–` operator as well.
Decrement `–` Operator Syntax:
--variable; variable--;
Decrement `–` Operator Example:
#include <stdio.h> int main() { int valueRight = 13; int valueLeft = 13; int left = --valueLeft; int right = valueRight--; printf("left-variable: %d , right-variable: %d, valueRight: %d\n",left, right, valueRight); return 0; }
Output:
left-variable: 12 , right-variable: 13, valueRight: 12
Note: The basic arithmetic operations mentioned in this section apply to operands from left to right.
For example, if we have `int res = 10/2; ` the value 10 is divided by 2 and not vice versa!