Operators in C Complete Tutorial

In this section, we will learn what the operators are and how to use them in C.

What Is an Operator in C?

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.

Also, there’s another symbol called assignment `=` which we used 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 the next sections are called `operators`.

Basically, operators will cause an operation to happen in C.

C Operands:

When we do operations like mathematical operation, there are at least two values involve which we call them `operand`.

Operands are values. These values 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 the assignment `=` operator.

C LValues and 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 the 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 an error instead of compiling the program properly.

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

C 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` which at the end gives us a single 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.

List of C Operators

In C language, there are 5 supported 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 couple of sections, we will introduce and explain each of these operators.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies