Wednesday, September 25, 2013

C++ Tutorial #1: How Code Becomes a Program

Part 1 of the C++ tutorial, in which I try to explain (as efficiently as possible) how code works...

Modern-day programming, regardless of language, utilizes two basic types of code: machine code and source code.

Machine code is just another way of saying binary code—it's a bunch of 0s and 1s.  The reason it's called "machine" code is because a computer is indeed a machine that operates on electrical signals.  The 0s and 1s are comparable to switches being "off" and "on" respectively.  Think of machine code/binary as Morse code for computers.  It is the one and only language a computer understands.

However, it would be next-to-impossible to write modern programs in binary—just think of all those wacky combinations of 0s and 1s we'd have to understand.  Thus, people created more understandable languages, like C++, to make things easier.  C++ is what we call a high-level programming language, meaning it's very close to English (at least, compared to binary).  As a programmer, you'd be much happier working with C++ than machine code.

Still, one question remains: if you write a program in C++, but the computer only understands binary, how do you get a C++ program to run?

The answer is that you translate it.  After you write code, you have to compile it, or translate it into binary.  So you actually end up with two files for every program you write: the original file with the C++ code you wrote (this is called source code) and the new file, translated into binary, that the computer can understand (this is the executable file, which is what people who use your program would run).

How do you translate source code into machine code?  Well, you use a special type of program called a compiler, which will so kindly translate and create an executable file for you.  If you own a Mac computer with OSX, you already have a compiler.  However, if you use Windows on any type of computer, you'll need to install one yourself.  But fear not—just pay close attention and you'll get it.

I'll tackle actually installing the necessary programs in the next post, but for now, just review these terms:

Machine code: also, "binary".  A code of 0s and 1s that the computer understands as language.
Source code: the high-level code (such as C++) used in programming.
Compile: to translate high-level code into machine code.
Compiler: the program that does this for you.
Executable file: a file with source code translated into machine code.

As usual, if you have any questions, feel free to comment or email me.


1 comment: