Introduction

C programming language has been around for quite some time (since 1972) and people still use it a lot. If you are a software developer, or want to become a software developer, chances are high that you will need to read C code sooner or later.

Even more important is that C is so widely used and so well known that many modern languages inherit various features of C. Curly brackets, semicolons, and assignment with = can be found in the majority of programming languages used today. So let's see how it all started.

Variables and expressions

A program would normally store some information in memory. Maybe you write a program which will calculate how much a bunch of bananas cost; the price of one banana and the amount of bananas will be stored in what we call a variable, and putting a value into a variable, so the program would remember it, we call assignment.

Modern languages such as Python or JavaScript allow you to assign both numbers and strings to variables as you wish; C does not. C is a low level language and requires you to declare variables with types before a variable can be used; a type tells you what values the variable can store: numbers (and how big they are), characters like a or b, etc.

int is a very common C type that stores whole numbers. We'll talk more about that later, but for now,

int price;

we say that price is a variable of type int, that is, we can store a number there and use it later. We do it this way:

price = 2;

The = character here is a tricky one. It's not like in math, where it means "equals"; in C (and in almost all programming languages) it means "let this variable have this value". To the right of = is any expression: we could write

price = 1 + 1;

or – let's say the price needs to increase by one –

price = price + 1;

which will set the value of price to 3. Note that, in algebra, this line, x = x + 1, is an equation which has no solutions; but here, it's taking the old value of x, increases it by 1, and puts it back into x, effectively increasing x by one.

So back to our bananas, if we have price and we have

int count;

count = 4;

then we can easily calculate the total amount to pay:

int amount;

amount = price * count;

and when the program is executed, the value of amount will be set to 12 (if our price is 3).

Let's see how we can actually write and execute this program. In C, it will take some effort.