3 – Operators
This lesson is going to be very easy. Operators are symbols, or combinations of symbols that we use in our code in order to perform various tasks. From the mundane assignment operation —which you have already seen— to shortcut coding goodies.
Now, there are quite a
few lot of operators in C and some of them wouldn’t make sense to introduce at this point. So we will be only be taking a look at the most basic operators, the ones that are relevant to you as a beginner.
The assignment operator, as you have seen, is the equality sign
=. It instructs the computer to take whatever is on the right of the symbol and store it into what is on the left of it. In the ‘Variables & Datatypes’ lesson you saw this done quite a few times (eg.
destination = value).
There are many other obvious operators in C, like for doing simple math processing. Of course we have the basic math symbols addition, subtraction, multiplication and division (eg.
3 + 2). One important thing to remember for the beginner with these binary (meaning that it needs two arguments/variables in order to work) operations, is that the result isn’t stored anywhere on its own. To store the result, we combine it with the assignment operation (eg.
int result = 3 * 2;). Multiplication is represented by the asterisk symbol, while the division is represented by a forward slash.
Exercise 3.1 — Bug Hunting 101
Look at the code below, what do you think the result would be?
int a = 3; int b = 2; int result = a / b;
Hint: the result is not going to be 1.5. The reason why it isn’t getting the right value, is because the integer datatype —as you learned in the previous lesson— cannot hold floating point data, for that we need to use a
float. So the value of
int result is going to get truncated. This is important to know, because this is in fact a bug in our code, but the code is perfectly valid — your compiler or IDE may give you the yellow card for doing something like this, if not these bugs are very hard to hunt down.
Unary Math Techniques
These operators are absolutely awesome, you will find these to be invaluable in programming. Unary means that we operate on one single variable. We can do things like increment and decrement a variable’s value — open up your template from lesson 1, and add the following code.
// declare variables we need int a = 1; int b = 1; // print original variables printf("original values\n"); printf("a: %i\n", a); printf("b: %i\n", b); // increment a a++; // decrement b b--; // print altered variables printf("altered values\n"); printf("a: %i\n", a); printf("b: %i\n", b);
Notice we are printing the exact same variables, but we altered their values with the increment operator (
variable++) and the decrement operator (
There are a couple of neat mathematics tricks we can do in programming besides the unary operators, that are equally awesome. You are going to be using these like crazy! — same drill, open up your template and add the following code.
// declare variables we need int a = 3; int b = 2; // print original variables printf("original values\n"); printf("a: %i\n", a); printf("b: %i\n", b); /* a minus-equals b a gets decremented by the value of b. */ a -= b; // a should become 1 after this line. // print a, to check the value printf(“altered a: %i\n", a); /* b plus-equals a b gets incremented by the value of a, which is now 1. */ b += a; // b should become 3 after this line. printf(“altered b: %i\n", b);
I think the comments explains pretty well what the plus-equals and minus-equals operators do. Even so, I do recognize that this may seem confusing for a beginner, but if you don’t get it play around with the values of the variables ‘a’ and ‘b’ and I guarantee that you will understand it.
Play around with the
/=, which does the same, except that this is multiplication and division — remember that for division, you will have to change the variables from
float and in the
printf function substitute
%f. Have fun.
In this lesson you learned the basic operators in C programming and how to use them, briefly touching on intermediate techniques of manipulating variable data within your code.