Arithmetic Operators in C
C language provides five main arithmetic operators which include the addition (+), subtraction (-), multiplication (*) and division (/) operator. These operators are used to perform arithmetic operations on numeric values. All these operators perform the function what is signified by their name. In addition there is a modulus operator (%) which gives the remainder left over from a division operation. For exponentiation there is no specific operator in C. Operands can be constants or variables containing integer quantities, floating-point quantities or characters. However, the modulus operator requires that both operands be integers and the second operand be non-zero. Similarly, the division operator (/) requires that the second operand be non-zero, though the operands need not be integers. Division of one integer quantity by another is referred to as integer division. With this division the decimal portion of the quotient will be dropped. An arithmetic operator along with its operand(s) gives rise to Arithmetic Expression. The output of an arithmetic expression is a numeric value decided by the nature of operation. Program 1 shows these operators in action which is followed by the screenshot of the output.
#include <stdio.h>
void main()
{
int i, j, p, q, r;
i = 12 + 3;
j = 12 – 3;
p = i / j;
q = i * j;
r = p % q;
printf(“12 + 3 is %d\n”,i);
printf(“12 – 3 is %d\n”,j);
printf(“i / j is %d\n”,p);
printf(“i * j is %d\n”,q);
printf(“p %% q is %d\n”,r);
getch();
}
Program 1: Program shows binary arithmetic operators in action
C also provides two unusual arithmetic operators called increment (++) and decrement (–) operators. The operator ++ adds 1 to the operand while –- subtracts 1. It should be noted that all arithmetic operators are binary operators, however these 2 operators (++ and –) are unary (Note that addition [+] and subtraction [-] are both binary as well as unary operators). This means, they only operate on one operand. However, the operand can only be a variable but no constant. To understand the working of these operators, let’s consider an arithmetic expression in which ++ operator is used on some variable x. As such, the value of x after operation will be 1 greater than that what was before the operation. Therefore, the two statements shown below will have same affect individually.
X ++;
X = X + 1;
However, both unary operators take two forms as shown below.
X ++; or ++ X;
X –; or –-X;
Though ++X and X++ mean same thing, they behave differently when they are used in an expression wherein their value is further processed. Specifically, ++X means increment the value of X by 1 and then substitute the expression ++X by that value. In contrast, X++ means substitute the expression by the current value of X and then increment the value by 1. Similarly postfix and prefix decrement operators behave. Program 2 shows the difference in postfix and prefix operators followed by screenshot of the output.
#include <stdio.h>
void main()
{
int i, j, p, q, r;
i = 12;
j = i++; // Postfix Increment
p = ++j; // Prefix Increment
q = j–; // Postfix Decrement
r = –q; // Prefix Decrement
printf(“The value of i is %d\n”,i);
printf(“The value of j is %d\n”,j);
printf(“The value of p is %d\n”,p);
printf(“The value of q is %d\n”,q);
printf(“The value of r is %d\n”,r);
}
Program 2: Program shows postfix and prefix operators in action