Arithmetic Operators in C++ are the symbols that are used to perform mathematical calculations. Also, these operators are used for incrementing and decrementing the values of the variables and can perform unary operations. In this article, we will discuss what the arithmetic operators are in C++, their types, syntax, precedence, associativity, and examples in C++ programming.
Table of Contents:
What are Arithmetic Operators in C++?
Arithmetic operators in C++ are the operators used to perform basic arithmetic operations on variables or values. They perform calculations such as addition(+), subtraction(-), multiplication(*), division(/), and modulus(%).
Syntax of Arithmetic Operators in C++
variable = value1 arithmetic_operator value2;
Where:
- value1 and value2 are the numerical values or variables.
- arithmetic_operator is one of the arithmetic operators.
- and variable stores the calculated value.
Types of Arithmetic Operators in C++
Arithmetic operators in C++ are classified into three categories based on the operations they perform.
1. Basic Arithmetic Operators
These are the arithmetic operators in C++ used to perform basic mathematical calculations.
Operator Name |
Symbol |
Description |
Syntax |
Addition |
+ |
Adds two numbers |
a + b |
Subtraction |
– |
Subtracts the second from the first |
a – b |
Multiplication |
* |
Multiply two numbers |
a * b |
Division |
/ |
Divides first by second (quotient) |
a / b |
Modulus |
% |
Returns the remainder of the division |
a % b |
2. Unary Arithmetic Operators
The unary arithmetic operators are the operators that work on a single operand. They take only one value and perform operations on it.
Operator Name |
Symbol |
Description |
Syntax |
Unary Plus |
+ |
Represents a positive value (has no effect) |
+a |
Unary Minus |
– |
Negates the value (makes positive negative and vice versa) |
-a |
3. Increment and Decrement Operators
These arithmetic operators are used to increase and decrease the value of a variable by 1.
Operator Name |
Symbol |
Description |
Syntax |
Pre-Increment |
++a |
Increases value by 1 before using it |
++a |
Post-Increment |
a++ |
Uses value first, then increases by 1 |
a++ |
Pre-Decrement |
–a |
Decreases the value by 1 before using it |
–a |
Post-Decrement |
a– |
Uses value first, then decreases by 1 |
a– |
Basic Arithmetic Operators
Let’s discuss each of the basic arithmetic operators in brief, with examples in C++.
1. Addition (+)
The addition operator in C++ adds two numbers or operands.
Example:
Output:
The code shows that the addition operator adds two integers, a and b, with values 16 and 5, and prints the result 21 as an output to the console.
2. Subtraction(-)
The subtraction operator in C++ subtracts the second number from the first number.
Example:
Output:
The code shows how the subtraction operator is used to subtract two integers, a = 8 and b =3, and then the result 5 is printed as an output to the console.
3. Multiplication (*)
The multiplication operator in C++ multiplies two numbers.
Example:
Output:
The code shows how the multiplication operator multiplies two integers, a and b, with values 8 and 5, and then the result 40 is printed to the console.
4. Division (/)
The division operator in C++ divides the first number by the second number and gives the quotient as a result.
Example:
Output:
The code shows how the division operator is used to divide the integer a = 20 by the integer b = 4, and then the result 5 is printed to the console.
5. Modulus (%) Operator
The modulus operator in C++ returns the remainder from the division of two integers. It only works with integer data types.
Example:
Output:
The code shows how the modulus operator is used to get the remainder 2 as a result from the division of two integers a = 20 and b = 3.
Precedence and Associativity of Arithmetic Operators in C++
The precedence checks the order of the operations performed by the operator, and the associativity of an operator checks the order in which that operator is evaluated.
Operator |
Operator Type |
Precedence |
Associativity |
+ |
Unary Plus |
Highest |
Right to Left |
– |
Unary Minus |
Highest |
Right to Left |
* |
Multiplication |
Highest |
Left to Right |
/ |
Division |
High |
Left to Right |
% |
Modulus |
High |
Left to Right |
+ |
Addition |
Low |
Left to Right |
– |
Subtraction |
Lowest |
Left to Right |
C++ Example of Unary Arithmetic Operators
Output:
The code shows how the unary plus operator does not change the value of a and keeps it as it is, and the unary minus operator negates the value of a.
C++ Example of Increment and Decrement Operators
Output:
The code shows how ++a and –a first modify the values and then assign them, and a++ and a– first assign the value, then modify it. The results of all the operations are then printed to the console.
Conclusion
Arithmetic operators in C++ are important for performing various types of mathematical operations, such as addition, subtraction, etc. These operators can also be used to increment and decrement the value of the variables. Also, they have precedence and associativity order. So, by understanding how these operators work and what their precedence and associativity orders are, you can easily perform arithmetic calculations in C++ programming.
FAQs
Q1. What happens if I divide a number by zero?
If you divide a number by zero, then it will cause a runtime error.
Q2. Can modulus be used with floating-point numbers?
No, the modulus operator cannot be used with floating-point numbers. So, you can use fmod() from <cmath> for the floating points.
Q3. What is the difference between a++ and ++a?
The difference between a++ and ++a is that a++ returns the value first, then increments, and ++a increments first.
Q4. Why does integer division discard decimals?
Because integer division keeps only the whole number.
Q5. How do you change operator precedence?
To override the operator precedence, use parentheses ().