Check The Programming Section
Arithmetic operators are used to perform arithmetic operations between the variables and constants. The arithmetic operators listed in Table can be applied to almost any built-in type.
The operator +, -, * and / works with any built-in type. When the division operator / (backslash), is applied between two integers or characters, any reminder will be truncated. Consider the example below:
res = 15/4;
The actual division result between 15 and 4 is 3.75. But 15 and 4 both are integers, so the reminder part will be truncated and 3 will be assigned as the value of res. It is true for division between two characters also. Consider the next example:
char ch1 = 'Z';
char ch2 = 'A';
char res = ch1/ch2;
When we perform division, between two characters, actually the compiler considers the ASCII values of the characters to perform the division operation. For the above division operation (ch1/ch2), the ch1 and ch2 will be replaced first by its value 'Z' and 'A'. Thereafter, the ASCII value 90 and 65 for Z and A respectively is considered during division. And the similar kind of treatment will be given, any reminder part will be truncated.
NOTE: It is not possible to divide any number by zero. This is an illegal operation that results in a run-time divison-by-zero exception and program execution will be terminated.
The modulus operator % returns the remainder of an integer division. Use of the modulus operator between floating-points is not allowed. The following code snippets illustrates %:
int x, y;
x = 7;
y = 3;
printf("%d", x/y);
printf("%d", x%y);
The first printf() statement prints 1, because 7/3 is an integer division and x%y inside the second printf() statement prints 1 as reminder.
The addition (+), subtraction (-), and multiplication (*) operators can accept a mix of integeres and floating-point numbers, with the following conditions:
If both the operands are integer then the result of above memntioned operation will be an integer value.
If one or both operands are floating-point numbers, the result will be a floating-point number.
Consider the following code example (edit and try online):
#include<cstdio>
using namespace std;
int main(){
int a = 9, b = 3;
printf("%d\n",a+b);//it prints 12
printf("%d\n",a-b);//it prints 6
printf("%d",a*b);//it prints 27
}
For the above sample code, at first value of a get added with b (a+b) and prints 12. Similarly, for the second printf() statement value of b get subtracted from a (a-b) and prints 6. Atlast, value of a and b gets multiplied (a*b) to each other and prints 27 as output.
The unary minus multiplies its operand by -1. That is, any number preceded by a minus sign switches its sign. A positive number becomes negative and a negative become a positive number. Consider the below example:
int a = 90, b;
b = -a;
printf("%d %d",a, b);
The unary minus operator (-) before operand a multiplies its value by -1 and assigned to b. The output of the printf() statement is 90 for a and -90 for b. The statement b = -a; is also equivalent to the follwoing statement.
b = a * (-1);
Note: The difference between unary minus and subtract operator is, unary minus operator requires one operand. On the other side subtract (-) operator requires two operands.
C/C++ includes two useful operators to increment and decrement a value of a operand by 1. The increment operator (++) adds 1 to its operand and decrement operator (--) subtracts 1. In other words:
x = x + 1;
is same as
x++;
and the statement
x = x - 1;
is same as
x--;
Both the increment and decrement operator either precede (prefix) or follow (postfix) the operand. For example,
x = x + 1;
can be written as
x++;
or
++x;
Similar to this, the statement x = x -1; can be written as
x--;
or
--x;
The behavior of the prefix and postfix form of the increment (++) and decrement (- -) operator is different when we use these operators in an expression.
An example of prefix
int a = 10;
int b = ++a;
int c = --a;
For the above example, value of operand a is incremented first by 1 and then assigned to b. For the next statement value of operand a is decrement first by 1 and then assigned to operand c.
An example of postfix
int a = 10;
int b = a++;
int c = a--;
In postfix form, value of the operand a will assigned first to operand b and then incerment or decremented by 1.
The C/C++ compilers produce very fast, efficient object code for increment and decrement operations and the generated code is better than that generated by using the equivalent assignment statement. For this reason, you should use the increment and decrement operators when you can.
Operator precedence determines which operator is performed first in an expression with more than one operators with different precedence. Consider the example below:
Operators Associativity is used when two operators of same precedence appear in an expression. Associativity can be either Left to Right or Right to Left.
To understand the precedence and associativity consider the example below:
int k = 4 * 5 + 10 - 2;
Before evaluate the above arithmetic expression, compiler considers the associativity rules for the operators on the same level. In the above expression addition (+) and subtract (-) operator are in same precedence level. At first multiplication operation (4 * 5) gets executed first by the compiler (higher precedence). Consider the below evaluation process:
k = 20 + 10 -2;
Now compiler considers left to right (associativity) and addition gets calculated before subtraction operation as follows:
k = 30 - 2;
At last, value of k is
k = 28;
Further arithmetic operators can partitioned into two groups as:
Arithmetic operators +, -, %, *, / requires two operands to perform any arithmetic operation and considered as binary arithmetic operators.
Arithmetic operator's unary minus (-), increment (++), and decrement (--) works over one operand and considered as unary arithmetic operators.
Unary operators (+, -, ++, --) have higher precedence than most binary operators, and their associativity is right-to-left. See the example given below:
#include <stdio.h>
int main() {
int a = 5, b = 10, c = -20;
int result = ++a + b-- - -c;
printf("Result: %d, b: %d\n", result, b);
return 0;
}
Let's evaluate the expression ++a + b-- - -c step by step, respecting precedence and associativity.
Step 1: ++a (Prefix Increment)
Precedence: Highest among all operators.
Associativity: Right-to-left.
Operation: Increment a by 1 before using its value in the expression.
a = 5 becomes a = 6.
The value 6 is used in the expression.
Updated variables
a = 6, b = 10, c = -20
Step 2: b-- (Postfix Decrement)
Precedence: Equal to ++, but postfix associativity is left-to-right.
Operation: Use the current value of b in the expression, then decrement b by 1.
Value used in the expression: 10.
b is decremented after its use, so b = 9.
Updated variables:
a = 6, b = 9, c = -20
Step 3: -(-c) (Unary Negation)
Precedence: Unary negation - has higher precedence than addition/subtraction.
Associativity: Right-to-left.
Operation: Negate c, which is -20, making it 20.
Updated variables:
a = 6, b = 9, c = -20
Step 4: Evaluate the Arithmetic
After resolving all unary operators, the expression becomes:
result = 6 + 10 - 20;
Now evaluate the arithmetic, following the left-to-right associativity for addition (+) and subtraction (-):
6 + 10 = 16
16 - 20 = -4
Final value of result:
result = -4