Some Remarks About C [Since so many people in this class already know C, the following remarks are included more for completeness than to indicate what was discussed in class.] The C programming language has been used extensively in class examples. However, not everyone is familiar with C. One advantage of learning C is that programming constructs in the AWK language are very much like C. One good think about C is that it is a "strong typing" language. This means you must declare all your variables. The program lint can be used to help you find bugs in your code even before you try to compile it. It checks that variables have been declared and that they are not used before being initialized. C declares variables with statments like: int i; float r; However, C also has some special variables called pointers. int *j_ptr; float *s_ptr; declare that j_ptr is the address of an integer, i.e., j_ptr points to an integer. s_ptr points to a floating point variable. In Fortran a subroutine can be used to change many of its arguments, but in C, there are only functions. A function can only return one value. However, if the arguments to the C function are pointers to variables you want changed, then the function can reach out and touch those variables! If you have a pointer to a variable, you can use the operation of dereferencing to get the value stored at the address. For example float y; y = *s_ptr; sets the variable y to the value stored in the address s_ptr. So, *s_ptr is a floating point value. However, a statment like y = s_ptr; is NOT valid because y is a float and s_ptr is a pointer or address. To get the address of a variable, you just put an ampersand, &, in front of the variable name. Here is an example from ~sg/chap3/euler_cromer.c: euler_2d(&y,&v,&accel,&t,g,dt,nsteps); Down in the euler_2d subroutine, we have derefencing is in: *v_ptr = (*v_ptr) + (*accel_ptr)*dt; Control structures in C The for loop is used extensively in C. The word for is followed by three statements within parentheses. The statment that follows is executed under control of the loop. If you want more than one statement, enclose them in curly braces { and }. Consider as an example: for (i=1; i <= nsteps; ++i) printf ("%d\n",i); Within the parentheses after for is first an initialization statement i=1. This sets i to 1 before the loop starts executing. One can actually have more than one initialization if they are separated by commas. The next part i <= nsteps is a logical condition that is tested at the beginning of the loop. If it is true, the loop is executed. At the end of the loop, the third part within the for statement is executed. ++i is short for i = i + 1. Format statment in C %d integer variable %f floating point variable 6.3 %e exponential or scientific format 1.03e-10 %le double precision exponential format %s string variable