Computers have been around us for many years now. They are placed on our desks, inside our phones, and inside our cars and washing machines. Computers do their work by following instructions. These instructions are bundled together, as computer programs. The programs are then executed, which means that the instructions are followed, sometimes one by one and sometimes more than one at the same time, and the computer does what the instructions tells it to do.

The act of creating computer programs is referred to as programming, or if you wish to make it more clear, computer programming. This book has the goal of giving you an introduction to computer programming. It will do so by describing the basic elements of a programming language, and how these elements can be used to create programs.

Before learning how to do the actual programming, it is important to gain experience with the practical usage of computers. I will assume that you have such practical experience, and most likely you are using a computer, or perhaps another type of electronic reading device, right now, as you read this text.

If you read the book on the web, you are typically using a browser program, such as Firefox or Opera. If you use another reading device, such as a tablet computer, you may be using some kind of reader software, like a Kindle reader program or an iBooks program.

There are many types of computer programs, used in different scenarios, such as programs for word processing, program for making numerical calculations, programs for reading e-books, and programs for controlling the speed of your car or the temperature of your washing machine.

The programs described in this book will be small compared to a browser program, and they will also be small compared to a car control program, but they might still be useful for learning the important concepts.

The book describes how to write the program text, also referred to as source code. In order for a program to run, which is another word for execute, the program must be represented in a format that is understandable by the computer. This format is referred to as machine code. This means that the source code must be translated into machine code.

Although the book concentrates on how to write the source code, there will be references given, to places where more information on how to make actual programs run on your own computer can be found. It is advisable to follow along, and to make your own programs, as you read the book.

This is a Book with Views. This means that the book covers several programming languages at the same time. When you read the book, you read it one view at a time, and you can easily switch betwen the views. In the e-book version of the book there are links to the views at the end of each section, and in the web version of the book there are also links to the views in the left sidebar of each page. The view you are currently reading treats the programming language C.

The book is written to give an introduction, and to give possibilities to practice writing programs. When doing this, you take on the role of a programmer. Perhaps you will do it as a hobby, or perhaps you will do it as a profession. In any case I hope the book might be of help in the task of learning programming.

The remained of this chapter will give an introduction to programming, in the form of a simple example. In the following chapter Names and values, we will start looking at variables.

Say hello

It is customary to write a small program first, before making larger programs. A small program, with its only task being the display of a text message, saying "Hello, world", is often used. Such a program can be referred to as a Hello world program.

A Hello world program written in C often starts with an include directive. The include directive starts with the word #include, which is an instruction to the C preprocessor. The include directive tells the preprocessor to insert the contents of a file, in this case a file named stdio.h, and it is written as

#include <stdio.h>

The purpose of the include directive is to allow the program to have access to certain functions. A function can be thought of as an aggregation of instructions, put together for a specific purpose. In this program a function named printf is used. This function, which is used in the program as

    printf("Hello, world\n"); 

makes the string Hello, world appear on the screen of the computer where the program is run. The complete program is shown in Figure 1.

#include <stdio.h>

int main(void)
    printf("Hello, world\n"); 
    return 0; 

Figure 1. A hello world program in C.

This the C view - other views are Java - Python

The program in Figure 1 contains a function named main. The purpose of this function is to define a starting point, so that the operating system knows where to start the program when it is executed.

Read more

How to make it run

The program in Figure 1 can be executed on a computer. I assume that you have a computer, perhaps a computer looking like the computer in Figure 2.

Figure 2. A computer, of a possible type that you may want to use for programming. It may also be a computer in the form of a mobile phone, or some other device where a computer resides inside.

When you program using the language C you need a compiler. You also need a linker. Often it is possible to obtain a compiler and a linker for free. If Windows is used, it is possible to use Visual C++ 2010 Express. If Linux is used, a compiler by the name of gcc is most likely already available. On Mac OS, an alternative could be to use the Xcode development environment.

The Xcode developent environment for Mac OS includes a version of gcc. This is the case also for an environment called Cygwin, which is available for Windows. This means, in essence, that gcc can be used on Linux, on Mac OS, and on Windows.

Read more


This book has been produced using several open source software products and open standards. It is written using XML, and translated, using Python software, to html for the web version, epub for the e-book version, and LaTeX to be converted to pdf for the print version. The mobi version is currently created using Calibre. Emacs and PSGML are used for editing. The book contains images from the Open Clipart Library.

Read more