Skip to main content

Operators

This section is a more detailed look at the various mathematical operators available to us in C.

Types and Operators

Mathematical operators usually work on both integer and floating point types (there is one exception). However, the type of the result of the operation depends on the data types provided. If both operands are integers result is an integer. If one or both operands are floating point, the result is a floating point.

Terminalogy

An expression is a sequence of operator(s) and operand(s) that evaluates to a single result.

Operators are symbols used to indicate the operation to be performed. For example, the + symbol is the addition operator, which specifies that the operands should be added together.

Operands are the data that is being used by the operator. For example the two values on either side of the + would be the operands for that operation

Precedence refers to the order of operations of the operators. Each operator has a precedence which dictate the order of evaluation within an expression with multiple operators. Precedence can be modified based using round brackets ().

Basic Mathematical Operators

Operator NameOperator Symbol
Multiplication*
Division/
Addition+
Subtraction-

Note that multiplication in C is not ×\times It uses the astericks (*) character instead.

The above 4 operators work pretty much as you would expect with the exception of the division operator on integers.

When doing division with two integers, the result is a truncated integer value. if we were to see what 5/3 is on a calculator we would get something like this.

5/3 = 1.66667

however in our programs, because it is integer division we chop off the decimals. Thus 5/3 = 1

This is also what happens when it is negative. -5/3 = -1

There is no rounding. Effectively we just stop at the decimal point.

This may seem awkward to how you may usually think about doing a problem but this method of division actually has some really useful properties that can help us solve certain problem.

For example, suppose I wanted to know how many full hours are in 351 minutes. We can do find this with an integer division. 351/60 = 5.

Modulus

The Modulus operator is represented by the % symbol. It is the only operator that requires integer operands. Using floating point types as either operand would result in a syntax error.

The modulus operator calculates the remainder of a division

5 % 3 = 2

This is because the remainder of 5/3 is 2.

Finding the remainder is probably something that you had not thought about doing for a long time.. but the modulus operator is actually very useful. Here are a few common use cases:

  1. Finding out if a value is odd or even. If you take any even value (2,4,6,8... etc.) and calculate the number % 2, you will find that the remainder of this calculation is 0 if the number is even, not 0 if the number is odd (will be -1 or 1 depending if value is negative....)

  2. Find the last digit of a value. Given any number, the last digit of that number by calculating number % 10. Note that the more accurate calculation is actually | number % 10 | (absolute value of number%10) as the result can be negative if number is negative

While the above are the most common examples of the usefulness of modulus, there are actually other very useful things you can do with modulus. One of the key things to note about modulus is that it is actually a repeating pattern... consider the result of calculating % 4 with all numbers from 0 to 10:

0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
6 % 4 = 2
7 % 4 = 3
8 % 4 = 0
9 % 4 = 1
10 % 4 = 2

Note how the results go 0, 1, 2, 3, 0, 1, 2, 3.. in fact this pattern will continue forever.

In general when you calculate m % n where m and n are both positive, the result will be between 0 and n-1 inclusive.

warning

Note that C does not correctly implement the mathematical definition of the modulo operation for negative operands. What it does is calculate the modulus operation as if both operands are positive, then if the first operand is negative, it makes the result negative. Mathematically speaking this is not correct behaviour.

Order of Operators

Similar to math, C has operator precedence. That means that if multiple operators appear in the same expression, the evaluation occurs according to this ordering. The result of applying one operator is then used to calculate the rest of the expression. If two operators are at the same level of precedence, you apply according to the operators associativity rule (left to right or right to left).

While there are many operators in C, we are going to focus on the arithmetic operators at this time. We will look at other operators as we go and augment the following chart. Each row is a different level of precedence. Operators in the same row have the same precedence and is evaluated according to their associativity.

operator symboloperator nameassociativity
* // %multiplication, division, modulusleft to right
+ -addition, subtractionleft to right
=assignmentleft to right
tip

When you read the = operator symbol read it as "assignment". Do not call it the "equals" operator because in math, the meaning of that operator is more ambiguous and you decide if you are assigning or testing equivalency based on context. However in C, this operator only has one meaning... assignment.

Example of Order of operations. Consider the following snippet of code:

int x;
int y;
x = y = 6 + 3 % 4 * 5 - 2;
printf("x = %d, y = %d\n", x, y);

what is the output of the above program?

We can work out this out by following the mathematical equation:

ExpressionComment on what we are calculating
x = y = 6+3 % 4 * 5 - 2starting expression
x = y = 6 + 3*5 - 23 % 4 = 3 so we replace that with 3
x = y = 6+15 - 23 * 5 = 15 so we replace that with 15
x = y = 21 - 26 + 15 = 21 so we replace that with 21
x = y = 1921 -2 = 19 so we replace that with 19
x = 19result of = operator is the value assigned. y has value of 19, result is 19
19result of x = 19 is 19 as 19 was assigned to x but we don't do anything with it though.

Self Assignment Operators

There are operators in C that allow you to express an assignment and operator in a single operator. These operators are +=, -=, *=, /=, and %=. In other words, its an operator that combines both an assignment and mathematical operator in one. The result is the value assigned to the left operand. As this operation is an assignment, you must have an l-value, or something that you can modify, for the left operand.

These operands are a shortcut notation for applying an operation indicated to left and right operand. It then puts the result back into the left operand.

Example:

int x=5;
x = x + 2; // x is 7 after this expression is evaluated

The above can be rewriten as:

int x = 5;
x += 2; // x is 7 after this expression is evaluated

Increment and Decrement Operators

The increment and decrement operators are one of the most common sources of errors for beginner programmers. It is important to understand what they do so that you do not use it in the wrong manner.

The increment and decrement operators use the symbol ++ (increment) and -- (decrement). Unlike other operators, it only has 1 operand. The operand must be an lvalue (something that you can change).

Prefix vs Postfix.

While there are two symbols for increment/decrement operations (++ and --), there are in fact 4 operators. The difference between them has to do with where the operator lies with respect to the operand.

Post means after. So postfix means the operator comes AFTER the operand. Pre means before. So prefix means the operator comes BEFORE the operand.

int x = 5;
x++; //postfix ++
++x; //prefix ++

These two operators do NOT have the same order of precedence.

postfix has higher precedence than prefix.

What they do:

The increment operator will increase the operand by 1. The decrement operator will decrease the operand by 1. This statement is true no matter if it is prefix or postfix.

int x = 1;
int y = 1;
x++;
++y;
printf("x = %d, y = %d\n", x,y);

The output for this code snippet would be:

x = 2, y = 2

Soo... if they both cause the operand to increase by 1, why are there two operators? The reason that there are 2 has nothing to do with what happens to the operand. The difference is that these expressions evaluates to different values. Lets take a look at this via an example.

int x = 1;   //both x and y start at 1
int y = 1;
int a;
int b;
a = x++; //x becomes 2, a becomes 1 (old value of x)
b = ++y; //y becomes 2, b becomes 2 (updated, new value of y)
printf("x = %d, y = %d\n", x,y);
printf("a = %d, b = %d\n", a,b);

The output for this code snippet would be:

x = 2, y = 2
a = 1, b = 2

postfix increment/drecrement evaluates to the original value of the operand. prefix increment/decrement evaluates to the new value of the operand.

Note that the operand changes for both versions! It is only the result of the expression that is different.