0. , . - , - , , . , - , - , - . , : ; INT (, , UNSIGNED); ; , &. 6.8.  O - , - , - . , - . , - , , INT , FLOAT . - , , . - - , - . , - . UNION U_TAG \( INT IVAL; FLOAT FVAL; CHAR *PVAL; \) UVAL; UVAL , , , , - - . UVAR , : . - , - ; - , , . : . -------------------- -> ---------------------------- , . - , UVAL, - UTYPE, : 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 %D IN UTYPE\N", UTYPE); . ( ) , . , , STRUCT \( CHAR *NAME; INT FLAGS; INT UTYPE; UNION \( INT IVAL; FLOAT FVAL; CHAR *PVAL; \) UVAL; \) SYMTAB[NSYM]; IVAL SYMTAB[I].UVAL.IVAL PVAL *SYMTAB[I].UVAL.PVAL , . - , " " , , . , , - , ; - , . - , - . , 8 , , , - . 6.9.  "C" , TYPEDEF . , TYPEDEF INT LENGTH; LENGTH INT. "" LENGTH , .. , INT: LENGTH LEN, MAXLEN; LENGTH *LENGTHS[]; TYPEDEF CHAR *STRING; STRING CHAR*, , STRING P, LINEPTR[LINES], ALLOC(); , TYPEDEF , TYPEDEF. TYPEDEF EXTERN, STATIC . . - , . - TYPEDEF , - : TYPEDEF STRUCT TNODE \( /* THE BASIC NODE */ CHAR *WORD; /* POINTS TO THE TEXT */ INT COUNT; /* NUMBER OF OCCURRENCES */ STRUCT TNODE *LEFT; /* LEFT CHILD */ STRUCT TNODE *RIGHT; /* RIGHT CHILD */ \) TREENODE, *TREEPTR; : TREENODE () TREEPTR ( ). - TALLOC TREEPTR TALLOC() \( CHAR *ALLOC(); RETURN((TREEPTR) ALLOC(SIZEOF(TREENODE))); \) , TYPEDEF - ; - . : , , . TYPEDEF #DEFINE , - - , - "C". , TYPEDEF INT (*PFI) (); PFI " , - INT", 5 PFI STRCMP, NUMCMP, SWAP; TYPEDEF. , . - , -, - TYPEDEF, . - TYPEDEF SHORT, INT LONG - . TYPEDEF - - TREEPTR - , , . , , - , LINT, - TYPEDEF .  * 7. *  / "", . - , . " /", , / ""- - . , , - . , " ", . , , "" , , "", , - - , , - . - /; , - ""-, - . 7.1.  , , - #INCLUDE <STDIO.H> STDIO.H , /. - , - ( UNIX LUSRLINELUDE). , - ; PDP-11 UNIX, , : CC .. -LS -LS . 7.2. - GETCHAR PUTCHAR - " ", - , GETCHAR. GETCHAR() . , "", - < : PROG - GETCHAR PROG<INFILE , PROG INFILE, . , PROG ; - "<INFILE" ARGV. - , (PIPE) ; OTHERPROG \! PROG , OTHERPROG PROG, , PROG OTHERPROG. GETCHAR EOF, - , - . EOF -1 ( #DEFINE STDIO.H), - EOF, -1, . PUTCHAR(C), '' " ", - . > : PROG PUTCHAR, PROG>OUTFILE OUTFILE, . UNIX - . PROG \! ANOTHERPROG PROG ANOTHERPROG. PROG . , PRINTF, , PUTCHAR PRINTF - . - ; GETCHAR, PUTCHAR PRINTF - . - , . , , LOWER, - - : #INCLUDE <STDIO.H> MAIN() /* CONVERT INPUT TO LOWER CASE */ \( INT C; WHILE ((C = GETCHAR()) != EOF) PUTCHAR(ISUPPER(C) ? TOLOWER(C) : C); \) "" ISUPPER TOLOWER - , STDIO.H . ISUPPER , - , - , , . TOLOWER - . , , - , - . , , CAT UNIX, CAT FILE1 FILE2 ... \! LOWER>OUTPUT , . ( CAT ). , - / "" GETCHAR PUTCHAR . . 8 , . 7.3. - PRINTF : PRINTF SCANF (- ) E . - . PRINTF; . PRINTF(CONTROL, ARG1, ARG2, ...) , CONTROL. - : , - , , PRINTF. % . % - : - , - . - , . - , , . - , , ( , ) - . - , - , ( ). - , . - (), , , - FLOAT DOUBLE. - L, , - LONG, INT. : D - . O - ( ). X - ( 0X). U - . C - . S - : , - , - . E - , FLOAT DOUBLE, [-]M.NNNNNNE[+-]XX, N . 6. F - , FLOAT DOUBLE, [-]MMM.NNNNN, N . 6. , F . G - %E %F, ; - . % , ; , % , %%. . - , . "HELLO, WORLD" (12 ). - , . :%10S: :HELLO, WORLD: :%10-S: :HELLO, WORLD: :%20S: : HELLO, WORLD: :%-20S: :HELLO, WORLD : :%20.10S: : HELLO, WOR: :%-20.10S: :HELLO, WOR : :%.10S: :HELLO, WOR: : PRINTF . - - , .  7-1 -------------- , - . ( ) - . 7.4. - SCANF SCANF PRINTF . SCANF(CONTROL, ARG1, ARG2, ...) , , CONTROL, . - ; , , , . - , - . : - , (" - "), . - ( %), - . - , %, - *, - , - . . - , . , , * - , . , - ; , , . , - , SCANF , . - ; "" . : D - ; - . O - ( - ); . X - ( - 0X ); . H - SHORT; - SHORT. C - ; ; . - ; , , - %1S. S - ; , , \0. F - ; - FLOAT. - E F. FLOAT , , - , E, - , . D, O X L, , LONG, INT. - , L E F, , - DOUBLE, FLOAT. , INT I; FLOAT X; CHAR NAME[50]; SCANF("&D %F %S", &I, &X, NAME); 25 54.32E-1 THOMPSON I 25,X - 5.432 NAME - "THOMPSON", \ 0. , , . INT I; FLOAT X; CHAR NAME[50]; SCANF("%2D %F %*D %2S", &I, &X, NAME); 56789 0123 45A72 I 56, X - 789.0, 0123 NAME "45". - A. - NAME , , &. 4, SCANF: #INCLUDE <STDIO.H> MAIN() /* RUDIMENTARY DESK CALCULATOR */ \( DOUBLE SUM, V; SUM =0; WHILE (SCANF("%LF", &V) !=EOF) PRINTF("\T%.2F\N", SUM += V); \) SCANF , , . - . . EOF; - , 0, . SCANF . : SCANF . - SCANF("%D", N); SCANF("%D", &N); 7.5.  SCANF PRINTF SSCANF SPRINTF, , , . - : SPRINTF(STRING, CONTROL, ARG1, ARG2, ...) SSCANF(STRING, CONTROL, ARG1, ARG2, ...) , SPRINTF ARG1, ARG2 .. , CONTROL, STRING, . K, STRING , . , NAME - - , N - , SPRINTF(NAME, "TEMP%D", N); NAME TEMPNNN, NNN - N. SSCANF - STRING - CONTROL - ARG1, ARG2 .. - . SSCANF(NAME, "TEMP%D", &N); N , TEMP NAME.  7-2 -------------- 4, SCANF / SSCANF. 7.6.  - , - , - . - - , , - . , - , CAT, - . CAT , . , CAT X.C.Y.C X.C Y.C . , - , .., , , - . . - , FOPEN . FOPEN ( X.C Y.C), - - ( ) , . , " ", - , - , , , . , -, STDIO.H, - FILE. : FILE *FOPEN(), *FP; , FP FILE FOPEN FILE. O , FILE , INT, - ; TYPEDEF. ( , UNIX, 8). FOPEN : FP=FOPEN(NAME,MODE); FOPEN "" , - . MODE ("") , - , . - : ("R"), ("W") ("A"). , , - , ( ). . - . - (, , ). - NULL ( STDIO.H). - , . - , GETC PUTC . GETC ; - , , . - , C=GETC(FP) "C" , - FP, EOF, . PUTC, GETC, PUTC(C,FP) "C" FP "C". - GETCHAR PUTCHAR, GETC PUTC , . - , . , - ; - STDIN, STDOUT STDERR. - , STDIN STDOUT - (PIPE), 7.2. GETCHAR PUTCHAR - GETC, PUTC, STDIN STDOUT : #DEFINE GETCHAR() GETC(STDIN) #DEFINE PUTCHAR(C) PUTC(C, STDOUT) - FSCANF FPRINTF. SCANF PRINTF, , , , - ; . , CAT . : - , . - , . , . #INCLUDE <STDIO.H> MAIN(ARGC, ARGV) /*CAT: CONCATENATE FILES*/ INT ARGC; CHAR *ARGV[]; \( FILE *FP, *FOPEN(); IF(ARGC==1) /*NO ARGS; COPY STANDARD INPUT*/ FILECOPY(STDIN); ELSE WHILE (--ARGC > 0) IF ((FP=FOPEN(*++ARGV,"R"))==NULL) \( PRINTF("CAT:CAN'T OPEN %\N",*ARGV); BREAK; \) ELSE \( FILECOPY(FP); FCLOSE(FP); \) \) FILECOPY(FP) /*COPY FILE FP TO STANDARD OUTPUT*/ FILE *FP; \( INT C; WHILE ((C=GETC(FP)) !=EOF) PUTC(C, STDOUT); \) STDIN STDOUT - - ; , - FILE*. , , - - . FCLOSE FOPEN; , FOPEN, . - , . , CAT, , - . - FCLOSE - , PUTC . ( FCLOSE - ). 7.7. - STDERR EXIT CAT . - , - , - . , , , - (PIPELINE) - . , , STDIN STDOUT, , STDERR. , , STDERR, , - . CAT , . "INCLUDE <STDIO.H> MAIN(ARGC,ARGV) /*CAT: CONCATENATE FILES*/ INT ARGC; CHAR *ARGV[]; \( FILE *FP, *FOPEN(); IF(ARGC==1) /*NO ARGS; COPY STANDARD INPUT*/ FILECOPY(STDIN); ELSE WHILE (--ARGC > 0) IF((FP=FOPEN(*++ARGV,"R#))==NULL) \( PRINTF(STDERR, "CAT: CAN'T OPEN,%S\N", ARGV); EXIT(1); \) ELSE \( FILECOPY(FP); \) EXIT(0); \) . - , FPRINTF, STDERR , , - , (PIPELINE) - . EXIT , - . EXIT - , , - - , . 0 - , , . EXIT FCLOSE - , , _EXIT. _EXIT - ; , - . 7.8.  FGETS, - GETLINE, . FGETS(LINE, MAXLINE, FP) ( ) - FP LINE; MAXLINE_1 . - \ 0. FGETS LINE; NULL. ( GETLINE , - ). FPUTS ( ) : FPUTS(LINE, FP) , FGETS FPUTS , , -: #INCLUDE <STDIO.H> CHAR *FGETS(S,N,IOP) /*GET AT MOST N CHARS FROM IOP*/ CHAR *S; INT N; REGISTER FILE *IOP; \( REGISTER INT C; REGISTER CHAR *CS; CS = S; WHILE(--N>0&&(C=GETC(IOP)) !=EOF) IF ((*CS++ = C)=='\N') BREAK; *CS = '\0'; RETURN((C==EOF && CS==S) 7 NULL : S); \) FPUTS(S,IOP) /*PUT STRING S ON FILS IOP*/ REGISTER CHAR *S; REGISTER FILE *IOP; \( REGISTER INT C; WHILE (C = *S++) PUTC(C,IOP); \)  7-3 --------------- , , - .  7-4 --------------- - 5 , - , , . ?  7-5 -------------- , - . 7.9.  - , . : STRLEN, STRCPY, STRCAT STRCMP. . 7.9.1.  - : SALPHA(C) 0, "C" , 0 - . SUPPER(C) 0, "C" , 0 - . SLOWER(C) 0, "C" , 0 - . SDIGIT(C) 0, "C" , 0 - . SSPACL(C) 0, "C" , , 0 - . OUPPER(C) "C" . OLOWER(C) "C" . 7.9.2. UNGETC UNGETCH, 4; - UNGETC. UNGETC(C,FP) "C" FP. . UNGETC SCANF, GETC GETCHAR. 7.9.3.  SYSTEM(S) , - S, . S - . , , UNIX SYSTEM("DATE"); DATE, . 7.9.4.  CALLOC ALLOC, - . CALLOC(N, SIZEOF(OBJCCT)) , N , NULL, . - . , , CHAR *CALLOC(); INT *IP; IP=(INT*) CALLOC(N,SIZEOF(INT)); CFREE(P) , "P", "P" CALLOC. , -, - CALLOC. , CALLOC, , 8.  * 8. UNIX *  -- UNIX. "C" UNIX, . - , - - "C". : /, . - UNIX. 7 , . - -, . - UNIX , . 8.1.  UNIX - , , - ,