From the course: Advanced C Programming: Optimize Performance and Efficiency

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Obeying the order of precedence

Obeying the order of precedence

- [Instructor] You can call it a pecking order, and you've probably seen examples in various bits of code as well as in other movies in this course, but it's the order of precedence. It determines which operations take place first in a complex equation or expression. Open exercise file 05-08_order1. Here you see an equation at line 7. You can do the math in your head but will you get the same answer as the computer? So 5 plus 20 is 25, times 2 is 50, minus 8 is 42, divided by 2 is 21. So is the answer 21? Build and run the code. The computer generates 41 as the answer. The reason is simple. Certain operators have priority or precedence over others. In this case, if you look at line 7, multiplication and division have precedence over addition and subtraction. So 20 times 2 is 40, and 8 divided by 2 is 4 5 plus 40 is 45, minus 4 is 41. That's how the equation was evaluated in the C language. Obviously, knowing…

Contents