C Programming Tutorial 33 – Unary Plus and Minus

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

This video is going to be talking about the minus and plus operator.  But wait, don’t leave yet. We’re not talking about the binary minus and plus operators, rather the unary ones.  As a reminder, the difference is how many operands they work on. The binary minus operator, for example, will take one operand, subtract the second operand from it, and generate a result from that.  Neither of the operands are modified.

The unary minus, on the other hand, just takes one operand, and negates it.  To negate something means to take the negative of.  To make this clear, let’s go through an example.

double money = 25;

double bill = 15;

double total = money – bill;

This example uses the binary operator.  But now let’s say you go to pay your bill and they say, you know what, we are going to give you some money instead:

double total = money – -bill;

Now, we essentially have 25 – -15, which becomes 40.

Now, what is the value of bill?

The value of bill is still 15.  This operator does not change the value.  In the next video we are going to talk about a unary operator that actually does change the operands value.

There is also a unary plus, which is less useful, so I’m not really going to talk about it.  I would encourage you if you are an eager learner to look up the use of the unary plus operator.

In the next blog we are going to be discussing all of the Increment and Decrement Operators. Check it out!

Leave a Reply

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