2 List - "max.h" a, b max List - max "max.h" #define max(a, b) ((a) > (b)? (a) : (b)) max List -2 List -2 max #include "max.h" int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max(x, y) = %d\n", max(x, y)); "max.h" EOF
3 "max.h" Fig.- #define max(a, b) ((a) > (b)? (a) : (b)) EOF #define max(a, b) EOF ((a) > (b)? (a) : (b)) Ÿ EOF Ÿ Fig.- #define #define #include preprocessing directive 4 4 4 4 4 4 4 4 4
4 "max.h" List -2 '0x8' '0x40' Fig.-2 x, y int int x, y; Fig.-2 white-space character # List -3 fopen fclose
5 List -3 detab "detab.c" #include <stdlib.h> --- srcdst --- void detab(file *src, FILE *dst, int width) int ch; int pos = ; while ((ch = fgetc(src))!= EOF) int num; switch (ch) case '\t': num = width - (pos - ) % width; for ( ; num > 0; num--) fputc(' ', dst); detab pos++; break; "test.c" case '\n': 4 fputc(ch, dst); pos=; break; default: fputc(ch, dst); pos++; break; > detab -t4 test.cÿ 8 int main(int argc, char *argv[]) int width = 8; 8 FILE *fp; > detab test.c xyz.cÿ if (argc < 2) detab(stdin, stdout, width); else while (--argc > 0) if (**(++argv) == '-') if (*++(*argv) == 't') width = atoi(++*argv); else fputs("\n", stderr); return (); else if ((fp = fopen(*argv, "r")) == NULL) fprintf(stderr, "%s\n", *argv); return (); else detab(fp, stdout, width); fp fclose(fp);
6 List -4 List - List -4 max "max.h" #define max (a, b) ((a) > (b)? (a) : (b)) max 2 TRUE #define TRUE TRUE ( ) ( List -4 max ( Fig.-3 max (a, b) ((a) > (b)? (a) : (b)) #define max(a, b) ((a) > (b)? (a) : (b)) max(x, y) (a, b) ((a) > (b)? (a) : (b))(x, y) #define max(a, b) ((a) > (b)? (a) : (b)) max(x, y) ((x) > (y)? (x) : (y)) Fig.-3
7 ( 4 4 4 4 ( z = max (x, y); max( ( ) max 4 4 z = (max)(x, y); ( ) List -5 List -5 --- --- #define max(a, b) ((a) > (b)? (a) : (b)) --- --- int (max)(int a, int b) puts("max"); return (a > b? a : b); x5ÿ y7ÿ max(x, y) = 5 max (max)(x, y) = 5 int x, y; printf("x"); printf("y"); scanf("%d", &x); scanf("%d", &y); printf("max(x, y) = %d\n\n", max(x, y)); printf("(max)(x, y) = %d\n", (max)(x, y));
8 # # # # include #if defined( DOHC ) #include <double.h> #else Fig.-4 #include <single.h> #endif Fig.-4 Fig.-4 #if Fig.-5 a = x; a = x; ax Fig.-5 Fig.-6 #if ax Fig.-6 #if 0 a = x; 0 #endif #if
9 #if 0 #if0 if 0 #if Fig.-7 # # if if # if Fig.-7 List -6 List -6 #if #define DEBUG 0 a5 int a = 5; int x = ; #if DEBUG == a = x; ax #endif printf("a%d\n", a); a DEBUG 0 DEBUG #if
0 2 #define para 0 #define para 0 #include "max.h" #include "max.h" "abc.h" max "max.h" #include "max.h" "max.h" #include "abc.h" "max.h""abc.h" "max.h" "def.h" 4 4 int a; #include "def.h" #include "def.h" List -7 List -7 "max.h" #if!defined( MAX) #define MAX #define max(a, b) ((a) > (b)? (a) : (b)) #endif MAX MAX max MAX!defined( MAX)
max a, b, c, d x = max(max(a, b), max(c, d)); Fig.-8 x = max(max(a, b), max(c, d)); x = ((((a) > (b)? (a) : (b))) > (((c) > (d)? (c) : (d)))? (((a) > (b)? (a) : (b))) : (((c) > (d)? (c) : (d)))); Fig.-8 x = max(max(max(a, b), c), d); Fig.-9 x = max(max(max(a, b), c), d); x = ((((((a) > (b)? (a) : (b))) > (c)? (((a) > (b)? (a) : (b))) : (c))) > (d)? (((((a) > (b)? (a) : (b))) > (c)? (((a) > (b)? (a) : (b))) : (c))) > (d)); Fig.-9 > if x = a; if (b > x) x = b; 4 if (c > x) x = c; if (d > x) x = d;
2 max List -8 inline inline function List -8 // // max++ "max.h" // //--- ---// inline int max(int a, int b) return (a > b? a : b); max(x++, y) ((x++) > (y)? (x++) : (y)) 2 int function overloading "max.h" List -9 cplusplus List -9 max++ "max.h" #if defined cplusplus ++ inline int max(int a, int b) return (a > b? a : b); inline long max(long a, long b) return (a > b? a : b); inline double max(double a, double b) return (a > b? a : b); #else #define max(a, b) ((a) > (b)? (a) : (b)) #endif
3 max > int, long, double function template max "max.h" List -0 List -0 max++ "max.h" #if defined cplusplus ++ template <class Type> Type max(type a, Type b) return (a > b? a : b); #else #define max(a, b) ((a) > (b)? (a) : (b)) #endif int double List - List - // // max++ // #include <iostream> #include "max.h" using namespace std; x5ÿ y7ÿ max(x, y) = 5 int x, y; cout << "x"; cin >> x; cout << "y"; cin >> y; cout << "max(x, y) = " << max(x, y) << endl;
4 0 int x = 5; 5 initializerx 5 List -2 List -2 int n = 4.5; n = 4 printf("n = %d\n", n); int y = 97.2; int y 97.2 97
5 4 4 4 4 4 int m = 3; int x; x = 0; const int a; int b; a variable a a 053 32.5 'x' objecta b Fig.-0 Fig.-0 nameidentifier B. W. Kernighan and Dennis M. Ritchie The C Programming Language variable object
6 List -3 List -3 #include <math.h> void func(int no) register int i; auto int x = 00; printf("x = %d\n", x); for (i = 0; i < no; i++) double x = sin((double)i / no); printf("x = %f\n", x); printf("x = %d\n", x); x = 00 x = 0.000000 x = 0.099833 x = 0.98669 x = 0.295520 x = 0.38948 x = 0.479426 x = 0.564642 x = 0.64428 x = 0.77356 x = 0.783327 x = 00 func(0); func no i x func automatic storage duration Fig.- auto register Fig.- auto register register
7 func x x for x for for x x for sin((double)i / no) List -4 List -4 x = 957 int x; printf("x = %d\n", x); x 957-38 0
8 static storage duration List -5 List -5 int ft = 0; void func(void) int at = 0; static int st = 0; ft++; at++; st++; printf("ft = %d at = %d st = %d\n", ft, at, st); ft = at = st = ft = 2 at = st = 2 ft = 3 at = st = 3 ft = 4 at = st = 4 ft = 5 at = st = 5 ft = 6 at = st = 6 ft = 7 at = st = 7 ft = 8 at = st = 8 int i; for (i = 0; i < 8; i++) func(); ft st static Fig.-2 static Fig.-2
9 func ft st 0 func at func 0 ft st ft st int ft; void func(void) int at = 0; static int st; 0 void func(void) int x = sin(0.9); static int st = sin(0.9); Table - Table - main 0
20 List -6 List -6 x = 9572 int x = ; int x = x; printf("x = %d\n", x); main x x x x x x List -7 List -7 int z = ; int x = z; int z = 0; int y = z; printf x, y, z
2 int i = 5; ++ int i(5); // ++ List -8 List -8 // // ( )++ // #include <iostream> using namespace std; x = 5 y = 5 int x = 5; x5 int y(5); y5 cout << "x = " << x << endl; cout << "y = " << y << endl; Complex class Complex double re, im; public: Complex(double r, double i = 0.0) re = r; im = i; //... ; // // Complex Complex a(5.0, 7.5); // Complex b(5.0); // ( ) ( ) Complex b = 5.0; // int double
22 int a[3] =, 2, 3;, 2, 3 a, 2, 3 a[0], a[], a[2], 2, 3 List -9 List -9 int vx[3] =, 2, 3; vx[0] = ma[0] = ms[0] = vx[] = 2 ma[] = 2 ms[] = 2 int i; vx[2] = 3 ma[2] = 3 ms[2] = 3 int ma[3] =, 2, 3; static int ms[3] =, 2, 3; for (i = 0; i < 3; i++) printf("vx[%d] = %d ma[%d] = %d ms[%d] = %d\n", i, vx[i], i, ma[i], i, ms[i]); K&R static ma List -20 3 b 0 Fig.-3 b[] b[2] 0 0 Fig.-4
23 List -20 0 int i; int b[3] = ;,0,0 if (b[]!= 0 b[2]!= 0) puts(""); else for (i = 0; i < 3; i++) printf("b[%d] = %d\n", i, b[i]); b[0] = b[] = 0 b[2] = 0 int b[3] = ; int b[3] =, 0, 0; Fig.-3 int x[000] = 0; 0 0 Fig.-4 2 int c[2] =, 2, 3; int d[3]; static 0 static p.7 int x[3]; x = 0,, 2;
24 List -2 List -2 int i, j; int x[3][2] = 0,, 2, 3, 4, 5, ; for (i = 0; i < 3; i++) for (j = 0; j < 2; j++) printf("x[%d][%d] = %d\n", i, j, x[i][j]); x[0][0] = 0 x[0][] = x[][0] = 2 x[][] = 3 x[2][0] = 4 x[2][] = 5 x Fig.-5 x[0][0], x[0][], x[][0], x[][], x[2][0], x[2][] int x[3][2] = 0,, 2, 3, 4, 5, ; 0 2 3 4 5 x[0][0] x[0][] x[][0] x[][] x[2][0] x[2][] Fig.-5 int x[3][2] = 0,, 2, 3, 4, 5, ;, int x[3][2] = 0,, 2, 3, 4, 5 ; Fig.-6
25,, Fig.-6 int d[3] =, 2, 3,;, 0 Fig.-7 int x[3][2] = 0, 2, 3, 4, ; int x[3][2] = 0, 0, 2, 3, 4, 0, ; 0 0 2 3 4 0 x[0][0] x[0][] x[][0] x[][] x[2][0] x[2][] Fig.-7 Fig.-8 int x[3][2] = 0,, 2; int x[3][2] = 0,, 2, 0, 0, 0, ; 0 2 0 0 0 x[0][0] x[0][] x[][0] x[][] x[2][0] x[2][] Fig.-8
26 typedef typedef typedef typedef int INTEGER; INTEGERint INTEGER int INTEGER a; aint int a; aint typedef List -22 int 5 Int5ary typedef List -22 typedef int i; typedef int Int5ary[5]; int5 Int5ary x =, 2, 3; for (i = 0; i < 5; i++) printf("x[%d] = %d\n", i, x[i]); x[0] = x[] = 2 x[2] = 3 x[3] = 0 x[4] = 0 typedef 5 x 0 int x[5] =, 2, 3;
27 List -23 List -23 typedef int i; typedef int IntAry[]; int IntAry a =, 2, 3; 3 IntAry b =, 2, 3, 4, 5; 5 for (i = 0; i < 3; i++) printf("a[%d] = %d\n", i, a[i]); for (i = 0; i < 5; i++) printf("b[%d] = %d\n", i, b[i]); a[0] = a[] = 2 a[2] = 3 b[0] = b[] = 2 b[2] = 3 b[3] = 4 b[4] = 5 int IntAry typedef a b IntAry a 3 b 5 a, b int a[3] =, 2, 3; int b[5] =, 2, 3, 4, 5; IntAry