You've already started watching Operators and Expressions
Operators specify how variables are manipulated. Expressions combine variables to produce new values. C supports the typical binary operators: addition, subtraction, multiplication, and division. C also provides unary operators.

0:00
You now know that variables are the basic data objects manipulated in a C program.

0:06
I now want to talk about the movers and shakers of those variables.

0:10
I'm talking about operators and expressions.

0:14
Operators specify how your variables are manipulated.

0:18
Expressions combine variables to produce new values.

0:23
C supports the typical binary operators you're all familiar with—

0:26
addition, subtraction, multiplication, and division.

0:31
C adds additional unary operators, as well.

0:34
Let's take a closer look at operators and expressions in C.

0:40
Operators and expressions are fundamental to programming.

0:44
Let's take a look at their use in C.

0:46
The first thing we'll do is go to our Development folder

0:49
and create a new subfolder and call it operators and expressions.

0:58
Copy our template for our C program and place it in the folder.

1:03
Now open the project in Xcode.

1:08
Now, let's remove the print statement that's there,

1:11
leaving the return 0.

1:13
So, let's declare an int variable called a and initialize it to 9.

1:20
Let's print 9—something we've become familiar with at this point.

1:28
Nothing particularly special—ah, but there is an arrow.

1:32
We did not actually include the variable that we wanted to print.

1:37
Actually, let's take a look at this—we see "unused variable 'a'"

1:41
and our print statement is pretty much gobbledeegook.

1:45
Insert the variable a—our warnings disappear and a appears.

1:52
We'll now look at something called autoincrementing.

1:56
We'll declare a new variable c and assign it a

2:00
and then autoincrement a after the assignment.

2:04
The plus pluses translated to a + 1—

2:08
placing the plus pluses behind the a

2:11
makes this statement mean assign a to c

2:16
and afterward, increment a by 1.

2:19
Let's now print both c and a.

2:28
So, if we've done things correctly, c should have the original value of a,

2:33
which was 9 and a should now be 1 + 9, or 10.

2:40
C is now 9—the original value of a.

2:44
A is now 9 + 1, which is 10.

2:49
Let's declare another variable, d.

2:52
Let's autodecrement a and assign a to d.

2:58
Autodecrementing is subtracting 1 from a,

3:02
which would make a, which is currently 10—make it 9 again;

3:07
however in this assignment statement,

3:09
it translates into d = a  1—

3:13
we have decremented a before the assignment to d,

3:18
as opposed to incrementing a after the assignment to c.

3:23
Let's print those values—to speed things up,

3:25
I'll just copy and paste the print statement we used for c and a.

3:34
Let's run this—so d is now 9,

3:39
which is the previous value of a,

3:41
minus 1, and a has now been decremented back to 9.

3:47
Let's take a look if we—instead of autodecrementing before the assignment—

3:51
we autodecrement it after the assignment.

3:55
D is assigned the original value of a, which was 10,

3:59
but a is still reduced by 1 back to 9.

4:04
Let's take a look at an operator called modulo.

4:08
We'll declare a new variable b, and call it a modulo 4.

4:14
I know you're thinking, modu—what? What's a modulo?

4:17
Recall your long division from school—

4:20
remember learning about remainders?

4:23
The modulo operator returns the remainder

4:26
after division of one number by another.

4:30
So, let's print out what we have—

4:37
a is currently 9. 9 divided by 4 goes in 2, leaving 1—

4:44
that is the remainder—the remainder is assigned to be.

4:47
The value of a remains unchanged at 9.

4:51
C, of course, has all the typical binary operators we've become familiar with.

4:57
For example, I can declare a float variable fa,

5:00
and assign it the value of 5 divided by .5.

5:12
So, 5 divided by .5, or 5 divided by ½, is 10.

5:17
C also provides a compact version of addition, subtraction, multiplication, and division.

5:24
Here we'll use the compact form of multiplication.

5:28
fa x= 10—what this means is take the original value of fa,

5:35
multiply it by 10, and assign it back to fa—all in one line of code.

5:42
This is a shortcut for the long way, which would be fa = fa x 10.

5:49
So, we could have written fa = fa x 10,

5:55
but we're replacing all of this with a single binary operator, fa x= 10.

6:08
Let's see what the value of fa is now—

6:12
the value is 10 x 10, or 100.

6:16
This compact form can be used by all the binary operators—

6:19
multiplication, division, addition, and subtraction.
You need to sign up for Treehouse in order to download course files.
Sign up