3 3 1. 1 (K&R.9) ~, &,, >>, <<. (K&R 5.7) 3. (K&R 5.9). (K&R 5.) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, stderr - fopen(), fclose(), getc(), putc(), fprintf(), fscanf(),... 3.1 1 (K&R.9) ~, &,, >>, << bit.c 3 int main(){ unsigned int x, r1, r, r3, r, r5; 5 6 scanf("%d", &x); 7 r1 = x & 0177; 8 r = x 0xb; 9 r3 = x & ~077; r = x << 1; 11 r5 = x >> ; 1 13 printf("x: %u\nx & 0177: %u\nx 0xb: %u\n" 1 "x & ~077: %u\nx << 1: %u\nx >> : %u\n", 15 x, r1, r, r3, r, r5); 16 return 0; 17 } % echo 19 bit x: 19 x & 0177: 1 x 0xb: 139 x & ~077: 18 x << 1: 58 x >> : 3 bit unsigned 0 8 0x 16 (K&R p.5) 15
3. (K&R 5.7) multiarray1.c 3 int main(){ 5 int test[][3] 6 = {{5,, 15}, {0, 00, 300}}; 7 8 int (*pt)[3] = test; 9 int i, j; 11 for(i=0; i<; i++) 1 for(j=0; j<3; j++) 13 printf("test[%d][%d] = %d at %p\n", 1 i, j, test[i][j], &test[i][j]); 15 16 putchar( \n ); 17 for(i=0; i<; i++) 18 for(j=0; j<3; j++) 19 printf("*(*(test+%d)+%d) = %d at %p\n", 0 i, j, *(*(pt+i)+j), *(pt+i)+j); 1 return 0; 3 } test[0][0] = 5 at 0xbffffba0 test[0][1] = at 0xbffffba test[0][] = 15 at 0xbffffba8 test[1][0] = 0 at 0xbffffbac test[1][1] = 00 at 0xbffffbb0 test[1][] = 300 at 0xbffffbb int test[][3] *(*(test+0)+0) = 5 at 0xbffffba0 *(*(test+0)+1) = at 0xbffffba *(*(test+0)+) = 15 at 0xbffffba8 *(*(test+1)+0) = 0 at 0xbffffbac *(*(test+1)+1) = 00 at 0xbffffbb0 *(*(test+1)+) = 300 at 0xbffffbb 16
multiarray.c 3 int element(int array[][3], int, int); void initialize(int (*array)[3]); 5 6 int main(){ 7 8 int test[][3] 9 = { {5,, 15}, {0, 00, 300}}; int i, j; 11 1 for(i = 0; i < ; i++){ 13 for(j = 0; j < 3; j++) 1 printf("%d\t", element(test, i, j)); 15 printf("\n"); 16 } 17 18 initialize(test); 19 for(i = 0; i < ; i++){ 0 for(j = 0; j < 3; j++) 1 printf("%d\t", element(test, i, j)); printf("\n"); 3 } return 0; 5 } 6 7 int element(int (*multi_array)[3], int x, int y) 8 { 9 return multi_array[x][y]; 30 } 31 3 void initialize(int multi_array[][3]) 33 { 3 int i,j; 35 for(i = 0; i < ; i++) 36 for(j = 0; j < 3; j++) 37 multi_array[i][j] = 0; 38 } f(int array[][13]) {... } f(int array[][13]) {... } f(int (*array)[13]) {... } ( : K&R p.11, p.137) 5 15 0 00 300 0 0 0 0 0 0 17
3.3 (K&R 5.9) pointer-marray1.c (K&R 5.9) 3 int main(){ 5 char *name[] 6 = { "Illegal month", "Jan", "Feb", "Mar" }; 7 char aname[][15] 8 = { "Illegal month", "Jan", "Feb", "Mar" }; 9 int i; 11 for(i = 0; i < ; i++) 1 printf("%s, %s (%p)\n", 13 name[i], *(name + i), name + i); 1 15 for(i = 0; i < ; i++) 16 printf("%s, %s (%p)\n", 17 aname[i], *(aname + i), aname + i); 18 19 return 0; 0 } Illegal month, Illegal month (0xbffffbb0) Jan, Jan (0xbffffbb) Feb, Feb (0xbffffbb8) Mar, Mar (0xbffffbbc) Illegal month, Illegal month (0xbffffb70) Jan, Jan (0xbffffb7f) Feb, Feb (0xbffffb8e) Mar, Mar (0xbffffb9d) char *name[] ={ Illegal month, Jan, Feb, Mar name: Illegal month 0 Jan 0 Feb 0 Mar 0 aname[][15] ={ Illegal month, Jan, Feb, Mar aname: Illegal month 0 Jan 0 Feb 0 Mar 013567890135678901356789013567890135678 pointer-marray.c 3 int main(){ 5 char *name[] 6 = { "Illegal month", "Jan", "Feb", "Mar" }; 7 int i; char **ip; 8 9 ip = name; for(i = 0; i < ; i++) 11 printf("%s\n", *ip++); 1 13 return 0; 1 } ip: name: Illegal month 0 Jan 0 Feb 0 Mar 0 Illegal month Jan Feb Mar 18
3. (K&R 5.) argument1.c 3 int main(int argc, char *argv[]) { 5 int i; 6 7 for(i = 0; i < argc; i++) 8 printf("argv[%d] = %s\n", i, argv[i]); 9 printf("\n"); 11 for(i = 1; i < argc; i++) 1 printf("%s%s", argv[i], (i < argc -1)? " " : ""); 13 printf("\n"); 1 15 for(i = 1; argv[i]!= NULL; i++) 16 printf("%s%s", argv[i], (i < argc -1)? " " : ""); 17 printf("\n"); 18 19 while(--argc > 0) 0 printf((argc > 1)? "%s " : "%s", *++argv); 1 printf("\n"); return 0; 3 }? K&R p.6 (null pointer) K&R p.15 (0) NULL % argument1 abc ABC argv[0] = argument1 argv[1] = abc argv[] = ABC abc ABC abc ABC abc ABC % echo hello, world argv: 0 (NULL) echo 0 hello, 0 world 0 argument.c #include <stdlib.h> 3 #include <math.h> /* -lm may be required. */ 5 int main(int argc, char *argv[]){ 6 int x, y; 7 8 if (argc!= 3) 9 exit(1); x = atoi(argv[1]); y = atoi(argv[]); 11 printf("%d to the %dth power = %.0f\n", 1 x, y, pow((double) x, (double) y)); 13 return 0; 1 } (K&R p.56) ( ) % argument 8 to the 8th power = 56 19
3.5 (K&R 7.5) (K&R 7.6) cat1.c (K&R 7.5 p.197. ) 1 /* K&R Chapter 7.5 */ #include <stdio.h> 3 /* cat: version 1 */ 5 6 int main(int argc, char *argv[]) 7 { 8 FILE *fp; /* file pointer */ 9 void filecopy(file *, FILE *); 11 if (argc == 1) 1 filecopy(stdin, stdout); 13 else 1 while (--argc > 0) 15 if ((fp = fopen(*++argv, "r")) == NULL) { 16 printf("cat: can t open %s\n", *argv); 17 return 1; 18 } else { 19 filecopy(fp, stdout); 0 fclose(fp); 1 } return 0; 3 } 5 /* filecopy */ 6 void filecopy(file *ifp, FILE *ofp) 7 { 8 int c; 9 while ((c = getc(ifp))!= EOF) 30 putc(c, ofp); 31 } stdin stdout getc(stdin) getchar() putc(c, stdout) putchar(c) 0
cat.c (K&R 7.6 p.199) 1 /* K&R Chapter 7.6 */ #include <stdio.h> 3 #include <stdlib.h> 5 /* cat: version */ 6 7 int main(int argc, char *argv[]) 8 { 9 FILE *fp; /* file pointer */ void filecopy(file *, FILE *); 11 char *prog = argv[0]; 1 13 if (argc == 1) 1 filecopy(stdin, stdout); 15 else 16 while (--argc > 0) 17 if ((fp = fopen(*++argv, "r")) == NULL) { 18 fprintf(stderr, "%s: can t open %s\n", 19 prog, *argv); 0 exit(1); 1 } else { filecopy(fp, stdout); 3 fclose(fp); } 5 if (ferror(stdout)) { 6 fprintf(stderr, 7 "%s: error writing stdout\n", prog); 8 exit(); 9 } 30 exit(0); 31 } 3 33 /* filecopy */ 3 void filecopy(file *ifp, FILE *ofp) 35 { 36 int c; 37 while ((c = getc(ifp))!= EOF) 38 putc(c, ofp); 39 } stderr ferror(fp) fp - ( r ) ( w ) ( ) - 1 fgets() (K&R 7.7, p.00) ( ) - (K&R B3, p.313) strtok() ( strtok r() ) sscanf(), sprintf() K&R.1,. 1
1. (K&R.9) ~, &,, >>, <<. (K&R.11.3) (#ifdef, #if, #endif) 3. (K&R.) (K&R.6). separate compilation) (K&R.5) 5. 6. 7. malloc(), strdup() struct.1 ~, &,, >>, << getbits.c (K&R.9, p.61) #include <stdlib.h> 3 void error_message(char *); unsigned getbits(unsigned, int, int); 5 6 int main(int argc, char *argv[]) 7 { 8 unsigned int x; 9 int position, n; 11 if (argc!= ) { 1 fprintf(stderr, "usage: %s x p n\n", argv[0]); 13 exit(1); 1 } 15 16 x = atoi(*++argv); 17 position = atoi(*++argv); 18 n = atoi(*++argv); 19 printf("result: %u\n", getbits(x, position, n)); 0 return 0; 1 } 3 /* getbits: get n bits from position p */ unsigned getbits(unsigned x, int p, int n) 5 { 6 return (x >> (p+1-n)) & ~(~0 << n); 7 } x p n x p () n 0
% getbits1 93 0 1 result: 1 % getbits1 93 1 1 result: 0 % getbits1 93 1 result: 1 % getbits1 93 3 result: 7 p x % getbits 93 3 7 6 5 3 1 0 0 1 0 1 >> (x >> (p+1-n)) & ~(~0 << n) n 1 1 0 1 93 ~0 1 1 1 1 1 1 1 1 << 3 0 0 0 1 0 1 1 1 1 1 1 1 1 0 0 0 ~ 0 0 0 0 & 0 1 1 1 0 0 0 0 0 1 1 1. (K&R.11.3, p.111) - #ifdef, #if, #endif, preprocessing1.c #define DEBUG 3 int main() 5 { 6 #ifdef DEBUG 7 printf("debug mode \n"); 8 #else 9 printf("normal mode \n"); #endif 11 return 0; 1 } preprocessing.c /* #define DEBUG */ 3 int main() 5 { 6 #ifdef DEBUG 7 printf("debug mode \n"); 8 #else 9 printf("normal mode \n"); #endif 11 return 0; 1 } cc -DDEBUG DEBUG cc -E ( ) 3
.3 (K&R.) (K&R.6) main1.c 1 void init(); 3 int main() { 5 init(); 6 return 0; 7 } init1.c 1 void init() {} init.c 1 static void init() {} main.c extern int value; 3 int main() 5 { 6 printf("%d\n", value); 7 return 0; 8 } value1.c 1 int value = 1; value.c 1 static int value = 1; static1.c #define ITERATIONS 0 3 int count(void); 5 int main() 6 { 7 int i; 8 for(i = 0; i < ITERATIONS - 1; i++) 9 count(); printf("%d\n", count()); 11 return 0; 1 } 13 1 int count(void) 15 { 16 static int x = 0; 17 return ++x; 18 } % gcc -c main1.c -Wall % gcc -c init1.c -Wall % gcc main1.o init1.o -Wall -o test1 % gcc -c init.c -Wall init.c:1: warning: init defined but not used % gcc main1.o init.o -Wall -o test Undefined first referenced symbol in file init main1.o ld: fatal: Symbol referencing errors. No output written to test collect: ld returned 1 exit status % gcc -c main.c -Wall % gcc -c value1.c -Wall % gcc main.o value1.o -Wall -o test3 % test3 1 % gcc -c value.c -Wall value.c:1: warning: value defined but not used % gcc main.o value.o -Wall -o test Undefined first referenced symbol in file value main.o ld: fatal: Symbol referencing errors. No output written to test collect: ld returned 1 exit status % gcc static1.c -o static1 -Wall % static1 0 K&R p.1. static static
. separate compilation (K&R.5) filecopy.h 1 void filecopy(file *input_file, FILE *ouput_file); filecopy.c #include "filecopy.h" 3 /* filecopy */ 5 void filecopy(file *ifp, FILE *ofp) 6 { 7 int c; 8 while ((c = getc(ifp))!= EOF) 9 putc(c, ofp); } cat main.c #include <stdlib.h> 3 #include "filecopy.h" 5 int main(int argc, char *argv[]) 6 { 7 FILE *fp; /* file pointer */ 8 char *prog = argv[0]; 9 if (argc == 1) 11 filecopy(stdin, stdout); 1 else 13 while (--argc > 0) 1 if ((fp = fopen(*++argv, "r")) == NULL) { 15 fprintf(stderr, "%s: can t open %s\n", 16 prog, *argv); 17 exit(1); 18 } else { 19 filecopy(fp, stdout); 0 fclose(fp); 1 } if (ferror(stdout)) { 3 fprintf(stderr, "%s: error writing stdout\n", prog); 5 exit(); 6 } 7 exit(0); 8 } - (#define) - extern - - ソースソースコードソースコードコードコンパイル, アセンブル オブジェクトオブジェクトファイルオブジェクトファイルファイル リンク 実行形式 ライブラリ % ls cat_main.c filecopy.c filecopy.h % cc -c filecopy.c % cc -c cat_main.c % ls cat_main.c cat_main.o filecopy.c filecopy.h filecopy.o % cc filecopy.o cat_main.o -o cat1 % ls cat1* cat_main.c cat_main.o filecopy.c filecopy.h filecopy.o 5
.5 cp1.c #include <stdlib.h> 3 #include "filecopy.h" 5 int main(int argc, char *argv[]) 6 { 7 FILE *fp_in, *fp_out; /* file pointers */ 8 void filecopy(file *, FILE *); 9 char *prog = argv[0]; 11 if (argc!= 3) { 1 fprintf(stderr, 13 "usage: %s input_file output_file\n", prog); 1 exit(1); 15 } 16 17 if ((fp_in = fopen(*++argv, "r")) == NULL) { 18 fprintf(stderr, "%s: can t open %s\n", 19 prog, *argv); 0 exit(); 1 } 3 if ((fp_out = fopen(*++argv, "w")) == NULL) { fprintf(stderr, "%s: can t create %s\n", 5 prog, *argv); 6 exit(); 7 } 8 9 filecopy(fp_in, fp_out); 30 fclose(fp_in); 31 3 if (ferror(fp_out)) { 33 fprintf(stderr, 3 "%s: error writing %s\n", prog, *argv); 35 exit(3); 36 } 37 fclose(fp_out); 38 exit(0); 39 } % ls cp1* textsample % cat textsample Most wild animals remain fertile until they die, or until close to that time. % cp1 textsample tmp % ls cp1* textsample tmp % cat tmp Most wild animals remain fertile until they die, or until close to that time. 6
.6 get integers.c 3 int main(){ int v, n; 5 char c; 6 7 while((n = scanf("%d", &v))!= EOF) 8 if (n == 1) 9 printf("%d\n", v); else { 11 scanf("%c", &c); 1 printf("%c\n", c); 13 } 1 return 0; 15 } scanf() process line.h 1 void process_line(char *); get each line.c #include <stdlib.h> 3 #include <string.h> #include "process_line.h" 5 #define MAX_LINE 56 6 7 int main(void){ 8 char line[max_line]; 9 while(fgets(line, sizeof(line), stdin)!= NULL) { 11 if (line[strlen(line)-1]!= \n ) { 1 fprintf(stderr, "too long line\n"); 13 exit(1); 1 } 15 process_line(line); 16 } 17 exit(0); 18 } process line1.c #include <stdlib.h> 3 #include <string.h> #include "process_line.h" 5 6 void process_line(char *p) 7 { 8 if (!strcmp(p, "OWARI\n")) 9 exit(0); 11 printf("%s", p); 1 } gets() fgets() scanf( %s, str) fgets() ( \n ) strlen(s) s \0 (K&R p.8) strcmp() 0 (K&R p.0) 7
process line.c #include <stdlib.h> 3 #include <string.h> #include "process_line.h" 5 #define DELIM " \t\n\":;.,!?" 6 7 void process_line(char *str) 8 { 9 char *tp, *p; 11 tp = strtok_r( str, DELIM, &p); 1 while (tp!= NULL) { 13 puts(tp); 1 tp = strtok_r( NULL, DELIM, &p); 15 } 16 } process line3.c #include <string.h> 3 #include "process_line.h" #define DELIM " \t\n\":;.,!?" 5 #define DEBUG 6 7 void process_line(char *str) 8 { 9 char *tp, *p; 11 #ifdef DEBUG 1 fprintf(stderr, "str: %p : %s\n", str, str); 13 #endif 1 15 tp = strtok_r(str, DELIM, &p); 16 17 #ifdef DEBUG 18 fflush(stdout); 19 fprintf(stderr, "tp : %p : %s\n", tp, tp); 0 fprintf(stderr, "p : %p : %s\n", p, p); 1 #endif 3 while (tp!= NULL) { puts(tp); 5 tp = strtok_r(null, DELIM, &p); 6 7 #ifdef DEBUG 8 fflush(stdout); 9 fprintf(stderr, "tp : %p : %s\n", tp, tp); 30 fprintf(stderr, "p : %p : %s\n", p, p); 31 #endif 3 } 33 } strtok r(str, delim, &p) s delim (, token) p s NULL p str tp p R u m o r h a s i t n 0 str tp p R u m o r 0 h a s i t n 0 str tp p R u m o r 0 h a s 0 i t n 0 str tp p R u m o r 0 h a s 0 i t 0 0 % a.out > tmp Rumor has it str: 0xbfbfe980 : Rumor has it tp : 0xbfbfe980 : Rumor p : 0xbfbfe986 : has it tp : 0xbfbfe986 : has p : 0xbfbfe98a : it tp : 0xbfbfe98b : it p : 0xbfbfe98e : tp : 0x0 : (null) p : 0x0 : (null) % cat tmp Rumor has it 8