C Programming Tutorial 31 – Arithmetic Operators

Did you read the last blog on Intro to Operators? Are you new here? Start at the beginning, Intro to C!

This blog is going to discuss the arithmetic operators. Now, we’ve discussed these some already and I’m sure you are familiar with them from math classes you’ve taken so this blog is really not going to be that hard or confusing!

The arithmetic operators include the +, -, /, and *. These are all binary operators that need two operands.

The / is called a forward slash. Some people call \ a forward slash and they are wrong! The top of the slash is the reference point, not the bottom. The top is leaning forward, therefore / is called the forward slash.

The * is called an astersk. I like to call it capital 8, but I’m weird.

Now, when you have an expression like this:

First the multiplication and division happens from left to right, whichever comes first. This means that 3 * 4 happens first. The expression could be simplified to 2 + 12 / 3 – 2. The next thing to happen is 12 / 3. The expression could be simplified to 2 + 4 – 2. Next, the addition and subtraction happens, whichever comes first from left to right. So we get 6 – 2 which evaluates to 4.

You may find it more clear if we did this:

You can use as many parenthesis as you want and wherever you want, as long as you remember to close any parenthesis you open. They always get evaluated from the inside out. So 3 * 4 happens first, then the result of that gets divided by 3.

Now, you can also use parenthesis to force a certain part of the expression to happen first. For example, if we wanted the subtraction to happen first, we could have set it up like this:

Let’s output this and make sure we get the result we expect.

Some people choose to use parenthesis for nearly everything because they believe without them it is easier to make mistakes with arithmetic, but this is up to you.

Now, I’m going to introduce you to a new operator…

Check out this expression.

Now, I can tell you now that the value of y is going to be 1. Can you figure out what this operator does? We’ll find out in the next video.

In the next blog we are going to be discussing all of the Modulus Operator. Check it out!

Leave a Reply

Your email address will not be published. Required fields are marked *