Hello and welcome. Now that we've talked about all the main operators,
the fundamental ones, the weird ones, we have to talk about
this little concept of operator precedence.
So this comes into play basically when how do you evaluate the order of these operators.
So operator precedence determines the grouping of the terms and expression
and decides how an expression is evaluated.
Now you really don't need to understand any of this
if you use parentheses which we'll get to in a second, but you have to understand this concept
because it's part of the language it's part of understanding the language.
So precedence is going to dictate the order of evaluation
when two operators share an operand.
And this is going to become clear with an example.
So for example, the multiplication operator has a higher precedence
than the additional operator.
Certain operators are going to have different order of evaluations. So if you were to do a
compound statement
where you're performing many different calculations, for example, 7 plus 3 times 2.
This can have multiple results,
the addition and you're doing the multiplication.
You need to understand do I need to do the addition first or should I do the multiplication first because
depending on what you do first changes the result.
So for example, this x equals 7 plus 3 times 2,
the result can either be 13 or 20 depending on the order
of when you evaluate each operand.
So the order of the executing the various operations can make a huge difference.
And this is why you need to understand precedence.
C provides specific rules for choosing what to do first.
In this case, do I do the multiplication of plus. Well, c is going to tell you what's going to happen.
Ideally if you wrap those in parentheses, you would be telling it
what to do, you would be making that decision. But if you don't put that in parentheses,
the programming language is going to make the decision for you.
So in c, multiplication is going to have a higher precedence
than the addition operator. So what's going to happen is the result is going to be 13.
Now you could either say 7 plus 3,
which is 10 and then multiply by 2, the result would be 20.
Or you can do 7 plus 3 times 2, which is 7 plus 6 which is 13.
So that's how you can have different values.
That's ambiguous. Do you do the plus first or do you do multiplication first.
Luckily, again, that's decided by the language. But you can see how there's two different results.
7 plus 3 times 2
or 7 plus 3 times 2. You see the pause there.
Now c is going to do the latter. It's going to do the multiplication first
because that has higher precedence.
So it's going to do 3 times 2, which is 6 and then it's going to add to 7.
So the result x is going to be 13 because
the star asterisk has higher precedence than the addition.
And so there are rules for every single operator,
which operator has precedence over the other.
There's a precedence level.
And multiplication and division have higher precedence than addition and subtraction.
So that's they're performed first, for example.
You can avoid all this precedence and having the language
decide for you if you provide parentheses.
Parentheses will always group the expressions
at the highest precedence. So if I say 7 plus 3 and I enclose that in parentheses,
it's always going to do the addition first.
If I say
3 times 2 and I enclose that in parentheses,
it's always going to do the multiplication and then add whatever outside of the parentheses.
So you should really just be using parentheses. Always use parentheses.
Otherwise, the language is going to decide for you. You have to memorize all the precedence levels
which is hard to do as I'm going to show you in the next table.
There's a lot of different operators and you have to know
precedence for all these different operators.
So it can become very very confusing.
This is similar to you know either you let the language do an implicit conversion
for you or you do it with an explicit conversion with a cast operator,
same type of idea here, just use parentheses.
So if we look at this table, which we'll look at a second i guess because I
first want to talk about associativity.
Associativity is related to precedence because
what happens if two operators have the same precedence. Then how do you figure out the order.
Well, then you apply associativity. So whenever operators
have the same precedence level, then you apply additional rules.
So associativity is basically whether or not you
start on the left-hand side and go to the right or right to the left.
So if you share an operand and they're both executed in the same order,
you're going to execute them in the order of the statement
based on the associativity rule. So for most operators,
the order is from left to right. So let me show you an example.
If you say 1 equals equals 2 is not equal to 3,
the equals equals and the not equals operators
exclamation point equals have the same precedence.
So in this case we have an ambiguity. We have a problem.
We don't know what to evaluate first. We don't know if we need to do 1 equals equals 2
or 2 is not equal to 3
and it matters because you're doing a Boolean expression.
This is A boolean expression here, so it's guys return to true or false.
So what you would do here is you would apply associativity.
The associativity of both the equals equals and the not equals is left to right.
You're going to first do 1 equals equals 2, that's going to be false.
And then you're going to say false, which is
not equal to 3,
which it isn't. So 1 equals equals 2
and then that's not equal to 3.
So the result is going to be false. So the expression on the left is executed first
and moves toward the right.
So how you want to think of that is basically it's equal to these parentheses.
First, you're going to evaluate one is equals equal to 2.
And then if that evaluates true or false, you're then going to say is that equal to 3.
And this is confusing because
it doesn't have true or false values.
But 1 equals equals remember that's the comparison operator, that's not the assignment operator.
And we're not using variables here, so it's even more confusing.
But if you were to put these numbers in variables,
you would just be saying like, if x equals equals y,
so if x was 1 and y was 2, that would evaluate 2 false.
Because they're both not equal. And then if 3 was another variable
z which is equal to 3,
you'll say does x equals y is false,
z is equal to 3. So
false is not equal to 3. So it would be false.
So 1 equals equals 2 executes first, result again 0 false.
Then 0 is not equal to 3, executes resulting in the 1 true.
So I said false initially, but obviously 0 is not equal to 3,
which the whole thing would evaluate to false.
The whole thing would evaluate to true, I'm sorry because 0 is not equal to 3.
So 0 not equal to 3 I was saying it's false because they're not equals, but
that means that it's actually evaluates to true.
So pretty neat there. Now here's our table that says
all the precedence and associativity rules and it's very hard to memorize this.
So again, what do you do, use parentheses.
But here is the table. The highest precedence level is at the top,
so the postfix operators have the highest preference level.
And if you're using any of those in the same expression,
then you read left to right.
So the postfix operators, things like
the parentheses are the highest precedence.
So every time you include something in parentheses that's going to take effect.
But you have the brackets and then you have
postfix operators. We haven't talked about a couple pointer stuff,
which is this dash greater than, but we do know
now about plus plus and minus minus
which are postfix those have the highest
highest precedence. Unary operators things like the plus minus, the exclamation point for not,
the tilde for the bitwise, the plus plus the minus minus
and then the casting operator is referred to as a unary operator because it only has one operand.
And then the asterisk operator is the pointer operator. The ampersand is the bitwise
and the sizeof.
Those have the next highest precedence.
But here you're actually reading right to left
to evaluation if you have two on the same line state statement.
And then again we just go in orders. You have to know this order and memorize it. So pretty tough to do,
but I did want to show it to you, did just want to
have a little illustration to show some of the tables. There's a lot of operators.
And so there's a lot of presedence levels, and there's the associativity for the most part
like we said earlier is left to right.
You only have the associativity of right left for the unary operators.
So the bitwise operators follow towards the bottom,
all left to right. Conditional ternary operators right to left.
Assignment operators are always at the bottom. That's always evaluated last.
And then if they're equals, other ones on there you evaluate right to left.
And then comma is also referred to as an operator. That's left to right.
Another concept though precedence and
associativity that we're not going
to really address with these other examples. We're going to end up
using the parentheses all the time, and that's what you want to do.
But again, if you want to fully understand the language, you want to understand these concepts that's why I tell it to you.
No one can actually claim they know language if they don't know some of the details like this.
But use parentheses as much as you can. Thank you.