operators

“I can calculate the motion of heavenly bodies, but not the madness of people” Isaac Newton

If we want to multiply two or more values we use asterisk symbol *. If there was a need to sum two values, we use plus + symbol.

Example:

int length = 3 *7;

The final value assigned to the length variable will be 21. This is because first the result of the multiplication will be declared and then this result will be sent to the length variable.

There’s another symbol called assignment = which we use to assign a value to a variable.

For example:

int age = 24;

You might think that this = symbol means equal but no! The equal sign in the world of C programming is ==.

So in the example above, we’ve actually assigned the value 24 to the variable named age.

These signs and some others which we will introduce in this and next sections are called operators.

The name might not be that intuitive but this is what we call them in the world of programming.

When we do operations like mathematical operation, there are at least two values involved and we call these values operand.

Operands are values. This value could be the returned value of calling a function, the value assigned to a variable or simply just a constant value.

Let’s take an example:

int numberOne = 240;
int result = 330 + numberOne;

In the example above we have the variable numberOne and result.

We first assigned the value 240 to the numberOne variable via assignment = operator and then in the next line we’ve used + operator to add together two operands 330 and the value assigned to numberOne. The sum of these two values is assigned to the result variable via assignment = operator.

LValues, RValues:

Let’s take a look at a simple example:

int jackSalary = 200000;
int ellenSalary = 160000;
int totalSalary = jackSalary + ellenSalary;

LValues are those values on the left side of assignment operators and point to a space in the memory that we can use to store values. As we know from the variable section, a variable is a pointer to a location in the memory. So then jackSalary, ellenSalary and totalSalary are the right candidates for LValues.

So LValues variables:

  1. They point to a location in the memory.

  2. They reside on the left side of the symbol `=` operator.

Note: both conditions should apply to the LValues. For example:

int jackSalary = 200000;
5000 = jackSalary;

The second assignment is wrong because the value 5000 is a constant and does not point to any location in the memory and so if you run the example above the compiler will return error instead of compiling the program.

Note: other than = operator there are multiple assignment operators which you’ll learn about them in assignment section.

RValues:

The values that sit on the right side of the symbol = operator are called RValues.

RValues can be constant like 200, 1000, 2.2 etc., variables like jackSalary and ellenSalary that we saw in the example above, or any expression that yields a value like jackSalary + ellenSalary that give us the sum value.

In the example above the value 200000 and 160000 are RValues.

Also the expression jackSalary + ellenSalary; is an RValue because it yields a constant value.

There are 5 categories of operators:

Name: Example:
Arithmetic Operators +, -, * etc.
Assignment Operators =, -=, += etc.
Comparison Operators <, >, <=, >=, == etc.
Logical Operators &&, ||, !
Bit fiddling operators (AKA bitwise operators) <<, >>, ^, &, |

In the next sections we will introduce and explain each of these operators.