Printf ("6 + a / 5 * b = %i\n", 6 + a / 5 * b) Įxtra blank spaces are inserted between int and the declaration of a, b, c, and d in the first four statements to align the declaration of each variable. Program 3.3 More Examples with Arithmetic Operators // More arithmetic expressions Program 3.3 reinforces what you just learned and introduces the concept of integer arithmetic. Integer Arithmetic and the Unary Minus Operator The result of 300 is handed to the printf() routine. Is evaluated according to the rules stated previously as (a * b) + (c * d) You will notice from the last statement in Program 3.2 that it is perfectly valid to give an expression as an argument to printf() without having to first assign the result of the expression evaluation to a variable. Just be certain you have as many closed parentheses as you have open ones. Parentheses can also be nested, in which case evaluation of the expression proceeds outward from the innermost set of parentheses. Were used instead, the value assigned to result would be 2550 because the value of a ( 100) would be added to the value of b ( 2) before multiplication by the value of c ( 25) would take place. However, if the expression result = (a + b) * c Thus, the statement result = a + (b * c) Ĭould have been substituted in Program 3.2 to achieve identical results. In fact, the expression listed previously is a perfectly valid C expression. If you want to alter the order of evaluation of terms inside an expression, you can use parentheses. (This is the same way this expression would be evaluated if you were to apply the basic rules of algebra.) Therefore, the expression a + b * cīy the C programming language. However, the operations of multiplication and division are given precedence over the operations of addition and subtraction. Evaluation of an expression generally proceeds from left to right. This is because C, like most other programming languages, has rules for the order of evaluating multiple operations or terms in an expression. If it is determined that the divisor is zero, an appropriate action can be taken and the division operation can be averted.ĭoes not produce the result of 2550 (102 × 25) rather, the result as displayed by the corresponding printf() statement is shown as 150. In Chapter 5, you see how you can check for division by zero before the division operation is performed. 2 Even if the program does not terminate abnormally, the results obtained by such a division will be meaningless. On some computer systems, attempting to divide a number by zero results in abnormal termination of the program. The result of 4, as obtained by dividing 100 by 25, is displayed by the printf() statement immediately following the division of a by c. The next program statement introduces the division operator-the slash. The result of the multiplication is then displayed using a printf() call that should be familiar to you by now. Has the effect of multiplying the value of b by the value of c and storing the product in result. Printf ("a * b + c * d = %i\n", a * b + c * d) Īfter declaring the integer variables a, b, c, d, and result, the program assigns the result of subtracting b from a to result and then displays its value with an appropriate printf() call. Program 3.2 Using the Arithmetic Operators // Illustrate the use of various arithmetic operators Appendix A provides a complete list of operator precedences and their rules of association. This is known as the associative property of an operator. Expressions containing operators of the same precedence are evaluated either from left to right or from right to left, depending on the operator. This precedence is used to determine how an expression that has more than one operator is evaluated: The operator with the higher precedence is evaluated first. In fact, each operator in C has a precedence associated with it. The last two operations performed in the program introduce the notion that one operator can have a higher priority, or precedence, over another operator. Program 3.2 further illustrates the operations of subtraction, multiplication, and division. You have seen how a simple operation such as addition can be performed in C. These operators are known as binary arithmetic operators because they operate on two values or terms.
C programming modulus operator floating point exception plus#
In C, just as in virtually all programming languages, the plus sign ( +) is used to add two values, the minus sign ( −) is used to subtract two values, the asterisk ( *) is used to multiply two values, and the slash ( /) is used to divide two values. Learn More Buy Working with Arithmetic Expressions