Created on: 3/20/05 Last edited on: 3/20/05 LESSON 0-a - THE C-SUBSET C++ is an extension of C. Anything you can do in C, you can do in C++. In fact, a C program IS a C++ program for all intents and purposes. Sure, you may have to adjust a few things around, but you hardly have to change anything. The main difference between C and C++ is that C++ brings C into the realm of object-oriented programming. For the sake of being backwards compatible, I will teach you how to use C-style Input and Output (I/O), how to use malloc(), C-strings, and possibly some other things as well. Your computer can not run a C++ program, or a C program. Your computer is only capable of running a program written in it's own language. If you are using Windows, then that language is called x86 assembler. If you are using a Mac, I have no clue what the assembly language is called. But know this - Windows and Macs speak completely different languages, so a program on a Windows machine can't be run on a Mac. However, with the C++ and C language, it is possible to translate, or compile, a C++/C program into the language that your computer understands. Because of this, you can bridge the barrier between Windows and Macs because both languages have compilers which translate the program into a language that they understand. This is a trait which is shared by all high-level languages, and is the main advantage to using a high-level language. So long as you can find a compiler for a computer, you can run a C/C++ program on it. (As a side note: Java specializes in this trait. So much so, in fact, that you can run Java programs from straight off of the internet). Here is the process of how a C/C++ program is translated into a program that your computer can understand: 1.) You have created source code to be compiled. 2.) First, the C pre-processor takes your computer, and searches for lines of code called "pre-processor directives". These lines of code are ALWAYS preceded by a '#' symbol. The pre-processor the produces new source code which has been modified to satisfy the demands of the pre-processor directives (just know that these exist for now, OK?). 3.) This new source code is handed to a compiler. The compiler takes the program and modifies it into something that your computer can almost understand (for example, your computer doesn't know how to add 3 numbers together, but it DOES now how to add 2 numbers together. Thus a statement such as: 1+2+3 = 6 would become 2 statements: 2 + 3 = 5 1 + 5 = 6 (can't forget the closing parenthesis here:)) Your computer knows how to do that, though it only knows how to do this in binary. The compiler then takes this code and either translates it into assembly, or straight into machine code (if it is translated into assembly, the compiler then hands it to an assembler to translate it into machine code). The compiler also creates a "symbol table", a list of all the names that you use in your program for functions and variables. After all of this is done, your compiler may try to "optimize" your code. Quite frequently, what a program is compiled, the compiler will produce redundant statements. When the compiler optimizes your code, it tries to find and eliminate any redundant statements, and statements which have no purpose (empty loops, for example. You'll understand what I'm talking about later). 4.) The compiler hands the symbol table and code to the Linker. It is the linkers job to produce the final product. The code that you have is very close to being understandable to a computer. The Linker translates the symbols in your symbol table into memory addresses, and formats your program so that it can be read by your computers operating system. As I said before, C/C++ isn't capable of doing very much on it's own. For example, C & C++ does NOT have any way to input or output information. However, your operating system has a set of useful machine code programs which input and output various information. How useful. The linker links your program to what ever system-library programs it needs to, and then produces an executable image, which your computer is entirely capable of understanding and running. There the compilation process is done. That was quite a mouthful, wasn't it?