There are five arithmetic operators, +, -, *, I, and %, which respectively represent the processes of addition, subtraction, multiplication, division, and modulus. The modulus operator (%) gives the remainder when one integer is divided by another integer. All of the five operators have been described with examples of codes in Table.
It should be noted that each of these operators has a precedence level, that is, priority in operation. If an expression contains operation of multiplication and addition, the multiplication is performed first because it has higher precedence level than that of the addition. Table shows the precedence level of different operators. In many arithmetic operations, the parentheses () are also used. It is generally employed to change the precedence level, that is, priority in operation. The precedence determines when a particular operator will be implemented when there are several operators in a single formula.
The step-by-step operation of a computer on a calculation is illustrated below.
A= 10/2 - 3%2 + (3 + 2) * 5
= 10/2 - 3%2 + 5 * 5
= 5 - 1+25
= 4 + 25 = 29
If there are several arithmetic operators of same priority in a line, the operations are done from left to right. This is called associativity. However, the assignment represented by (=) is last to be implemented, and it is implemented from right to left. In the above operation 29 is assigned to A.
The characters are integral constants. Similar to other constants, characters may also be added or subtracted. Program illustrates some arithmetic operations on character variables.
char ch1,ch2, ch3, ch4,ch5,ch6, ch7 ;
/* chl to ch8 are simply names of variable. Their type is char*/
ch1 = 'A'; /*The value of character A in ASCII code is 65.*/
ch2 = ch1; // assigning value of one to another.
ch3 = ch1 +5 ;
ch4 = ch3 +'2';//here '2' is a character
ch5 = ch3 - 2;
ch7 = '5' + '6';
printf("ch2 = %c,\t ch3 = %c\n", ch2, ch3 );
printf("ch4 = %c,\t ch5 = %c\n", ch4, ch5 );
printf("ch6 = %c,\t ch7 = %c\n", ch6, ch7 );