Hello. Welcome back. In this lecture,
we're going to discuss some of the basic operators
supported by the c programming language.
So operators again just special symbols for functions
you're performing some kind of operation.
So we're going to discuss basically in this lecture
4 different types of operators.
The arithmetic, logical,
assignment and relational operators.
And then we're going to discuss in another lecture the bitwise operators
and then a couple other special operators that don't really look like
they be an operator, but they are, the size of operator and the casting operator.
So an arithmetic operator is probably the most
common sense one, the one that you're probably most familiar with. It's a mathematical function,
takes 2 operands and performs calculation on them.
It's infix so the operators and between the operands.
A logical operator, sometimes called a Boolean operator,
is an operator that returns a Boolean result. True or false value, that's what a Boolean is.
So it's really just a conditional operator.
It's based on the Boolean result of one or more 2 other expressions,
one or two other expressions.
An assignment operator just sets variables equal to values.
It assigns the value of the expression at its right to the variable at its left.
So it's a right operand and a left operand.
Again, it's infix.
And then a relational operator will compare variables against each other.
So this is kind of comparable type order, less than and greater than.
But let's go over each one of them because there's many, many, many
of the different operators. C supports a super rich set.
So the first ones are these arithmetic operators in c.
These are the mathematical functions. So there's a little table
that I found online
from tutorials points website, which is nice.
And this summarizes all the arithmetic operators in c that are supported.
So the basic ones, just like in math. There's nothing really new here.
On the left-hand side of the table is the operator,
and the middle is a description of what it does and then on the right is an example.
So the plus symbol
that's going to add to operands. It's infix. It's in between the 2 operands.
So what you would do is you could either add variables,
you could add a combination of constants in a variable or just constants.
And this one we're saying a plus b equals 30.
But the actual operator and operands are just a plus b.
And then that would be the example.
Minus subtracts 2 operands, a minus b.
So a minus b could actually be equal to something like a negative 10 if a is equal to
20 and b is equal to 10 or
b is equal to a negative 10.
Asterisk is for multiplication.
Again infix
in between operands.
Division is the forward slash.
Percent is modulus operator. This is the remainder operator.
So if you were to say like a variable percent a,
what is that going to be equal. If b was equal to 20, a was equal to
12,
the modulus operator would return 8 because that's the remainder of 20 divided by 12.
Plus plus and minus minus, these are specific incrementation and decrementation operators.
They're still referred to as arithmetic operators,but you typically only see them
mostly in loops
when you repeat code, but not always. I mean in this example here,
basically you're incrementing a variable by 1
or by a specific value if you're doing
something else. But so if you do a plus plus,
whatever a was, if it was 5, it's not going to be equal to 6.
And then there's a decrement operator,
which decreases the integer value by 1, so a minus minus.
And the plus plus and the minus minus you should notice are after the variable.
This referred to as postfix.
And what this means is if that variable's in an expression,
it's actually not going to be assigned the extra one until after that statement.
If it's prefix,
it'll be assigned before. So let's go through some examples of these arithmetic
operating code blocks so that they make a little more sense.
So here's code blocks, here's our main routine.
We're familiar with this by now. I'm just going to create a couple variables
and do some operations on them and then print them out.
And I'm just going to create integers. And it doesn't matter though you could create floats to do
calculations or doubles, but we'll just make it basic.
We'll say a is equal to 33.
Create another variable of type integer, and we're going to assign that to
15. So we're doing a declaration and an assignment in a single statement.
And then we're just going to have a result variable.
And this is going to be initialized to 0
so here we can start performing calculations. We can say c,
and we can do assignments. We haven't learned about assignments specifically formally,
but we know what assignments are. Assignments are taking data
and putting that data in that variable. So c equals is
taking whatever is on the right-hand side and putting the result in to that variable.
So if we say a plus b,
again this just looks like normal mathematical routine,
and it is. It would just be 33 plus 15, and what would be the value of c,
48. And so you could just print out the values.
You could just say c is percent d
using our format specifier and just printing out c.
And then maybe we could do a line feed.
And that would just add 2 numbers. It's that basic. It's that simple.
We're just displaying it. So when we run this program, we're going to see
the addition of 33 plus 15.
We recompile this, save it, say build,
and then save run.
You'll see that when I run it after my virus scanner, it says c is 48.
So you can play around with these. I mean this is these are basic. So a plus b,
you can do the minus, you can do the asterisk for multiplication,
and then you can do the division for forward slash
and then the percent symbol which is modulus.
So this is interesting because you know 33 divided by 15 would be 2
with a remainder of 3, so result is going to be 3 in this case, let's just make sure though.
Save this, and make sure we close our execution.
And when we run it, we can see
that
we have c is equal to 3 because that's the remainder.
Otherwise, it's pretty basic. The important ones with the plus plus are to
understand the prefix and the postfix.
So if I were to say and this is more relevant when you actually do it in a single statement, printf.
So let's say I print out
a plus plus, what that's going to print out is 34
because you're going to increment a by 1
and then print it out. But you're going to do it in the single statement,
you're going to do in the same line because it's afterwards.
So actually what's going to be displayed here
is it's only going to display 33.
It's not going to actually increment a until the next line of code.
So if we run this,
you'll see it just prints out 33.
Now that's the difference between doing a plus plus a.
If you do a plus plus a, which is the same thing you're going to increment by one,
difference here though is you're going to increment it by 1
and then before the statement is executed. So here now
we'll notice that
we just have a couple warnings,
no errors. And when we run it, now you'll see the answer is 34
because it's actually incremented before the statement is executed. And that's important to know
between plus plus variable name versus variable name plus plus.
That's just referred to as prefix and postfix.
The same would happen for the minus minus.
So keep those in mind for your
unary.
These are also unary operators because they only have one operand.
The other ones are binary operators because they have an operator
in between the operands.
But understand the minus minus versus the plus plus.
And then also understand if you place it before the variable after the variable
when it's going to actually increment or decrement.
And again, all it is if it's after the variable,
it increments or decrements right there
before it executes the line of code. If it's um before the variable,
it's going to execute the line of code and then after that's executed the statement, it's then going to
either increment or decrement.
But play around with this, open up a window,
do a bunch of a different arithmetic expressions and see how it all works.
Now let's talk about some of the other types of operators.
The next group of operators are referred to as logical operators.
And we defined what this means is it basically is
conditional type operators. They return Boolean results,
true or false values.
So the symbols that represent logical operators are the ampersand ampersand.
The 2 parallel bars, and the exclamation point.
The ampersand ampersand is called the logical and operator.
It's also an infix operator that takes 2 operands,
one on the left, one on the side. Basically, if each operand
is non-0, then the condition becomes true.
And so non-0 means 1, which means true. So if a is true and b is true,
then the answer would be
true.
If a is false and b is true,
the answer is false. If a is false and b is false, the answer is
false.
. So again, we'll do some examples of that.
And then parallel bars are the logical or operator. Again, this should be similar to your math class,
so I don't want to spend too much time on it. But
if any of the 2 operands is non-0, either one is true,
then it's true. So if a is true and b is false, it's true.
And these are expressions that return values.
So the return would be if a is true and b is false, its return will be true.
And then the logical not operator which is the exclamation point,
this just reverses the logical state of its operand.
So if the condition is true, it then is going to return false.
So if you say not a, the exclamation point and a was true a is false.
If you put the not outside of parentheses,
whatever a and b evaluated to it's now going to value to the opposite.
So let's again look at some examples and code blocks.
Here's cold blocks. And again, I'm going to create a couple variables to demonstrate logical operators.
Again, I'll just create some integers we'll say a equals 12
and then b equals 20.
And what would be better
is we'll create a because I don't want to utilize conditions
so we'll just create an underscore bool,
and we'll assign that,
I will say that will be results,
and we'll initially not assign that to anything.
And then we're going to just print out some data.
So we want to see what the values are these expressions
returned if we use the logical operators.
So in here I'm going to print out
percent d, which is an integer. Because remember,
Boolean expressions are just integers.
Boolean variables are integers. So if the Boolean value is 1,
then that means it's true.
If it's 0, it's false. Anything else usually 1, it's true.
So I'm just going to print out the result.
And then I'm going to assign some data to it. So if I say results equals
a and b,
now that's not going to make any sense because these are numbers.
So let's initialize a equal to true,
and again use Booleans.
And here we'll say this is true
and this is true.
And when we use the ampersands, this is saying a and b
both have to be true in order for the return to be true.
So results should print out
1 because 1 represents true. So let's build this
and make sure that
we have to use the right
name space. So we're just going to represent 1 for true and 1
because we
we don't doing the right include right now, so that's no big deal. So now we run,
it should report true, 1 and it does 1.
Now if i change any of these to non
not true, if I say 0, then the result is going to be false
because a and b both have to be true in order for the result to be true.
So if I build this and I run it,
you're going to see
that the result is 0 because it's false.
So either one if I change a or b to 0, which means false,
the result would be false because both a and b have to be true.
Now if I say a parallel bar parallel bar, which is or a or b,
only one of them has to be true in order for results to be true.
So in this case, results should be 1.
So let's build this and run it.
And now we have a value of 1.
So only one of them has to be true. So now if I make them both false
and this is going to be much more applicable when we talk about if statements
and conditionals and making decisions, but I am demonstrating Boolean
logic here.
Now both are false. So this should return 0,
which is false and it does, 0,
so pretty neat. And then the last one is just the not symbol,
which is the exclamation point which just does the inverse.
So if I say not a,
if a is equal to 0, a is now going to be equal to 1 or result will be
and result is going to be equal to 1,
which it is.
So those are the 3 logical operators. Again, they'll be used in usually decision statements.
Let's now move on and talk about another set of operators.
Now here are the assignment operators. We're pretty familiar with at least the simple assignment operator,
which is the equal symbol. The equal symbol assigns data,
and that's pretty basic. C equals a plus b, c is going to be the result of a plus b.
Now there's these compound type of operators where you can do plus equals
or minus equals or star equals.
Plus equals is where you add and the assignment operator.
So it adds the right operand to the left operand
and then the assigns the results to the left operand.
So you could say c plus equals a, and that's the same as saying c equals c plus a.
Minus equals the exact same thing except you're subtracting. You're subtracting and then you're assigning.
So you subtract the right operand from the left operand and then assign the data.
So if you say c minus equals a, it's really c equals c minus a.
It's a shorthand notation, these plus equals and minus equals.
You can write things a little faster because
you do you do 2 operations essentially
in one single statement.
And then multiplies the same thing, star equals, multiply and assign.
So c star equals a is equivalent to c equals c times a.
That's pretty straight forward I think.
The other assignment operators are similar
except you're using division. So division equals is divide and then sign.
Percent is do the modulus and then assign the result.
So those are pretty straightforward.
And then we have some other ones, which we're going to talk about in another lecture in more detail.
So I'm not going to touch them on here because they're really completely different.
They're called the bitwise operators, which is
left left and all I'm saying here is that
for those bitwise operators that we're going to talk about, you can also assign them in
single statements.
So shifting, doing a left shift and right shift and assigning,
you can just do less than less than equals and write greater than any greater than equals.
And then again the ampersand, the carrot
and the parallel bar are all related to doing things on bits.
So we'll come back to that, but they are part of the assignment operators.
So the assignment operator is just pretty easy,
assigning data and then if you want to actually do expressions,
a couple expressions, then assign it, you do the combination ones.
The relational operators are comparison operators. They compare 2 values.
So this is important, equals equals. We'll compare the value of 2 variables.
And if they're the same, it's going to return true
because it's going to return a Boolean expression, true or false or Boolean value.
Exclamation point equals is checking if the values 2 operands are equal or not.
So a is not equal to b is how you want to read that.
If a is true and b is true, then that's going to return false because
they're equal.
So a would have to be false or b would have to be true or vice versa,
and that would return true. The greater than symbol is
checking whether or not a is greater than b.
Less than symbol, a is less than b. Greater than equals,
a is greater than or equal and less than equal.
So again, this is all basic math stuff.
I'm not going to go through examples in the IDE because this is pretty straightforward.
Comparison operator is just like in math, greater than equals and so forth.
So hopefully, we have a good handle on the basic operators
arithmetic, logic, assignment and relational.
Used quite a bit so you need to understand them. Hopefully, it's not too difficult
or confusing.
You're going to use a lot of this in if statements and decision statements,
so we'll provide a ton more examples on these. Thank you.