A Brief Review of C by John H. Mathews (to accompany his book "NUMERICAL METHODS: C Programs", Prentice Hall,1995). Specified Columns Form of a C Program (<#include statements>) // ( and are necessary here) {} function A; function B; begin {} {} A; {} B; {} end. Form of a C function [()]; { {} {} i.e. function body return (); } Form of a C Procedure or Subroutine (Function which returns no value) void [()] { {} {} i.e. procedure body } Form of a Call to a Procedure. [] Very important note C is a pass-by value language. In order to change the contents of a variable, three changes must be made to the notes above: 1) in the , the variable must be preceded by a &: ProcedureA (&x, y, z) 2) in the , a * must be inserted between the type and the variable name: void ProcedureA (int *x, int y, int z); 3) in the of the function, a * must precede the variable name: ... *x = *x - y * z Definition of Constants #define Max 10; #define Pi 3.1415926535; #define Small 1e-20; Specification Statements (Declaration Part) typedef float VECTOR[5]; typedef float MATRIX[5][5]; typedef int POINTER[Subs]; typedef enum (Done,Iterating,Working) STATUS; typedef char WORDS[80]; int Column,I,J; float A0,B0,Max,X,Y; Flag: boolean; V,R: VECTOR; A,D: MATRIX; Row: POINTER; Name: string[80]; Lines: WORDS; State: STATUS; Note about arrays Array subscripting begins at 0 in C, and ends at N-1 for an array of N elements example: int myArray[20] has 20 elements; myArray[0] accesses the first, and myArray[19] accesses the last. Assignment Statements X = A[1] + 3; Y = A*X*X + B*X + C; Flag = TRUE; Name = "Fran"; Arithmetic Operations + Addition - Subtraction * Multiplication / Division / Integer Division % Remainder (Integer) Relational Operators == Equal to != Not equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to Logical Operators ! Complement && True if both operands are true || True if either (or both) operands are true Comments // This is a single-line comment. /* This is a mulit-line comment.*/ Unconditional Transfer goto 100; ... 100: Warning. A GOTO statement must not leave the current block. IF (Block) Control Statement Performs the series of {} following it or transfers control to an else, or end statement, depending on the value of the (). if () {}; if () { {}; } if () {}; else {}; if () { {}; } else { {}; } if () { {} } else { if () { {} } else { {} } } Indexed Loop Control Statement (for loop) for ( ; ; ) for ( ; ; ) { ... } initialize loop-control variable(s) loop stops cycling when test becomes false increments loop-control variable(s) Sum = 0; for (k = 0 ; k <= 100 ; k = k+1) Sum = Sum + k; Sum = 0; for (k = 1000 ; k >= 1 ; k = k-1) Sum := Sum + 1.0/k; for (j = 1 ; j <= 5 ; j = j+1) for (k = 1 ; k <= 5 ; k = k+1) A[j,k] = 1.0/(j+k-1); While Loop Structure The is evaluated first. If the logical expression is true, then the loop body will be executed. The sequence is repeated as long as the expression remains true. If the expression is false, then the loop body will be skipped, and execution will continue with the statement following the }. while () { {}; } The following reserved words may be used in loops break; exits innermost loop continue; jumps to end of loop, then begins next loop cycle Switch Statement Switch (FunType) { case 1: F = x; break; case 2: F = x*x; break; default: F = x*x*x; } Input and Output scanf: (%i to read int, %f to read float, %lf to read double) (Variable name must be preceded by ampersand '&') scanf ("%i %f", &x, &y); scanf ("%lf", &A[i,j]); printf: (%i to print int, %f to print float or double) (a.b between % and flag indicate: a: Minimum field width b: Precision of number) printf ("\n"); // Prints a blank line printf ("x = %i y = %5.4f\n", x, y); printf ("A(i,j) = %15.5f", A[i,j]); Mathematical Functions Functions returning type double: cos(x) cosine (radians) sin(x) sine (radians) exp(x) exponential exp(x) arctan(x) inverse tangent (radians) ln(x) natural logarithm base e sqrt(x) square root fabs(x) absolute value (x is of type double) Functions returning type int: abs(x) absolute value (x is of type int)