C
505 1 C B.W.Kernighan, D.M.Ritchie, 2 C 3 C from C Programming Language ---- P13. 1-4 ; P20 1-8, 1-9, 1-10; P24 1-13; P31 1-18; P34 1-20, 1-22, 1-23; P46 2-3; P49 2-6, 2-8; P58 3-1; P60 3-2; P63 3-3; P64 3-5, 3-6; P71 4-1; P73 4-2; P79 4-4, 4-6, 4-10; P83 4-11; P88 4-13; P97 5-1, 5-2; P107 5-3, 5-5; P110 5-7; P118 5-13; P121 5-14; P126 5-19; P143 6-2, 6-3 34 20 10
C UNIX C UNIX UNIX Bell Lab K.Thompson D.M.Ritchie 1969 O.S. MIT AE Multies 1970 V1 V2 PDP-7 1971 V3 PDP11/23 1972 V4 PDP11/45 1972 D.M.Ritchie C C B BCPL CPL 1973 Ritchie Thompson C UNIX 90% V5
C 78 C UNIX C C UNIX 78 D.Ritchie C 88 ANSI C C K&R C 88 ANSI C 89 3 CPL ------- BCPL------ B -------- C ------ C++ 1968 C.Strachey 1969 M.Richards 1970 K.Thompson 1972 D.M.Ritchie 1988 B.Stroustrup
C
C C C C C
1 1 C 1 2 C
enum typdef int float char struct union short double long unsigned C
{ } { } if if else while, do_while, for switch goto, continue, break, return auto static extern register C
3 4 32 { } = += *= ++ -- 5 C 20% 6 C 75%~80% C C C
C C PASCAL C
1 2 C 1 2 1 0,1, 9 a,b, z,a,b, Z _ _ _ + - * / % > < ^ ->. & &&,, >>, <<, =, ==,!=, <=, >=, (, ), [, ],,?, :, {, },,, #, C
1.2.2 int auto goto if float static return else char extern break while short register continue for long do unsigned switch double case struct union void enum typedef sizeof const signed default define, undef, include, ifdef, ifndef, endif, line C C
1.2.3 C C C main main main C C.C.h C C
[ 1-1] /* file: hello.c */ main( ) { printf( hello, world\n ); } 5. 1. /* */ 2. main( ) 3. printf I/O 4. \n C \n \t \b \r \f \\ \ \ \ddd \ PASCAL C
[ 1-2] main( ) { /* simple.c */ int a, b, c, sum; a =1; b = 2; } scanf printf scanf( %d, &c); /* */ sum = a + b + c; printf( Sum = %d\n, sum); scanf printf % / %d %d %f %f %c %c %s %s %o %x %% % %d,%f %4d,%6.2f C
1.3 C / vi(unix), edit(dos) =>.o.obj.o(.obj) =>.exe C
1)UNIX Cc [-o ] [ c] o a.out c.o s1.c, s2.c s3.c C cc c s1.c cc c s2.c cc o s s1.o s2.o s3.c 2)DOS Windows DOS WINDOWS C C VC BC 3 / >, >> C> prog > outfile < C> prog < infile C>prog1 prog2 C
2 1 2 1 1 C _
2 1 2 C [ ] int lower, upper, step; char c, line[100]; extern double x; const double PI = 3.1415926; const char msg[ ] = Warning ;
2.2 2 2 1 literal line < 66 ci = 3.141592 * dia; s[i]!= \0 ; #define PAGESIZE 66 #define PI 3.141592 #define ENDSTRING \0 line < PAGESIZE ci = PI * dia; s[i]!= ENDSTRING; 66 3.141592 \0
2 2 2 1 long 1275, 0127, 0x19a, 0xABD, 123l, 89L 2 23.15, -8E-3, -125e+4 3 A, b,? ASCII IBM EBCDIC ASCII A 65 b 98 63 \0, \n, \t, \b, \r, \f \\, \, \ddd ( ddd ddd) if( c >= A && c <= Z ) return ( c + a A );
4 The C Programming Language \0 0 x x \0
2 3 32 16 UNIX, W32 DOS int 32 16 short(int) 16 16 long(int) 32 32 unsigned(int) 32 16 float 32 32 double 64 64 char 8 8 ANSI C const ---- #define const Volatile ----
2 4 int atoi(char s[ ]) { int i, n; n = 0; for(i=0; s[i] >= 0 && s[i] <= 9 ; i++) n = 10*n + s[i] 0 ; return (n); } main( ) { int n; char st[100]; } scanf( %s, st); n = atoi(st); printf( %d\n, n);
C --- 1 8 2 3 int unsigned
4 double float long unsign int short, char
cast --- < > < > x = sqrt((double) n );
2.5 C < >; C PASCAL E1 = E2 C C a = b = c = 0;
2 5 1 < > ::= < > < > < > < > < > < > < > < > ::= < > < > < > < > < >( [< > [, < >]* ]) < >
< > ::= < > < >[< >] < >.< > < >->< > (< >) *< >
1. < > < > < > ::= * &< > -< > +< > ~< >!< > (< >)< > sizeof < > sizeof (< >) ++< > --< > / < >++ < >-- /
[ 1] int x, y; int *px; 01000 X: 10 Y: 10 x= 10; px = &x; PX: y = *px; ( y = x) - < > &(3), &(x+5) - < > ++
[ 2] n = 5; n = 5; x = ++n; x = n++; n 6,? x: 6? x: 5, n: 6
[ 3] #include <stdio.h> main( ) { int a, b, c; a = b = c = 0; a = ++b + ++c; } printf( %d %d %d\n,a, b, c); a = b++ + c++; printf( %d %d %d\n,a, b, c); a = ++b + c++; printf( %d %d %d\n,a, b, c); a = b-- + --c; printf( %d %d %d\n,a, b, c); a = ++c + c; printf( %d %d %d\n,a, b, c); /* 2 1 1 */ /* 2 2 2 */ /* 5 3 3 */ /* 5 2 2 */ /* */
2 < > ::= < > < > > < > ::= * / % + - >> << > < >= <= ==!= & ^ &&, *, /, %, +, - >>, << >, <, >=, <=, ==,!= &, ^, &&,
[ ] x p n unsigned getbits(unsigned x, unsigned p, unsigned n) { } return ( ( x >> (p + 1 n)) & ~(~0 << n));
A && B A B A 0 B 0 A 0 B 0 >> int unsigned 0 << 0 / % e1,e2 e1 e2 e2 a = (t = 3, t+2); 5
3 < > ::= < 1>? < 2> : < 3> 1 2 3 if( a > b) else z = a; z = b; z = ( a > b)? a: b;
4 < > ::= >< > < > ::= = += -= /= %= *= >>= <<= &= ^= = (e1) op= (e2) op (e1) = (e1) op (e2) x += n; x = x+n; [ ] (e1) op= (e2) e1 (e1)=(e1)op(e2) a[++i] *= n a[++i] = a[++i] * n
bfreelist_frow->b_back += 2 x += n x = x+n y *= n+1; y = y*(n+1) y = y*n+1
2 5 2 1 ( ) [ ]. -> 2 -! ~ ++ -- & * ( ) sizeof 3 * / % 4 + - 5 << >> 6 < <= >= > 7 ==!= 8 & 9 ^ 10 11 && 12 13? : 14 *= /= %= += -= <<= >>= &= ^= = 15
3 1 C
1 *x++ = y *= z+3; ++x; --y; 2 goto break; continue; return; return ( ); 3
1 { [< >] < >* } } 2 if if_else 3 for while do_while 4 switch case
3 2 F if( ) T if( ) F else 1 2 1 T 2
if else if( n > 0) if(a >b) else z = a; z = b; else else if else if {} if(n > 0){ } else if(a >b) z = a; z = b;
if else if if( ) else if( ) else
[ 3-1] #include <stdio.h> main( ) { int a, b; char ch; scanf( %d %d, &a,&b); ch = getchar( ); if( ch = = y ){ int x; x = a; a = b; b = x; printf( a = %d b= %d\n, a, b); } else printf( no swap!\n ); }
3.3 while while F T
[ 3-2] #define EOF 1 main( ) { int c; while(( c = getchar( ))!= EOF) putchar( c ); }
[ 3-3] 2 1 3 2 5 4 6 5 7 main( ) { } int x = 3; while(x%3!= 2) x +=2; while(x%5!= 4) x += 6; while(x%6!= 5) x += 30; while(x%7!= 0) x += 30; printf( x = %d\n,x);
3.4 for for 1 2 3 1 1 while 2 { 2 2 T F } 3 3
for for( i=0; i<10; i++)
[ 3-4] int atoi(char s[ ]) { int i, n, sign; for(i=0; s[i] = = s[i] = = \n s[i] = = \t ; i++) ; /* skip white space */ sign = 1; if(s[i] = = + s[i] = = - ) sign = (s[i++] = = + )?1:-1; for(n=0; s[i] >= 0 && s[i] <= 9 ; i++) n = 10*n + s[i] 0 ; return ( sign * n); }
[ 3-5] void reverse(char s[ ]) { int c, i, j; for(i=0, j=strlen(s)-1; i < j ; i++,j--){ c = s[i]; s[i] = s[j]; s[j] = c; } } int strlen(char s[ ]) { int i=0; while(s[i]!= \0 ) ++i; return (i); }
[ ] for( ; (c = getchar( ))!= EOF; ) for( ; ;) s; s; 2 break return while(a) B; for(; A; ) B;
3.5 do_while do while ( ) F while do_while T
while(for) do_while [ ] do_while Pascal Repeat untill --- False True
[ 3-6] void itoa(int n, char s[ ]) { } int i, sign; if((sign = n) < 0) n = -n; i = 0; do { s[i++] = n%10 + 0 ; } while( (n /= 10 ) > 0); if(sign < 0) s[i++] = - ; s[i] = \0 ; reverse(s); do_while {} while while
for while do_while
3 6 (switch) switch ( ) case default
switch swich { case 1 1 } case 2 2 case n n default n+1 C1 C2 E Cn default S1 S2 Sn Sn+1 1) 2) case 3) 4) default default
[ ] 1) 2) switch 3) default default switch 4) case default pascal case switch break; 5) case C switch Pascal case C default
[ ] char score; switch(score) { case E : printf( no pass\n ); break; case D : printf( pass\n ); break; case C : printf( better\n ); break; case B : printf( right\n ); break; case A : printf( all right\n ); break; }
3.7 break continue goto ; ; return 1 return; 2 return [ ] 1 main return 2 return
goto goto goto goto int i, j, k, x; float y; for(i = ) { for(j = ) { for(k = ) { scanf( %d %f, &x, & y); if(x < 0 && y < 0.0) goto inerr; } } } inerr: printf( error in input data!\n );
[ 3-7] #define MAXLINE 1000 int getline(char s[ ], int lim); main( ) { } int n; char line[maxline]; while(( n = getline(line, MAXLINE)) > 0) { } while( --n >= 0) if(line[n]!= && line[n]!= \n && line[n]!= \t ) break; line[n+1] = \0 ; printf( %s\n, line);
int getline(char s[ ], int lim) { int c, i; for(i=0; i < (lim-1) && (c = getchar( ))!= EOF && c!= \n ; i++) s[i] = c; if( c = = \n ){ s[i] = c; ++i; } s[i] = \0 ; return (i); }
[ 3-8] main( ) { int count; char c; count = 0; while(( c = getchar( ))!= EOF){ if( c >= 0 && c <= 9 ) continue; count++; } printf( non digital character: %d\n, count); }
[ 3-9] #include <stdio.h> char input[ ] = SSSWILTECH1\1\11W\1WALLMP1 ; main( ) { int i, c; for( i = 2; (c = input[i])!= \0 ; i++){ } switch ( c ) { case a : case 1 : putchar( i ); continue; break; case 1: while( ( c = input[ ++i])!= \1 && c!= \0 ); case 9: case E : case L : default: putchar( S ); continue; putchar( c ); continue; } } putchar( ); putchar( \n ); SWITCH SWAMP
2 for( putchar( 1 ); putchar( 2 ); putchar( 3 )){ putchar( 4 ); continue; putchar( 5 ); } 1243243..
3 while(a) { if(b) contine; C; } while(a) if(!b) C;
4 1 1 2 I/O #include <stdio.h> ( getchar, putchar ) #include <math.h> (sin, cos, sqrt ) A B C D
4.2 4 2 1 ANSI C { [ ] }
main ANSI C int void C Pascal procedure function C [ ] C Pascal
[ ] C return
[ 4-1] x #include <stdio.h> int power(int x, int n); main( ) { } { int i; for (i=0; i <= 10; ++i) printf( %d %d %d\n, i, power(2, i), power(-3, i)); int power(int x, int n) int i, p; p = 1; for(i=1; i<=n; ++i) p = p*x; return (p); }
power int power(int x, int n) { int p; for(p = 1; n > 0; --n) p *= x; return (p); }
[ 4-2] #define MAXLINE 1000 int index(char s[ ], char t[ ]); int getline(char line[ ], int ml); main( ) { char line[maxline]; while(getline(line, MAXLINE) > 0) if(index(line, the ) >= 0) printf( %s, line); }
int index(char s[ ], char t[ ]) { int i, j, k; for(i =0; s[i]!= \0 ; i++){ for(j=i, k=0; t[k]!= \0 && s[j]= = t[k]; j++,k++) ; if(t[k] = = \0 ) return ( i); } return ( -1); }
Now is the time for all good men to come to the aid of their party this is the time men to come to the aid of their party
4 2 2 ANSI C void C
[ 4-3] double atof(char s[ ]) { double val, power; int i, sign; for(i=0; s[i] = = s[i] = = \n s[i] = = \t ; i++) ; sign = 1; if(s[i] = = + s[i] = = - ) sign = (s[i++] = = + )? 1:-1; for(val = 0; s[i] >= 0 && s[i] <= 9 ; i++) val = 10* val + s[i] 0 ; if(s[i] = =. ) i++; for(power=1; s[i] >= 0 && s[i] <= 9 ; i++) { val = 10*val +s[i] 0 ; power *= 10; } return ( sign * val / power); }
4 2 3 prototype ANSI C double atof(char s[ ]); double atof( char * );
[ 4-4] #define MAXLINE 100 double atof(char s[ ]); int getline( char line[ ], int maxline); main( ) { double sum; char line[maxline]; sum = 0; while(getline(line, MAXLINE) > 0) printf( \t%.2f\n, sum += atof(line)); } [ ]
4 2 4 ANSI C float double char short int C Pascal ------
void swap ( int x, int y) { } { } main( ) int temp; temp = x; x = y; y = temp; int a = 2, b = 3; swap(a, b); a b a=2 b=3 swap(a,b) x=2 y=3 swap x=3 y=2
a b void swap ( int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } main( ) { int a =2, b = 3; &a &b a b } swap ( &a, &b); px py
scanf int, char, double C C
4 2 5 C (1) x n = 1 n =0 x * x n-1 n > 0 (2) n! = 1 n =0 n * (n-1)! n > 0
[ 4-5] n! #include <stdio.h> int fact(int n); main( ) { printf( 3!=%d, 5!=%d\n, fact(3), fact(5)); } int fact(int n) { fact(3) 3*2*1 if( n <= 1) return ( 1); else 3*fact(2) 2*1 return ( n * fact(n-1)); 2*fact(1) } 1 1
fact int fact(int n) { int f = 1; While(n){ f *= n; n--; } return ( f); }
[ 4-6] (hanoi tower) void hanoi( int n, char x, char y, char z) { } if( n > 0 ) { } main( ) hanoi(n-1, x, z, y); printf( MOVE %d: %c %c\n, n, x, z); hanoi(n-1, y, x, z); { } int n; printf( Please input the number of hanoi tower: ); scanf( %d, &n); hanoi(n, A, B, C ); A B C
4 3 C Pascal --- ---
4 3 1 [ ] auto static extern auto register
1)
2 C extern extern extern
3) static static private ( C++ )
4) register 2~3 int, char, short, unsigned register register &
4 3 2 0 binary(int x, int y, int n) { int low = 0; int high = n 1; } ANSI C ( ANSI C int x[ ] = { 1, 3, 3, 3}; static char pattern[ ] = the ; static char pattern[ ] = { t, h, e, \0 };
4 4 C C f( ) { } g( ) { } C if(n > 0) { } int i; /* declare a new */ for(i=0; i<n; i++)
[ 4-7] int i = 0; main( ) { int i = 1; printf( i=%d,, i); { int i = 2; printf( i=%d,, i); { i += 1; printf( i=%d,, i); } printf( i=%d,, i); } printf( i=%d\n, i); }?i=1, i=2, i=3, i=3, i=1
4. 5 C #define #undef #include #if #ifdef #ifndef #else #endif #line ( # ) C C C #include #define #if
4 5 1 include #include #inlcude < > #define
#include.h #include s.c f.h r.h #include f.h #include r.h
#include.h #include.h #include <stdio.h> #include <math.h> #include local.h
4 5 2 #define #define 1 #define YES 1 #define EOF -1
#define := = #define then #define begin { #define end ;} Pascal C if( i > 0) then begin a := 1; b:=2; end
2) #define #define max(a, B) ((A) > (B)?(A) : (B)) x = max(p+q, r+s); x = ((p+q) > (r+s)? (p+q) : (r+s)); ( a. max(a,b); b. (A)>(B)?(A) : (B) )
#define square( x) x * x square(z+1) z+1*z+1 #define square(x) ((x) * (x))
#undef YES
4 5 3 C #if #ifdef #ifndef
#if #else #endif 1 -- 2 -- #ifdef YES #else #endif --YES --YES
#ifndef THIS #define THIS #endif
4 5 4 #line [ ] #line 100 another_name another_name, line 100
C
5 1 [ ] [ ] [ ] C C
1) int a[50]; a[0] ~ a[49] float m[ c - a ], p[2*sizeof(double)]; a[0] a[1]
2) char static char mes[ ] = C Language ; char line[100]; 0 ~ N-1 N \0 1
3 float y[4][3]; C C y[i][j] y[i, 0 1 2 0 1 2
float y[4][3] = { y[0] = { 1, 3, 5 }; } { 1, 3, 5 }, y[1] = { 2, 4, 6 }; { 2, 4, 6 }, y[2] = { 3, 5, 7 }; { 3, 5, 7 }, y[3] = { 0, 0, 0 }; float y[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 }; C float y[4][3] = { } {1}, {2}, {3}, {4} y[0] = { 1, 0, 0 }; y[1] = { 2, 0, 0 }; y[2] = { 3, 0, 0 }; y[3] = { 4, 0, 0 };
main( ) { } float a[4][3], b[3][4], c[4][4]; fun(a, b, c); void fun(float x[ ][3], float y[ ][4], float z[ ][4]; { }
[ 5-1] #define MAXLINE 1000 int getline(char line[ ], int maxline); void copy(char s1[ ], char s2[ ]); main( ) /* find longest line */ { } int len; /* current line length */ int max; /* maximum length seen so far */ char line[maxline]; /* current input line */ char save[maxline]; /* longest line saved */ max = 0; while( ( len = getline(line, MAXLINE)) > 0 ) if( len > max ) { } if( max > 0) max = len; copy(line, save); printf( %s, save);
void copy( char s1[ ], char s2[ ]) /* copy s1 t o s2 */ { int i = 0; while( ( s2[i] = s1[i] )!= \0 ) i++; }
[ 5-2] int day_of_year(int year, int month, int day) { int i, leap; static int day_tab[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; leap = year % 4 = = 0 && year % 100!= 0 year % 400 = = 0; for(i=1; i < month; i++) day += day_tab[leap][i]; return ( day ); }
5.2 C C 0 #define NULL 0 C
pi = &i; pi i; y = *pi; pi 0x100 i pi=&i pi 0x100 y=*pi y i
5 2 1 1. int *px; char *pc; char *acp[ ]; char (*pac)[ ]; int f( ); int *fpi( ); int (*pfi)( ); int (*p[4][3])( ); /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ int *(*pfpi)( ); /* */
int x = 100; int *px; px = &x; px x *px px 100 x *px
1 int x = 1; int *px; *px = x; 2 char *string; scanf( %s, string); strcpy(string, Hello ); C
malloc alloc &
2. 1) p p+n p-n p+1 p++ 2) P1 P2 py = px px py strcpy(py, px) 3) = =, >, < 4) p q q p p p q
[ ] mid = (low + high ) /2 low mid = low + (high low)/2 mid high
p++ p+1 y = *px + 1 y= *(px + 1) y = (*px)++ y = *px ++
1. p++ p p+1 p 2. *px+1 px 1 *(px+1) px 1 y= *px+1 = 101 y= *(px+1) = 200 3. (*px)++ px 1 *px++ px px 1 px 100 px 101 px px 100 px+1 200 100 Y=(*px)++ =100 Y=*px++ = 100
5 2 2 C 0
int a[10], x; int *pa; pa = &a[0]; x = *pa; x = a[0]; x = *a; x = *(pa + 1); x = a[1]; x = *(a+1); x = *(pa+i); x = a[i]; x = *(a+i); pa = &a[0] pa = a; a[i] = *(a+i) pa =a; a = pa ; a++; pa = &a;
printf( %d\n, data[i]); printf( %d\n, *data); data[i] *data i data i x + =>
C printf( a constant character string\n ); [ ] char *char_ptr, word[20]; char_ptr = point to me ; word = you can t do this ; word
int values[100], *inptr = values, i; &values[0] values intptr values[0] *values *intptr &values[i] values+i intptr+i valuse[i] *(values+i) *(intptr+i), intptr[i] values Values values i+1 Values i+1
[ 5-3] strlen 1) int strlen( char s[ ]) { } 2) int strlen(char *s) { } int n; for(n=0; *s!= \0 ; s++) n++; return (n); main( ) int n = 0; char st[100]; while(s[n]!= \0 ) scanf( %s, st); ++n; printf( %d\n, strlen(st)); return (n); } char s[ ] { char *s main( ) { char *st; } int l; st = C Language ; l = strlen(st); printf( length=%d\n, l);
[ 5-4] strcpy 1) void strcpy(char s[ ], char t[ ]) { } int i = 0; while((s[i] = t[i])!= \0 ) i++; void strcpy(char *s, char *t) { } while((*s = *t)!= \0 ) { } s++; t++; void strcpy(char *s, char *t) { while(*s++ = *t++) ; }
5 2 3
1) static char days[7][10] = { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] days[0] S u n d a y \0 days[1] M o n d a y \0 days[2] T u e s d a y \0 days[3] W e d n e s d a y \0 days[4] T h u r s d a y \0 days[5] F r i d a y \0 days[6] S a t u r d a y \0
2 static char *days[7] = { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; Sunday\0 days[0] Monday\0 days[1] Tuesday\0 days[2] days[3] days[4] Wednesday\0 Thursday\0 days[5] days[6] Friday\0 Saturday\0 char *days[7]
#include <stdio.h> a= 0x194, a[0]= 0x194, &a[0][0]= 0x194 char a[4][5] = { abcd, efgh, ijkl, mnop }; a+1= 0x198, a[0]+1= 0x195, &a[0][1]= 0x195 main( ) **(a+1)= e, *(a[0]+1) = b { printf( a=%x, a[0]=%x, &a[0][0]=%x\n, a, a[0], &a[0][0]); printf( a+1=%x, a[0]+1=%x, &a[0][1]=%x\n, a+1, a[0]+1, &a[0][1]); printf( **(a+1)=%c, *(a[0]+1)=%c\n, **(a+1), *(a[0]+1)); } a[0] a[0]+1 a a b c d \0 a+1 e f g h \0 i j k l \0 m n o p \0 a, a[0], &a[0][0] a+1 a[0]+1
[ 5-5] char *month_name(int n) { static char *name[ ] = { illegal month, January, February, March, April, May, June, July, August, September, October, November, December }; return ((n<1 n>12)? name[0] : name[n]); }
C main int main( int argc, char *argv[ ]) int main( int argc, char **argv) -argc -argv argv[0] argv[1] argv[2] argv[argc-1] argc-1
[ 5-6] UNIX each each hello world hello world main( int argc, char *argv[ ]) { } int i; for(i=1; i<argc; i++) printf( %s%c, argv[i], (i< argc-1)? : \n ); main(int argc, char *argv[ ]) { } while(--argc > 0) argv printf((argc > 1)? %s : %s\n, *++argv); each hello world
* int (*fp)( ); int *fp( ) = C
[ 5-7] int leapyear( int year); main( ) { int (*fnptr)( ); fnptr = leapyear; result = (*fnptr)(2000); } /* leapyear(2000) */ int leapyear( int year) { if(((year % 4 = = 0) && (year % 100!= 0)) (year % 400) = = 0) return ( 1); else return ( 0); }
dispatch, index 0~9 index 0 fn0 1 fn1 int fn0( ), fn1( ), fn2( ), fn9( ); static int (*dispatch[ ])( ) = { fn0, fn1, fn2, fn9 }; (*dispatch[index])( );
[ 5-8] static char *p[2][3] = { }; abc, defg, hi, jklmnop, qrstuvwxyz, ABCD ***p p[0][0][0] a **p[1] p[1][0][0] j **(p[1]+2) p[1][2][0] A p[1][1][7] x (*(*(p+1)+1))[7] *(p[1][2]+2) p[1][2][2] C
[ 5-9] 1100 9 9 short *pi, *pj, t; long *pl; double *pd; *pd += (double) *pi; pi = &t; *pi = (short) *pl; pj = pi; *pj /= 3; ++pj; ++*pj; pi 1300 t 1350 pj 1380 pl 1400 1410 1430 pd 1440 1100 14 20 1100 1410 7 0.0 1430 1350 2 21 1352 1410 1430 7 9.0
5 3 typedef typedef int LENGTH; typedef char *STRING; LENGTH len, maxlen; STRING lineptr[lines], alloc( ); int len, maxlen; char *lineptr[lines], *alloc( );
[ 5-10] int (*(*(*test( ))[ ])( )) [ ]; *test( ) typedef int AIN[ ]; *( )[ ] *( ) ( ) int ( ) [ ] typedef AIN *PAIN; typedef PAIN FPAIN( ); typedef FPAIN *PFPAIN; typedef PFPAIN APFPAIN[ ]; typedef APFPAIN *PAPFPAIN; test typedef PAPFPAIN FPAPFPAIN( ); FPAPFPAIN test; [ ]
[ 5-11] int (*(*test) ( ))[ ]; *test *( ) ( ) int ( )[ ]; test? typedef int AIN[ ]; typedef AIN *PAIN; typedef PAIN FPAIN( ); typedef FPAIN *PFPAIN; PFPAIN test;
[ 5-12] int (*test( ))[ ]; *test( ) int ( ) [ ]; test typedef int AIN[ ]; typedef AIN *PAIN; typedef PAIN FPAIN( ); FPAIN test;
typedef
C [ ] 1 2 identifier is 3 [x] an array of x ; function returning ; 4 * a pointer to ; 5 go to 3 6 type ;
int (*i) ( ); int ( * i ) ( ); i is a pointer to a function returning an int i is a pointer to a function returning an int
5 4 enum enum { } enum color { red, green, yellow, white, black }; /* */ enum color chair; enum color suite[10];
chair = red; suite[5] = yellow; if( chair = = green ) [ ] C 0 enum color { red, green, yellow = 5, white, black }; red=0, green=1, yellow=5, white=6, black=7 enum Boolean { FALSE, TRUE };
---
6 1 struct 1. 1 2 3
C Pascal record struct date { int day; int month; int year; int yearday; char mon_name[4]; } type end; date = record day: integer; month: integer; year: integer; yearday: integer; monname: array[0..3] of char; template struct date d1, d2; [ ] struct date struct
1 struct { } d; 2 struct date { } d; 3 typdef typedef struct { } DATE; DATE d, *pd, ad[10]; typedef
ANSI C static struct date d = { 27, 12, 1984, 361, Dec };
struct person { }; char name[namesz]; char address[adrsz]; char department[dpsz]; double salary; struct date birthdate; struct person table[100];
[ ] struct keyword { char *name; int count; struct keyword *next; } *base; base 5 int 2 float 4 char struct tnode { } *root; char *word; int count; struct tnode *left; struct tnode *right; root
2 struct person emp; emp emp.birthdate.year = 1970; emp.birthdate.month = 8; struct date *pd, d; pd struct date pd = &d pd = (struct date *) malloc(sizeof(struct date)); pd pd pd->year = 1958; strcpy(pd->mon_name, OCT ); pd->year (*pd).year C ->
struct { } *p; int x; int *y; ++p->x; x ++(p->x); (++p)->x; (p++)->x; p++->x; *p->y; *p->y++; (*p->y)++; *p++->y; p x x p y y y y p C ( ), [ ],, -> *, ++, --,
[ 6-1] #include <stdio.h> main( ) struct { int x; char *y; } *p, A[ ] = { }; 1, for, 2 3 3 2 d d o p 3, for 2, while 3, p_while 4, switch 2, while, 3, do_while, 4, switch { } int i; p = A; printf( %d, ++p->x); printf( %d, ++(p->x)); printf( %d, (p++)->x); printf( %d, p++->x); printf( %c, *p->y); printf( %c, *p->y++); printf( %c, (*p->y)++); printf( %c, *p++->y); for(i=0; i<4; i++) printf( %d, %s\n, A[i].x, A[i].y);
6 2 [ 6-2] C
6-2 C struct Key { char *keyword; int keycount; } Keytab[ ] = { auto, 0, break, 0, case, 0, while, 0 }; #define MAXWORD 20 #define NKEYS (sizeof(keytab) / sizeof(struct Key)) #define LETTER a #define DIGIT 0 struct Key *binary(char *word, struct Key tab[ ], int n); char getword(char *w, int lim); char type( int c);
6-2 main( ) /* count C keyword */ { int t; char word[maxword]; struct Key *p; } while((t = getword(word, MAXWORD))!= EOF) if( t = = LETTER) if(( p = binary(word, Keytab, NKEYS))!= NULL) p->keycount++; for(p=keytab, p < Keytab+NKEYS; p++) if(p->keycount > 0) printf( %4d%s\n, p->keycount, p->keyword);
6-2 struct Key *binary(char *word, struct Key tab[ ], int n) { int cond; struct Key *low = &tab[0]; struct Key *high = &tab[n-1]; struct Key *mid; } while(low <= high){ mid = low + (high low) / 2; if((cond = strcmp(word, mid->keyword)) < 0) high = mid 1; else if ( cond > 0) low = mid + 1; else return (mid); } return (NULL);
6-2 char getword(char *w, int lim) { int c, t; } if(type(c = *w++ = getch( ))!= LETTER){ *w = \0 ; return ( c); } while(--lim > 0) { t = type(c = *w++ = getch( )); if( t!= LETTER && t!= DIGIT){ ungetch(c); break; } } *(w-1) = \0 ; return ( LETTER);
6-2 char type(int c) /* return type of ASCII character */ { if( c >= a && c <= z c >= A && c <= Z ) return ( LETTER ); else if ( c >= 0 && c <= 9 ) return ( DIGIT ); else return (c); }
6 3 struct keyword { char *name; int count; struct keyword *next; }; struct tnode { char *word; int count; struct tnode *left; struct tnode *right; };
struct link { int n; struct link *next; }; 1) struct link *first *p; p = first = (struct link *)malloc(sizeof(struct link)); p->next = NULL; for(i=0; i<= 10; i++){ p->n = i; p->next = (struct link *)malloc(sizeof(struct link)); p = p->next; } 1 NULL 2 NULL 3 NULL
2 p1->next = p->next; p->next = p1; p 1 NULL 2 NULL 3 NULL 3 q = p->next; p1 4 p->next = p->next->next; free(q); p q 1 NULL 2 NULL 3 NULL
[ 6-3]
[ 6-3] #include <stdio.h> #define MAXWORD 20 struct tnode { char *word; int count; struct tnode *left; struct tnode *right; }; struct tnode *tree(struct tnode *p, char *w); struct tnode *talloc( ); char *strsave(char *s); void treeprint(struct tnode *p);
[ 6-3] main( ) /* word frequency count */ { struct tnode *root; char word[maxword]; int t; root = NULL; while((t = getword(word, MAXWORD))!= EOF) if( t = = LETTER) root = tree(root, word); treeprint(root) }
[ 6-3] struct tnode *tree(struct tnode *p, char *w) / * install w at or below p */ { int cond; } if( p = = NULL ) { /* a new word has arrived */ p = talloc( ); /* make a new node */ p->word = strsave(w); p->count = 1; p->left = p->right = NULL; } else if((cond = strcmp(w, p->word)) = = 0) p->count ++; /* repeated word */ else if ( cond < 0 ) /* lower into left subtree */ p->left = tree(p->left, w); else /* greater into right subtree */ p->right = tree(p->right, w); return ( p );
[ 6-3] struct tnode *talloc( ) { return (( strcut tnode *)malloc(sizeof(struct tnode )); } char *strsave(char *s) { char *p; if((p = malloc(strlen(s)+1))!= NULL) strcpy(p, s); return ( p ); } void treeprint( struct tnode *p) / * print tree p recursirely */ { if(p!= NULL) { treeprint(p->left); printf( %4d%s\n, p->count, p->word); treeprint(p->right); } }
[ 6-3] C language book study pascal statement buy C book language buy study pascal statement
6 4 int 1 #define O-READ #define O-WRITE #define RW #define HIDE if( status & HIDE ) status = O-READ; 0x01 0x02 0x04 0x08 O_READ O_WRITE RW HIDE
2) struct FileStatus { unsigned O_READ:1; unsigned O_WRITE:1; unsigned RW:1; unsigned HIDE:1; }; struct FileStatus status; if(status.hide) status.o_read = 1;
struct instruction { unsigned opcode:8; unsigned opert1:4; unsigned opert2:4; }; struct instruction f; opcode opert1 opert2 15 8 7 4 3 0
& 0
6 5 union union { }
[ ] union v_tag { int ival; float fval; char *pval; } uval; if(utype = = INT) printf( %d\n, uval.ival); else if (utype = = FLOAT) printf( %f\n, uval.fval); else if(utype = = STRING) printf( %s\n, uval.pval); else printf( bad type\n ); [ ]
[ ] struct { char *name; int flags; int utype; union { } uval; int ival; float fval; char *pval; } symtab[nsym]; symtab[i].uval.ival union Pascal
: ------------------------------------------------------------------------------------------ strcu v_tag { int ival; float fval; char *pval; }; union v_tag { int ival; float fval; char *pval; } uval; struct v_tag union v_tag int float char * ival fval pval int ival float fval char* pval struct v_tag union v_tag
: ------------------------------------------------------------------------------------------ union struct 0
/ UNIX UNIX
7 1 / I/O C C I/O I/O UNIX
1. / / C / stdio.h #include <stdio.h> I/O int getchar(void) int putchar(int c) char *gets( char *s) int puts( char *s) c s s UNIX
[ ] #include <stdio.h> main( ) { } int c; while((c = getchar( ))!= EOF) putchar ( isupper(c)? tolower(c):c); 1 c int; 2 while((c = getchar( ))!= EOF) c=getchar( ) C getchar putchar isupper tolower UNIX
[ ] getchar( ) I/O / I/O [ 7-1] #include <stdio.h> main( ) { } int c; while((c = getchar ( ))!= EOF) putchar( c); isn't this <Enter> isn t this CTR+d UNIX
prog < infile prog > outfile otherprog prog otherprog prog prog otherprog cat file1 file2 lower > outfile otherprog prog UNIX
2 / 1) int scanf( 1 2 ) UNIX
scanf %[*][size][l][h]type type (type) d u o x e, f, g c s [ ] [^ ] % char letter[100]; scanf( %[a-z], letter); scanf( %[^,. ;], letter); /* */ /* */ UNIX
[ ] int i; float x; cahr name[50]; scanf( %2d %f %*d %2s, &i, &x, name); 56789 0123 45a72 * size l h type i = 56, x = 789.0, 0123, name = 45 a scanf -1 UNIX
2 int printf( 1 2 ) %[flags] [ width] [.prec] [l] type UNIX
(type) d u o x a~f X A~F f e e E E f f e f E c s \0 % UNIX
flags - + + - # 0 0x( 0X) %g %G width * prec e f g s * l type UNIX
[ ] int width = 10; printf( %*d, width, result); printf( :%*.*d:\n, 10, 6, 100); : 000100: /* 10 result */ /* 10 6 */ [ ] hello, world 12 :%10s: :%-10s: :hello, world: :hello, world: :%20s: : hello, world: :%-20s: :hello, world : :%20.10s: : hello,wor: :%-20.10s: :hello, wor : :%.10s: :hello, wor: UNIX
3 sscanf(char * s, char *format [, pointer ]) s sprintf(char * s, char *format [, arg ]) s scanf printf sprintf main( ) { char buf[100], *cmd= pr -w, *file= /tmp/data ; int width = 80; sprintf(buf, %s %d %s, cmd, width, file); printf( %s\n, buf); } sscanf char *buf = 123.456 + 595.36 ; float val1, val2; char oper; sscanf(buf, %f %c %f, &val1, &oper, &val2); UNIX
3 / #include <stdio.h> 1) fopen( ); 2) getc, putc, fgetc, fputc, fgets, fputs, fscanf, fprintf 3) fclose( ); UNIX
fopen FILE *fopen(const char *filename, const char *mode); fopen FILE fopen FILE *fp; FILE struct fopen fopen( ) FILE FILE UNIX
fopen fp = fopen( ) r w a r+ w+ a+ UNIX
w a fopen w a r NULL r+ w+ r+ a+ rb w+b [ ] UNIX
getc( fp ), putc(c, fp) fgetc(fp), fputc(c, fp) fscanf(fp, format, ), fprintf(fp, format, ) / FILE stdin, stdout, stderr getc, putc fgetc,fputc fgetc fputc getc putc stdio.h #define getchar ( ) getc(stdin) #define putchar(x) putc(x, stdout) UNIX
[ 7-2] cat x.c y.c z.c #include <stdio.h> void filecopy(file *fp); main( int argc, char *argv[ ]) { FILE *fp; if( argc = = 1) filecopy(stdin); else while(--argc > 0) if(( fp = fopen(*++argv, r )) = = NULL) { printf( cat: can t open %s\n, *argv); break; } } else { filecopy(fp); fclose(fp); } void filecopy( FILE *fp) { int c; while((c = getc(fp))!= EOF) putc(c, stdout); } UNIX
4 exit(1), exit(0) exit fclose _exit UNIX
[ ] cat main( int argc, char *argv[ ]) { FILE *fp; if( argc = = 1) filecopy(stdin); else while(--argc > 0) if(( fp = fopen(*++argv, r )) = = NULL) { printf( cat: fprintf(stderr, can t cat: open can t %s\n, open %s\n, *argv); *argv); break; exit(1); } else { filecopy(fp); fclose(fp); } } exit(0); UNIX
5 / char *fgets(char *s, int n, FILE *fp) fp n-1 s s NULL int fputs( char *s, FILE *fp) s \n fp EOF fgets s gets fputs fgets fgets gets fgets(buf, 81, stdin); UNIX
6 / size_t fread(void *ptr, size_t size, size_t nobj, FILE *fp) fp size nobj ptr size_t fwrite(const void *ptr, size_t size, size_t nobj, FLE *fp) ptr fp size nobj fread(buf, sizeof(struct Date), n, fp); fwrite(buf, sizeof(struct Date), n, fp); UNIX
7 2 #include <ctype.h> isalpha(c) c 0 0 isupper(c) islower(c) #define isupper(c ) (c >= A && c<= Z )?1:0 isdigit(c) isspace(c) toupper(c) tolower(c) c 0 0 c c UNIX
ungetc(c, fp) #include <stdio.h> c fp #include <stdlib.h> system(s) s system( date ); #include <stdlib.h> void *malloc(size_t size); void *calloc(size_t nobj, size_t n); void free(void *p); int *ip; ip = (int *) malloc(100*sizeof(int)); UNIX
#include <string.h> char *strcpy(char *s1, char *s2); char *strcat(char *s1, char *s2); int strcmp(char *s, char *t); int strlen(char *s); char st[20], *p= c language ; char *strchr(char *s, char c); char *strstr(char strcpy(st, *s1, char p); *s2); int feof(file *fp) 0 int fseek(file *fp, long offset, int origin) origin offset origin SEEK_SET SEEK_CUR SEEK_END fseek(fp, 0, SEEK_SET); long ftell(file *fp) fseek(fp, -5, SEEK_END); 5 UNIX
#include <stdlib.h> double atof(const char *s); int atoi(const char *s); long atol(const char *s); UNIX
7 3 UNIX / 1 UNIX / UNIX file descriptor [ ] UNIX
2 1) 0 1 2 open creat 2) open fopen open 3) fopen creat fd = creat( char *name, int perms); name perms UNIX 9 bit 0755 111 101 101 rwx rwx rwx -1 UNIX
4) open #include <fcntl.h> (DOS io.h ) int fd; int open(char *name, int flags); fd = open(name, flags); name flags O_RDONLY O_WRONLY O_RDWR O_NDELAY O_APPEND O_TRUNC ( 0) 1 2 O_CREAT -1 UNIX
5) close(fd); 6) unlink(filename); UNIX
3. nr = read( ) nw = write( ) 1) read nr 0-1 2) write nw 1 512 1024) 512 1024 UNIX
[ ] #include <fcntl.h> #define BUFSIZE 512 main( ) { char buf[bufsize]; int n; } while((n = read(0, buf, BUFSZIE)) > 0) write(1, buf, n); UNIX
[ ] #include <fcntl.h> #define CMASK 0377 /* for making char s > 0 */ int getchar( ) { char c; /* char read */ } return(( read(0, &c, 1) > 0)? c & CMASK : EOF); UNIX
[ ] #include <fcntl.h> #define CMASK 0377 #define BUFSIZE 512 int getchar( ) { static char buf[bufsize]; static char *bufp = buf; static int n = 0; if( n = = 0){ n = read(0, buf, BUFSIZE); bufp = buf; } return ( (--n >= 0)? *bufp++ & CMASK : EOF); } UNIX
4. I/O [ ] pos n long lseek(int fd, long offset, int origin); int get(int fd, int pos, char *buf, int n) origin offset origin 0 1 2 { SEEK_SET, lseek(fd, SEEK_CUR, pos, 0); SEEK_END offset long return(read(fd, buf, n)); lseek(fd, 0l, 2); } lseek(fd, -5l, 2); 5 lseek -1 UNIX
C 1983 ACM UNIX C Ken Thompson 1040 C C
char s[ ] = { \t, 0, \n, } ;, \n, \n, /, *, \n, (213 lines deleted) 0 }; /* The string is a * representations of the body * of this program from 0 * to the end */ main( ) { } int i; C printf( char \ts[ ] = { \n }; for(i=0; s[i]; i++) printf( \t%d\n,\n, s[i]; printf( %s, s); s C Trojan horse UNIX C Ken Thompson