Arithmetic operators are used to perform arithmetic operations on arithmetic operands, i. e., operands of integral as well as floating type. Recall that an integral type includes all forms of char and int types, whereas the floating-point types include the float, double and long double types. These operations include addition (+), subtraction (- ), multiplication (*), division (!), modulo arithmetic (%), increment (++), decrement (– ), unary plus (+) and unary minus (- ). They can be grouped into three categories: unary operators, multiplicative operators and additive operators. The arithmetic operators are summarized in Table.
We’ll be covering the following topics in this tutorial:
Additive and Multiplicative Operators
The additive operators include addition (+) and subtraction (-) operators. They are binary infix operators that operate on two arithmetic operands, as in a + b and a – b.
The multiplicative operators include three operators: multiplication (*),division (/) and modulo arithmetic (%), i. e., remainder after integer division. These are binary infix operators that operate on two arithmetic operands, as in a * b, a I b and a %b.
The multiplication and division operators operate on any arithmetic operands-both integral and floating-type. As the multiplication symbol is not explicitly written in mathematical expressions, we tend to forget it in C expressions. However, remember that the multiplication operator must be explicitly written in a C expression. Thus, the mathematical expression abc should be written in C language as a * b * c.
Arithmetic operators in the C language
Right to left
Left to right
Left to right
The result of the division operator(/) depends on the type of operands. If both the operands are integral, the result is truncated to the integer value, e. g., 7 I 4 evaluates as 1. However, if either or both operands are floating-type, the result is also floating-type, e. g., the expressions 7.0/4, 7/4.0 and 7.0/4.0 evaluate as 1.75. The modulo arithmetic operator works only with integral operands such as the various int and char types. It gives the remainder after integer division, e.g., 7 % s evaluates as 2, whereas 13 % s evaluates as 3.
The C language does not provide exponentiation operator available in programming languages like FORTRAN and BASIC. Quantities involving small powers such as a2 and a3 are represented using direct multiplication, as in a*a and a*a*a, respectively. The expression xY is written using standard library function pow (for power) as pow (x, y).
Unary and Arithmetic Operators
The unary arithmetic operators include unary plus (+),unary minus (- ), increment (++) and decrement (–). These operators operate on arithmetic operands. Unary plus and unary minus are prefix operators, e.g., -a, +10. 25, etc. The unary minus operator negates the value of its operand, whereas the unary plus operator has no effect on the value of its operand. Note that the symbols for the unary plus and unary minus operators are the same as those of the addition and subtraction operators, respectively.
The increment operator (++) increments the value of the operand by 1, while the decrement operator (– ) decrements the value of the operand by 1. These operators must be written without a space between the two ‘+’ or’-‘ symbols. They can only be used with modifiable lvalues (such as variables) and not with constants and expressions.
The increment and decrement operators can be used either in prefix form, as in ++x, or in postfix form, as in x++. Although both the forms modify the value of the operand by 1, there is a subtle difference. In the prefix form, the value of the operand is first incremented and then used, whereas in the postfix form, the value of operand is first used and then incremented.
The associativity of operators decides the order in which operators at the same precedence level are bound to operands. The unary and assignment operators have right-to left associativity. If an expression contains more than one unary operator, they are bound to operands from right to left. Remember that only unary operators, assignment operators and conditional operator (?:) have right-to-left associativity. All other operators in C language have left-to-right associativity.
The multiplicative operators have left-to-right associativity. They are bound to operands in the order of their occurrence in an expression from left to right. For example, the expression a*b/c*d is interpreted as ( (a*b) /c) *d, i.e., multiplication of a and b is performed first followed by division and second multiplication. Similarly, the additive operators also have left-to-right associativity.