Introduction to C
+ Back to Teaching Page +
1. How might C programming be useful to me?
Lots of arithmetic operations done quickly
Repeatable 'random' number sequences
Problems with varying initial conditions
and many more examples...
C is less useful for visualizing data and too cumbersome for 'quick' math (IDL, Python, R, etc. are better).
A much more extensive introduction to C can be found here.
2. Tools for writing a C program
Aquamacs emacs* (see image ->)
really, any text editor will do...
*We'll use these two.
3. A Simple Program
This program illustrates scientific notation, exponents and arithmetic operations.
The Basic Elements of a C Program
Libraries - including these links to functions and definitions you will need in your program. Common ones are stdio.h, math.h, time.h, etc.
Comments - delimited by /* */ or // for a single line. These help you remember the purpose of the code later on or help another user figure out what crazed ideas you had.
main( ) - appears in every program, essentially marks the beginning of the 'meat' of the program.
Variables - the worker bees of your program. They can be numbers, strings (words), constants, arrays, matrices, etc.
Integers, Floats, and Doubles - types of numbers, each with different precision and properties.
Commands - anything that does something (I know, vague, but there are too many!).
Syntax - don't forget semicolons, brackets, and parentheses! Variables and your functions (we'll get to those below) have to be declared before they are used. Organized style, while not necessary, should make your program easier to understand.
>>>> Computer Lab Portion Starts Here <<<<
4. Compiling, Debugging (boo! hiss!) & Running
Copy the program above to a directory, then go there.
Before you can run a program, it must be compiled. Typical compilers are cc or gcc, and in order to compile the program linked above from the terminal, you would enter (the '>' is the prompt, just enter everything following it):
> gcc -Wall numbers_operations.c -o numop -lm
-Wall: this flag generates more descriptive information than just an error if the compiler runs into any trouble.
-o numop: this names the compiled program 'numop' rather than the default, which is 'a.out'.
-lm: this links to the math library (may or may not be necessary). In this case the flag ensures that the 'pow( )' function will be recognized.
There are other flags you can use including optimizers (which make the code run faster) and others that give you different formats for your error messages.
In the unlikely event that you don't get any error messages, the code has compiled successfully, the compiled program file is written to the current directory, and you give yourself a pat on the back for meticulous code writing. In the far more likely scenario when you get error messages, you need to debug your code by figuring out where you went wrong and fixing it. Sounds simple, but the process will often test your sanity. Derek Richardson's rule about debugging is very useful here - always start with the first reported error! One simple typo in your code may make it seem like your code is riddled wth errors, when you might just be missing a semicolon.
Now that you've debugged your code and it compiled successfully, you can run it from the terminal by entering:
> . /numop
You should see a bunch of numbers printed in the terminal as calculated and displayed by the program. Check the results against what is written in the program code. Notice anything strange?
Note: in order to compile programs on a Mac in the terminal or using X11, you may need to install Xcode.
5. A Less Simple Program
This program does a basic numerical integration using a function, a control statement (loop), and input values passed to main( ) (passed to main = entered separated by spaces after ./numop) or input during running. It also outputs a file with each integration step number and the cumulative total up to that step called 'output.txt'.
Now you're ready to move on to Aaron Skinner's Advanced C class.