Originally published by Robert Beisert at fortcollinsprogram.robert-beisert.com

Linux + C – Doing the Math I

If we’re being honest, we don’t start programming because we want to print out clever one-liners to a terminal. We want to calculate something complicated or massive, and we want to do it quickly.

Variables, functions, and symbols

We all know what a number is, right? We may behind many countries in mathematics and science, but we at least understand what is and is not a number.

Good. On to algebra.

In middle school we learn about functions, in which we replace some numbers with a variable that can hold a number of values. For example,

x + 3 = y

has one value of x for every possible value of y. We can replace x with a number (say, 3) and the equation is still correct if we find the correct y (6).

In C, we use variables in the same way. Variables are “tags” we associate with a “value”, meaning that we can go into memory and get a value we previously calculated.

How do we perform all operations in a computer? Through functions – tags we associate with a block of code, which performs some particular task. For example, we have used the printf() function, which performs the operation of sending some text to the terminal.

In C, unlike some previous languages, we have symbols that act like functions. In particular, we have math symbols that we can use to create math problems exactly like we would on paper.

Now, this being a section on mathematics, we have to determine which symbols we can use for math problems. Below is perhaps the most common set:

+      plus

–       minus

*       multiply / “times”

/       divide

%      modulo

=      equals (assignment – stores a value on the right into a variable on the left)

==    equals (test – are these things equal?)

>=    Greater-than-or-equal (is the thing on the left bigger than or equal to the thing on the right?)

<=    Less than or equal (is the thing on the left smaller than or equal to the thing on the right?)

()      Parentheses (allows for order of operations)

That means that we can write the equation

int x = (5 + 3) / (2 * 8) * 12

and the computer can understand it.

Assignments and Comparisons

You probably noticed (because I bolded it), that we have two different kinds of equal signs in C. This is because there are two things we want to say with the equals sign:

  1. This is equal to that.
  2. Is this equal to that?

The first is called an assignment. We use assignments to write a value into a variable. For example, in the equation

int x = (5 + 3) / (2 * 8) * 12

what we are doing is calculating the value on the right (6, for those of you playing at home), and storing it in the integer (number with no value after the decimal place) we called “x”.

We could then ask the question of whether x is equal to 5 or not. In C, this looks like:

if (x == 5)








One of the most common beginner mistakes is confusing assignments and equalities. Don’t worry too much if it takes a while to get it right.

Tomorrow we will look at some basic types of numbers (data types), and generate a program to put this into practice.