Understanding Precedence

As you were probably taught when you first learned arithmetic, operators have a precedence: rules dictating in what order calculations are made. These are important to understand because they can dramatically affect the end result.

Table 3.2 lists the order of precedence in C for the arithmetic operators, but it's probably easiest just to follow these three rules:

1.
Multiplication and division take place before addition and subtraction.

2.
Operators with the same precedence level are executed from left to right.

3.
Use parentheses to guarantee your results.

Table 3.2. The precedence of operators from highest to lowest (those listed first will be executed first).
Rules of Precedence
Operator
()
++, --
+, - (Sign operators)
*, /
+, - (Addition and subtraction)
=, +=, -=, *=, /=, %=

This last rule is really the most important; if you'd prefer, you can specify all of your arithmetic intentions using parentheses. The second rule means that, for example, if multiplication and division both take place and their order is not mandated by precedence, they'll be executed from left to right.

int a;
a = 10 * 2 / 5; /* 4 */
a = 10 / 2 * 5; /* 25 */

To show how precedence affects calculations, let's rewrite the temperature conversion example from Chapter 2.

To demonstrate precedence

1.
Create a new C source file (Script 3.3):

/* temperature2.c - Script 3.3 -
 remake of Script 2.6
 (temperature.c) */
#include <stdio.h>
int main(void) {

Script 3.3. You con control the order in which arithmetic occurs by using parentheses or mastering the rules of precedence.


2.
Declare the required variables:

float temp_f, temp_c;

This application will use two variables, both of which are floats.

3.
Set the temperature in Celsius and convert this to Fahrenheit:

temp_c = 32.0;
temp_f = temp_c * 9.0/5.0 + 32;

First, some random value is assigned to the temp_c variable. Because that variable is a float type, a decimal point and zero are added as well.

The formula for making this conversion is to take the temperature in Celsius, multiply it by the result of dividing 9.0 by 5.0 (which is 1.8), and then add 32. Using parentheses to be explicit, this could be written as

(temp_c * (9.0/5.0)) + 32

However, since multiplication and division automatically take place before addition, it can be simplified by removing the parentheses without affecting its accuracy.

4.
Print out the results:

printf ("%0.1f degrees Celsius is 
 %0.1f degrees Fahrenheit.
",
 temp_c, temp_f);

Both the temperature in Celsius and the calculated Fahrenheit will be printed here. Each is formatted to one decimal point (%01.f).

5.
Complete the main() function:

     getchar();
     return 0;
}

6.
Save the file as temperature2.c.

7.
Compile and debug the code.

8.
Execute the application (Figure 3.5).

Figure 3.5. The first conversion of a temperature.


9.
If desired, change the value of temp_c in the code, recompile, and re-execute the application (Figure 3.6).

Figure 3.6. Another temperature conversion.


✓ Tips

  • Technically, since multiplication and division have the same precedence and are executed from left to right, the temperature conversion formula is first multiplying temp_c times 9.0 and then dividing that by 5.0 (as opposed to dividing 9.0 by 5.0 and then multiplying that times temp_c). Due to the nature of multiplication and division, this does not affect the end result.

  • The conversion formula would not actually work if it were written as temp_c * (9/5) + 32. The division of 9 by 5 would take place, resulting in a value of 1 (because the remainder is dropped when dividing one integer by another). This value (1) would then be multiplied by temp_c. Conversely, the formula temp_c * 9/5 + 32 actually would work, because the float temp_c would first be multiplied by 9 and this resulting float would then be divided by 5, without ever losing a remainder. Rather than confusing yourself with these tedious rules, if you always use floats in division and incorporate parentheses, your arithmetic will stay correct.

  • In Chapter 5, “Standard Input and Output,” you'll see how to take numbers as keyboard input on which calculations will be made.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset