Aims and Objectives
Splitting programs across files
Scope Rules in C
Using Header Files
Recursion
The C Preprocessor
Macro Substitution
int a = 2; /* global */ double foo(double x, int y) { extern int a; int w = 34; /* local */ return (double) ((x * w) + (y * w)) * a; }
static int a = 2;
// Header file name = num.h #define THESTRING "hello, world\n" int binsearch(int x, int v[], int n); int getnum(int v[], int max);
#include "num.h"
/* p1.c */ #include <stdio.h> #include "num.h" main() { int v[20]; int n, i; int x = 5; printf(THESTRING); for (i = 0; i < 20; v[i++] = 0) ; n = getnum(v, 20); printf("%d is in array cell %d\n", x, binsearch(x, v, n)); }
/* p2.c */ #include <stdio.h> static const int NUM=1; static const int ALPHA=0; static int isnum(int c); int getnum(int v[], int max) { int c, i; for (i = 0; i < max && (c = getchar()) != EOF;) if (c == '\n') { ++i; } else if (! (isnum(c))) { printf("Input contains non-integer\n"); v[i] = 0; while ((c = getchar()) != '\n' && c != EOF) ; } else { v[i] = (v[i] * 10) + (c - '0'); } return i; } int isnum(int c) { extern int NUM; extern int ALPHA; if (c >= '0' && c <= '9') return NUM; else return ALPHA; }
/* p3.c */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low + high) / 2; if (x < v[mid]) { high = mid - 1; } else if ( x > v[mid]) { low = mid + 1; } else /* found match */ return mid; } return -1; /* no match */ }
// member.c /* is x a member of the list s? */ #include <stdio.h> #include <string.h> int member(int x, char s[]); main() { char x = 'h'; /* character to find */ char s[] = "look in this string"; /* search in this array */ printf("%c is %s \"%s\"\n", x, member(x, s) ? "in" : "not in", s); } int member(int x, char s[]) { if (strlen(s) == 0) return 0; else if (x == s[0]) return 1; else { return member(x, ++s); } }
// binsearchR.c /* recursive binsearch */ int binsearch(int x, int v[], int from, int to) { int mid; mid = (from + to) / 2; if (from > to) return -1; /* no match found */ else if (x < v[mid]) return binsearch(x, v, from, mid - 1); else if (x > v[mid]) return binsearch(x, v, mid + 1, to); else return mid; } /* iterative binsearch */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low + high) / 2; if (x < v[mid]) { high = mid - 1; } else if ( x > v[mid]) { low = mid + 1; } else /* found match */ return mid; } return -1; /* no match */ }
#define symbolic_constant string
#define max(a, b) ((a < b) ? (b) : (a))
allows you to create a macro (which happens to look like a function) - but max(a, b) is replaced by the expression prior to compilation
max(2, 3) /* in the program */
is replaced with
((2 < 3) ? (3) : (2))
and
max(p+q, q+r)
is replaced with
((p+q < q+r) ? (q+r) : (p+q))
max(p++, q++)
#define getc(fp) ((fp)->cnt-- ? (int) *(fp)->ptr++ : __getc(fp))
#define getchar() getc(stdin)
#define putc(c, fp) ((fp)->cnt-- > 1 ? (int)(*(fp)->ptr++ = (c)) : __putc(c, fp))
#define putchar(c) putc(c, stdout)
int getchar(int);
int putchar(void);
#if conditional integer expression statement #elif conditional integer expression statement #else statement #endif