Physics and Astronomy |

Back to top
On this page
Contents ## Curing bracketitusExperience shows that some students have a habit of putting brackets everywhere, even when they are not needed: a = (b) + (c*d); // Don't do this x = (y) + (2 * (y*z)); // or this presumably because they aren't quite sure whether they need brackets or not. This is a bad idea because: - The extra brackets are confusing
- It makes the markers think you do not understand either C or basic arithmetic
## Mathematical precedenceThe "mathematical" operators * / % + - have exactly the same effect as in ordinary maths, with % being treated on the same basis as * and /, that is to say: - * / % bind more closely that + and -:
2 * 3 + 1 => 7 not 4 2 + 5 % 3 => 4 not 1 - Operators with the same precedence are evaluated from left to right:
12 / 3 * 2 => 8 not 2
## No implicit multiplicationIn maths we often use implicit multiplication with single-character variable names: s = ut + 0.5at expands to: s = u*t + 0.5*a*t*t Once we understand the implicit multiplication convention we see that C's treatment of */+- is exactly the same as we have been used to. |