c_from_first_principles
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
| — | c_from_first_principles [2008/05/12 10:04] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== C from first principles ====== | ||
| + | This is a C programming tutorial for those who know nothing about programming but want to understand fully the reasons why things are done, not just rote learning. | ||
| + | -- // | ||
| + | |||
| + | |||
| + | ===== Getting set up ===== | ||
| + | //This section needs expanding// | ||
| + | For this tutorial we will use GCC and the standard Unix command line tools. | ||
| + | * Windows: Install [[http:// | ||
| + | * Mac OS X: Install XCode | ||
| + | * Linux: You should already have gcc installed | ||
| + | |||
| + | ===== A brief history of C ===== | ||
| + | C was created by Denis Ritchie at Bell Labs (then part of AT&T) in 1972/73 for porting the Unix operating system to the PDP-11 (an early minicomputer). | ||
| + | |||
| + | For more information, | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | * [[http:// | ||
| + | |||
| + | |||
| + | |||
| + | ===== How C gets from your brain to your computer ===== | ||
| + | Computers understand only machine code, a series of numbers encoding the instructions to execute. | ||
| + | < | ||
| + | 0000 1110 0010 1010 | ||
| + | 0110 1110 0000 00011 | ||
| + | </ | ||
| + | |||
| + | Not very easy to understand, is it? If we associated each instruction with a short name (mnemonic) then it would be easier to write down: | ||
| + | < | ||
| + | MOVLW 42 | ||
| + | MOVWF 3 | ||
| + | </ | ||
| + | |||
| + | But we'd still have to translate it by hand before giving it to the microcontroller. | ||
| + | < | ||
| + | answer | ||
| + | |||
| + | MOVLW 42 | ||
| + | MOVWF answer | ||
| + | </ | ||
| + | |||
| + | Taking the idea an step further, we could enhance the assembler program so that it took a higher level language. | ||
| + | < | ||
| + | int answer = 32; | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Working with the Unix command line ===== | ||
| + | The usual way of interacting with a Unix system is via the command line, also known as the shell. | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * .///program name// runs a program in the current directory. | ||
| + | |||
| + | Unlike Windows, a Unix shell expands wildcards such as '' | ||
| + | < | ||
| + | mv *.c | ||
| + | </ | ||
| + | and there were only 2 files matching '' | ||
| + | |||
| + | ===== Lesson 1: Your first C program ===== | ||
| + | To start with, open up a text editor (such as notepad on Windows or nano on Linux) and type this in: | ||
| + | < | ||
| + | |||
| + | int main(int argc, char **argv) | ||
| + | { | ||
| + | printf(" | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Save the file as '' | ||
| + | |||
| + | Now let's examine each line to find out what it does: | ||
| + | < | ||
| + | Tells the preprocessor include the file '' | ||
| + | |||
| + | < | ||
| + | A blank line. You can have these anywhere you want, whitespace between tokens is ignored. | ||
| + | |||
| + | < | ||
| + | Declares a function named '' | ||
| + | |||
| + | < | ||
| + | An open brace starts the body of the function. | ||
| + | |||
| + | < | ||
| + | Print '' | ||
| + | |||
| + | < | ||
| + | Return from the function with 0 as the result. | ||
| + | |||
| + | < | ||
| + | A close brace ends the function. | ||
| + | |||
| + | Now that we know what it does, let's compile it. Open up a shell, change into your home directory if you're not there already and run this command: | ||
| + | < | ||
| + | This runs the gcc program to compile your source file into something your computer can understand. | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | |||
| + | If gcc prints any errors or warnings, check that you typed the program in correctly and saved it in the right place and try compiling again. | ||
| + | < | ||
| + | and you should see it print | ||
| + | < | ||
| + | |||
| + | |||
| + | ===== Lesson 2: Scalar Types and printf ===== | ||
| + | Every piece of data in C has a " | ||
| + | |||
| + | Numbers in C can be either integer (having no fractional part) or floating point (having a fractional part and a wider range, but a limited precision). | ||
c_from_first_principles.txt · Last modified: 2008/05/12 10:04 by 127.0.0.1
