1. What is code? - Part 1
A simple tutorial on what it means
to write code and compile/build
- by Nandeep Mali (http://miniorb.in)
What is Code? by Nandeep Mali is licensed under a Creative Commons
Attribution-NonCommercial-NoDerivs 3.0 Unported License.
3. Code is written in a 'language'
What is a programming language?
English Some programming language
4. Its called 'language' for a reason
English helps me talk to you.
A programming language helps me talk to a computer and give
it instructions on what I want to achieve.
Will not go into details of the binary code that computers can
understand, etc.
5. Huge number of languages
How does the computer
know so many
languages?
It doesnt.
It can only understand
binary.
What do we do when we
don't know a language?
6. We ask for a translator/dictionary
This is what we do in a computer.
When we write some code in a language, we need to translate
it to something that computers understands.
This process is called "building" the code.
Note: You must be wondering why don't we just write the language that computer understands directly?
This is because, a computer understands only binary. And its is very very very difficult to write binary
code. Since we humans are lazy, the hardworking scientists wrote higher level languages that facilitate
easier semantics that resemble English, just so us lazy programmers would have at least some time for
recreation.
7. Building the code
All new linux users wonder. What the heck is "building the
code"? And how does one do it?
Building the code essentially involves this:
1. Get the code you are planning to build (duh)
2. Translate the code into binary
3. Generate the executable that contains translated code that a
computer can understand
4. Have fun
8. Too high level?
Ok lets break this down.
Remember we said we need to translate code? The build process does exactly
that.
Let us say we write some code in C. Computer understands binary. We now
need to convert this. This is done by tools like:
GCC
Sounds familiar? Ever wondered why you need GCC? You remember Turbo C
from college days? GCC is exactly that. Its a 'compiler'. In a loose definition, it
helps convert our code into machine readable code.
9. More examples of compilers
These names may seem familiar:
GCC
Visual Studio
Turbo C
All of these are compiler tools made by hard working people so
we can write code in C and not break our heads with binary.
10. A note
Compilers are very complicated piece of software. For the
scope of this tutorial we will stick to basics. There are many
other processes involved like linking and interpretation for
scripting languages which we will not dive deep into. Needless
to say, we encourage you to explore these topics independently
anyway.
11. What compilers help us do?
They help translate code from one form to another. Hence we
can write code in easier higher level languages.
This also enables us to 'create' more languages. Python is
written in C. C is written in Assembly. Assembly is written in
Binary. Big circle eh?
12. What does the written code mean?
This is an important part of a tutorial.
We write code.
The compiler translates.
How does the compiler know what we wrote?
Analogy:
Writer writes a novel.
We don't understand the words
We refer to a dictionary.
Exactly the same.
13. Compiler haz a dictionary
All the code that you write uses some keywords and syntaxes.
All these keywords and syntaxes are defined for the compiler
by the hardworking people. That definition contains the
following:
"Listen carefully compiler-kun! If you find <name of syntax> in
the code, it means <meaning>."
So the compiler, very obediently, in a loose definition, just
replaces all the code with whatever is the definition. Just the
way we do when we are reading a story.
14. These dictionaries can be made by you
also on a higher level
The most basic syntaxes and functions are defined in the compiler's dictionary. We can now use these
basic building blocks to make more advanced dictionary.
When we define a 'function' in our code, the compiler references that definition to understand what it
means when I use it.
Example:
add(a, b) {
return a + b;
}
main() {
add(4, 5);
}
Here the compiler understands the symbols like + ; ( ) { } and words like return. It does not however know
what is add until you define it. That is why the first 3 lines define it. The compiler takes in every symbol
and word and tries to make a sign for it. If it doesn't find a definition it barfs and gives an error like:
"undefined reference to xxx"
15. Stay tuned for part two
Next we learn:
1. What do libraries mean? Why do you need them?
2. What does 'linking' mean?
3. What does an executable contain?
4. What is the difference between compiled and interpreted?