Skip to main content

How Code becomes Apps

Our computers do not understand human language. If we want our computers to understand what we want it to do, we must write something called a program.

A program is simply a set of instructions for our computers. At its most fundamental, a computer understands only very simple instructions. These instructions are instructions like:

  • add 2 numbers together
  • move this number from one location in memory to another
  • copy a number from one location to another
  • subtract two numbers

The ability for a computer to do all that it does has to to do with its ability to do these very simple operations very quickly.

Programming

Programming is the process of creating a set of instructions to a computer to do some task. These instructions are called programs and are written in a human readable form. These programs are then translated into something that a computer understands by some sort of translation program. These translation programs are called by various names depending on what is being translated and how that translation takes place.

Computer Languages

Computer languages are sometimes described as a high level language vs a low level language. A low level language is a language that is closely related to the hardware that the program must work on. The lower the language the more closely it relates to the machine language for a particular machine. With assembly languages being the closest to machine language. These programs typically have a directly one-to-one translation from an assembly instruction into machine instruction.

High level programming languages are separate from the machines that they run on. You can write a program in a high level language and translate it into the machine languages for different plaforms. For example, the C program in the next section can be translated so that it runs on windows machine with an Intel processor and it can also be translated to run in MacOS on an M-Series processor.

The program that turns C programs into machine language for the computer it is running on is called a compiler

There are various compilers available to do this translation. Depending on the platform, you may be more likely to use one compiler vs another.

The C Compilation Process

The following diagram illustrates the compilation process.

The compilation process in C involves 3 basic steps. You usually don't need to know what these steps are. This is espcially true in the first semester. However, knowing that there are actually 3 steps may one day help you understand why a bug manifests a certain way. For now, this section may simply be interesting information that doesn't have a lot of real application

Preprocessor

In C, any statement that begins with the # symbol is a pre-processor statement. The main two preprocessor statements we will see in this course are:

#include ...
#define ...

Preprocessor statements are statements that act as text replacement in your program.

The #include statement for example is essentially a copy+paste command... when the pre-processor reaches this statement, it will replace the statement with the contents of the file indicated. Thus, the following statement will be replaced with the contents of the file stdio.h by the preprocessor

#include <stdio.h>

At the end of the preprocessor process you have a C file with preprocessor related statements turned into C code. The C code is actually still human readable at this point

Compiler

The compiler turns the C program into instructions that the computer understands. It is essentially a translater from C into machine language. The result of just this part of the program is binary file known as an object file. This file is no longer human readable

Linker (Loader)

This last step puts all the binary pieces together. If you made use of a library, the pre-compiled binary versions of the things that you used will be combined with your program. Think of it this way...when you are writing a program you are creating some pieces of the final executable. However there are some premade pieces that are already done. the linker assembles your pieces with the premade pieces to form the finall executable.

If you ever see an error message that says Link or ld error the error is at this stage of the compilation... there is a problem with piecing the bits together as opposed to a problem with the pieces.