Chinese Translation of the C FAQ

Similar documents
新・明解C言語入門編『索引』

新版 明解C++入門編

_汪_文前新ok[3.1].doc

C 1

CC213

FY.DOC

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

C/C++语言 - C/C++数据

CC213

C/C++ 语言 - 循环

nooog

智力测试故事

ebook8-30

30,000,000 75,000,000 75,000, (i) (ii) (iii) (iv)

I. 1-2 II. 3 III. 4 IV. 5 V. 5 VI. 5 VII. 5 VIII. 6-9 IX. 9 X XI XII. 12 XIII. 13 XIV XV XVI. 16

c_cpp

Ps22Pdf

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

untitled

2015年廉政公署民意調查

奇闻怪录

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

Microsoft Word - John_Ch_1202

全唐诗50

,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : : : /16 : : 96 : : : ISBN 7

ebook15-C

C

C++ 程式設計

untitled

untitled

C/C++语言 - 分支结构

[改訂新版]C言語による標準アルゴリズム事典

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

untitled

施 的 年 度 維 修 工 程 已 於 4 月 15 日 完 成, 並 於 4 月 16 日 重 新 開 放 給 市 民 使 用 ii. 天 水 圍 游 泳 池 的 年 度 維 修 工 程 已 於 3 月 31 日 完 成, 並 於 4 月 1 日 重 新 開 放 給 市 民 使 用 iii. 元

untitled

对联故事

我 非 常 希 望 该 小 组 的 建 议 尤 其 是 其 执 行 摘 要 能 受 到 将 于 2000 年 9 月 来 纽 约 参 加 千 年 首 脑 会 议 的 所 有 领 导 人 的 注 意 这 次 历 史 性 的 高 级 别 会 议 提 供 了 一 个 独 特 的 机 会 使 我 们 能 够

一、

-i-

Microsoft Word - 强迫性活动一览表.docx

<4D F736F F D20BB4FAA46BFA4B2C4A447B4C15F D313038A67E5FBAEEA658B56FAE69B9EAAC49A4E8AED72D5FAED6A977A5BB5F >


W. Richard Stevens UNIX Sockets API echo Sockets TCP OOB IO C struct C/C++ UNIX fork() select(2)/poll(2)/epoll(4) IO IO CPU 100% libevent UNIX CPU IO

Microsoft Word - COC HKROO App I _Chi_ Jan2012.doc

epub 33-8

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1

51 C 51 isp 10 C PCB C C C C KEIL

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

Microsoft Word - CPE考生使用手冊 docx


<D6D0B9FAB9C5CAB757512E6D7073>

Ps22Pdf

中医疗法(下).doc

Microsoft Word - Final Chi-Report _PlanD-KlnEast_V7_ES_.doc

眼病防治

中国南北特色风味名菜 _八)

CC213

RDEC-RES

Microsoft Word - Entry-Level Occupational Competencies for TCM in Canada200910_ch _2_.doc


全唐诗28

untitled

Microsoft PowerPoint - 20-string-s.pptx

bingdian001.com

(b)

我国服装行业企业社会责任问题的探讨.pages

II II

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

95年度社區教育學習計畫執行成果報告

「保險中介人資格考試」手冊

心理障碍防治(下).doc

財 務 委 員 會 審 核 2014 至 2015 年 度 開 支 預 算 的 報 告 2014 年 7 月

3.1 num = 3 ch = 'C' 2

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

<4D F736F F D205B345DB5D8AE4CACD AECAAFC5C1C9C1DCBDD0AB48A4CEB3F8A657AAED>

科学计算的语言-FORTRAN95

C PICC C++ C++ C C #include<pic.h> C static volatile unsigned char 0x01; static volatile unsigned char 0x02; static volatile unsigned cha

江苏宁沪高速公路股份有限公司.PDF

Microsoft Word - MP2018_Report_Chi _12Apr2012_.doc

南華大學數位論文

李天命的思考藝術

皮肤病防治.doc

性病防治

中国南北特色风味名菜 _一)

Transcription:

C Steve Summit, 0.9.3, 2005 5 18 c 2005

C FAQ (2004 7 3 ) Steve Summit The English version of this FAQ list is Copyright 1990-2004 by Steve Summit. Content from the book C Programming FAQs: Frequently Asked Questions is made available here by permission of the author and the publisher as a service to the community. It is intended to complement the use of the published text and is protected by international copyright laws. The on-line content may be accessed freely for personal use but may not be republished without permission. HTML http://c-faq-chn.sourceforge.net/ PDF http://sourceforge.net/projects/ c-faq-chn L A TEX Steve Summit (scs@eskimo.com), (sunyun.s@gmail.com, 1 12 ) (zhu.qunying@ gmail.com, 13 20 L A TEX )

i 1 1 1.1................... 1 1.2 64 64................ 1 1.3............... 2 1.4 extern................. 2 1.5 auto................... 2 1.6 typedef struct { char *item; NODEPTR next; } *NODEPTR; C.... 3 1.7 N.............. 3 1.8,,.. 4 1.9 main() void main()......... 4 1.10,....... 4 1.11 int f() { char a[] = "Hello, world!";}....... 5 1.12 char *p = malloc(10);........................... 5 1.13 char a[] = "string literal"; char *p = "string literal"; p[i],.... 5 1.14,.. 5 2 7 2.1 struct x1 {... }; typedef struct {... } x2;. 7 2.2 struct x {... }; x thestruct;............. 7 2.3............... 7 2.4 C?........... 7 2.5 C... 7 i

ii 2.6 : struct name { int namelen; char namestr[1];}; namestr............ 8 2.7.................... 8 2.8.............. 8 2.9 / /................... 9 2.10,,?.............................. 9 2.11 sizeof,?.. 9 2.12................. 9 2.13............... 10 2.14, core dump..... 10 2.15....................... 10 2.16 #define............ 10 2.17............... 11 3 13 3.1 : a[i] = i++;............ 13 3.2, int i=7; printf("%d\n", i++ * i++); 49, 56...... 13 3.3 int i = 3; i = i++;, 3, 4,..................... 14 3.4 : a ˆ= b ˆ= a ˆ= b a b......................... 14 3.5.......... 14 3.6 && while((c = getchar())!= EOF && c!= \n ).................. 14 3.7......... 15 3.8, a[i] = i++; a[], i 1,......................... 15 3.9 ++i i++...................... 15 3.10, ++i i++.................................. 15 3.11 int a = 100, b = 100; long int c = a * b;................................. 15 3.12 ((condition)? a : b) = complicated expression;........................... 16

iii 4 17 4.1, char *p; *p = malloc(10);................ 17 4.2 *p++ p p................ 17 4.3 char * int, ((int *)p)++;.............. 17 4.4, void f(int *ip) { static int dummy = 5; ip = &dummy;} : int *ip; f(ip);............... 18 4.5 void**,.. 18 4.6 extern int f(int *); int f(&5);. 18 4.7 C....................... 18 4.8... 19 4.9 int char *,.................................. 19 5 (null) 21 5.1................... 21 5.2................... 21 5.3 if(p) 0....................... 22 5.4 NULL,.................. 23 5.5, NULL.................................. 23 5.6 NULL #define NULL ((char *)0) NULL.................. 23 5.7 NULL 0,.................................. 24 5.8 NULL,, NULL ( 0)............. 24 5.9 #define Nullptr(type) (type *)0................................ 24 5.10 NULL 0, (null).. 24 5.11................................. 25 5.12....... 25 5.13, 0....................... 26 5.14,, 26

iv 5.15............. 26 6 27 6.1 char a[6], extern char *a....................... 27 6.2 char a[ ] char *a............. 27 6.3, C..... 28 6.4..... 28 6.5,........ 29 6.6,............... 29 6.7,.................. 29 6.8, 5["abcdef"] C.................. 29 6.9, arr, arr &arr............................ 30 6.10..................... 30 6.11.................................. 30 6.12.......... 30 6.13.................... 31 6.14 : int realarray[10]; int *array = &realarray[-1]; array 1.. 32 6.15,................................ 32 6.16....... 32 6.17....... 33 6.18, sizeof 34 7 35 7.1 char *answer; printf("type something:\n"); gets(answer); printf("you typed \"%s\"\n", answer);....... 35 7.2 strcat(). char *s1 = "Hello, "; char *s2 = "world!"; char *s3 = strcat(s1, s2);....... 35 7.3 strcat char * ( )........................ 36 7.4 char *p; strcpy(p, "abc");................ 36 7.5.................... 36 7.6,,,......................... 36

v 7.7........ 37 7.8 malloc(), :................................ 37 7.9 malloc 37 7.10 malloc(), void * int *................................ 37 7.11 char *p = malloc(strlen(s) + 1); strcpy(p, s); malloc((strlen(s) + 1) * sizeof(char))..... 37 7.12........................ 38 7.13 malloc............................ 38 7.14, : double *array = malloc(300 * 300 * sizeof( double )); malloc() null,,, malloc(),......... 38 7.15 PC 8 640K.. 38 7.16, malloc malloc() bug............... 38 7.17,...... 38 7.18 free() (, )..................... 39 7.19 malloc(), free()........................ 39 7.20,,...... 39 7.21.......... 40 7.22,,.................. 40 7.23 free()............... 40 7.24 malloc,....... 40 7.25 realloc() 40 7.26 calloc() malloc() calloc free() calloc(), cfree() 40 7.27 alloca().............. 41 8 43 8.1 strcat(string,! );.................. 43 8.2 char *string;... if(string == "value") { /* string matches value */... }.................................. 43

vi 8.3 char a[] = "Hello, world!"; char a[14]; a = "Hello, world!";...................... 43 8.4 ( ),....... 44 8.5 : sizeof( a ) 2 1 (, sizeof(char)).......................... 44 9 45 9.1 C #define enum true false..... 45 9.2 C, TRUE 1 1.......................... 45 9.3 p, if(p)............. 46 10 C 47 10.1 : #define begin { #define end }.................................. 47 10.2............... 47 10.3................. 47 10.4,.c,.h (.h )....... 48 10.5................ 48 10.6 #include <> #include ""............ 48 10.7................. 49 10.8,................................ 49 10.9,................................ 49 10.10,........................ 49 10.11 #if........... 49 10.12 sizeof #if............ 50 10.13 #include #ifdef. 50 10.14 typdef #ifdef......... 50 10.15 #if. 50 10.16, #ifdef #include #ifdef,............................. 50 10.17................... 50

vii 10.18, #define Paste(a, b) a/**/b...................... 51 10.19 #define TRACE(n) printf("trace: %d\n", n) TRACE(count); printf("trace: %d\count", count);.............. 51 10.20 #,..... 51 10.21,......... 51 10.22..................... 51 11 ANSI/ISO C 53 11.1 ANSI C....................... 53 11.2.................... 53 11.3................... 54 11.4 ANSI extern int func(float); int func(x) float x; {............. 54 11.5................ 55 11.6 extern int f(struct x *p); x.................. 55 11.7 : const int n = 5; int a[n];....................... 55 11.8,.................................. 55 11.9 const char *p char * const p.......... 56 11.10 const char ** char **..... 56 11.11 main()........................ 56 11.12 main() void, main.................................. 56 11.13 main() envp............ 57 11.14 main() void, exit() return, /.. 57 11.15 void main() 57 11.16 C void main().. 57 11.17 main(), exit(status) status... 57 11.18 ANSI #,..... 58 11.19 warning: macro replacement within a string literal.............................. 58 11.20 #ifdef....... 58 11.21 #pragma,.................... 59 11.22 #pragma once.. 59

viii 11.23 a[3] = "abc";............... 59 11.24 void*............... 59 11.25 memcpy() memmove().............. 59 11.26 malloc(0) 0. 59 11.27 ANSI... 60 11.28..... 60 11.29 ASNI/ISO ANSI............................ 60 11.30 C ANSI C,............................. 60 11.31 ANSI Frobozz Magic C ANSI, gcc..... 60 11.32 (implementation-defin-ed) (unspecified) (undefined)................................ 61 11.33,..... 61 11.34, ANSI................. 61 11.35 i = i++, ANSI,............... 62 12 63 12.1 char c; while((c = getchar())!= EOF)... 63 12.2 EOF, EOF........................... 63 12.3 while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); }........... 63 12.4,................. 63 12.5.............. 64 12.6 printf % \%, 64 12.7 printf %lf, scanf() %lf, printf() %f....... 64 12.8 size t, long, printf......... 64 12.9 printf, %8d........................ 64 12.10....... 65 12.11 scanf("%d", i).................. 65 12.12 char s[30]; scanf("%s", s); &........ 65

ix 12.13 double d; scanf("%f", &d);........ 65 12.14 scanf()............. 65 12.15 %d\n scanf,.......................... 65 12.16 scanf %d, gets(), gets()................... 66 12.17, scanf(),. 66 12.18 scanf()... 66 12.19 sprintf sprintf()............... 66 12.20 gets().................. 67 12.21 printf() errno ENOTTY.......... 67 12.22 fgetops/fsetops ftell/fseek fgetops() fsetops()....................... 68 12.23, fflush(stdin)................................ 68 12.24 fflush(),........... 68 12.25 fopen()............. 68 12.26 r+,,,............... 69 12.27 stdin stdout.......... 69 12.28 freopen(), stdout ( stdin) 69 12.29,...... 69 12.30 0x0a 0x0d, 0x1a, EOF.... 70 13 71 13.1 ( atoi ) itoa().... 71 13.2 strncpy() \0....... 71 13.3 toupper() toupper() tolower()......... 71 13.4 main() argc argv....................... 72 13.5........ 72 13.6 strcmp(), qsort(),............................ 72 13.7 qsort(), qsort().............. 73

x 13.8........................ 73 13.9.................... 73 13.10 C............... 73 13.11 localtime() time t struct tm, ctime() time t, struct tm time t....... 74 13.12 N...... 74 13.13..................... 75 13.14................. 75 13.15, rand().......... 75 13.16 /, rand() % 2, 0, 1, 0, 1, 0....................... 76 13.17.............. 76 13.18, #inlude............................. 77 13.19,............................. 77 13.20 end................ 77 13.21 printf........... 77 14 79 14.1 float 3.1, printf 3.0999999 79 14.2,......... 79 14.3, #include <math.h>, undefined: sin ( sin )...... 79 14.4,.... 79 14.5... 80 14.6............................. 80 14.7 C.................. 80 14.8 <math.h> M PI...... 80 14.9 IEEE NaN............... 81 14.10 C.................... 81 14.11 (FFT) ( )....... 81 14.12 Turbo C, floating point formats not linked ( )..................... 81 15 83 15.1 printf(), #include <stdio.h>..... 83

xi 15.2 %f printf(), float double........................ 83 15.3 n long int, printf("%d", n); ANSI....... 83 15.4................... 83 15.5 printf(), printf()................................ 85 15.6 scanf(), scanf()................................ 85 15.7.............. 85 15.8. 86 15.9 float, va arg(argp, float) 86 15.10 va arg()............. 86 15.11,.................................. 86 15.12............ 87 16 89 16.1,.. 89 16.2....... 89 16.3,, main()................................ 89 16.4, main().............................. 89 16.5,,,... 90 16.6 : char *p = "hello, worl!"; p[0] = H ;... 90 16.7 Segmentation violation, Bus error General protection fault............................. 91 17 93 17.1 C.................. 93 17.2 if(!strcmp(s1, s2)),... 93 17.3 if (0 == x) if (x == 0)......... 93 17.4 extern int func ((int, int));,.......................... 94 17.5 printf(), (void).. 94 17.6 (Hungarian Notation).. 94 17.7 (Indian Hill Style Guide)............................ 94

xii 17.8 goto,.. 95 18 97 18.1............................ 97 18.2 malloc.................... 98 18.3............... 98 18.4..................... 98 18.5 ANSI lint.................. 99 18.6 ANSI lint........... 99 18.7 C................. 99 18.8,......... 100 18.9 C........ 100 18.10 C............... 101 18.11 C.................. 101 18.12 ANSI/ISO C................. 101 18.13................. 101 18.14 C BNF YACC............. 101 18.15 C..................... 101 18.16........... 102 18.17................... 102 18.18............ 102 19 105 19.1 RETURN............................ 105 19.2,,,............................. 106 19.3...... 106 19.4... 106 19.5,...................... 107 19.6......................... 107 19.7 ( comm )................ 107 19.8....................... 107 19.9.......... 108 19.10..................... 108 19.11............................. 108 19.12 GIF JPEG................... 108 19.13.................... 108 19.14,................. 109 19.15................. 109

xiii 19.16............... 109 19.17 ( ).............. 109 19.18.......... 110 19.19......................... 110 19.20......................... 110 19.21 fopen("c:\ newdir \file.dat", "r")...................... 110 19.22 fopen() : "$HOME/.profile" " /.myrcfile". 111 19.23 MS-DOS Abort, Retry, Ignore? 111 19.24 Too many open files (,....................... 111 19.25 C........................ 111 19.26................. 111 19.27 64K................. 111 19.28 DGROUP data allocation exceeds 64K (DGROUP 64K), 64K.......... 112 19.29 ( ) 112 19.30 C (, ).................................. 112 19.31,......... 113 19.32.......... 113 19.33........... 113 19.34............ 113 19.35........... 114 19.36....... 114 19.37 control-c.......... 114 19.38..................... 115 19.39 socket /......... 115 19.40 BIOS ISR TSR............ 115 19.41, union REGS int86(). 115 19.42 near far.................... 116 19.43, ANSI!. 116 20 117 20.1.................... 117 20.2........................ 117 20.3,............................ 117 20.4 char *......... 117

xiv 20.5..................... 118 20.6..... 118 20.7............................ 118 20.8................ 119 20.9 printf().. 119 20.10 1....... 119 20.11................. 119 20.12 ++i i = i +1............................... 120 20.13,, i/=2.............. 120 20.14................. 120 20.15................. 121 20.16 case ( ) 121 20.17 return.............. 121 20.18 C........................ 121 20.19 C a+++++b............................. 122 20.20 C...................... 122 20.21 assert()..................... 122 20.22 C FORTRAN (C++, BASIC, Pascal, Ada, LISP)......................... 122 20.23 Pascal Fortran ( LISP, Ada, awk, C) C........................... 123 20.24 C++ C C++ C.. 123 20.25 strcmp,,.............................. 123 20.26............................ 124 20.27,.................. 124 20.28 (year%4 == 0) 2000...... 124 20.29 :.......... 124 20.30 (Duff s Device)................ 125 20.31 C (IOCCC)..................... 125 20.32 [K&R1] entry................ 126 20.33 C......................... 126 20.34 char.......................... 126 20.35 lvalue rvalue................ 126

xv 20.36 FAQ ( )........... 126 21 129 131

xvi

1 1.1 ( 32, 767 32, 767), long, ( ), short, int,,, ( ), ; 12.1,, C99 <inttypes.h> typedef,, 20.3 C, 18.17 : [K&R1, Sec. 2.2 p. 34]; [K&R2, Sec. 2.2 p. 36, Sec. A4.2 pp. 195-6, Sec. B11 p. 257]; [ISO, Sec. 5.2.4.2.1, Sec. 6.1.2.5]; [H&S, Secs. 5.1,5.2 pp. 110-114] 1.2 64 64 C99 long long, 64, longlong, 16 32 64, 18.17 : [C9X, Sec. 5.2.4.2.1, Sec. 6.1.2.5] 1

1 2 1.3, ( ), ( ).c, (.h),,.c, : ANSI C, ANSI Unix, ; ANSI C, DEFINE(int, i);,,,.c :, 10.4 18.6 : [K&R1, Sec. 4.5 pp. 76-7]; [K&R2, Sec. 4.4 pp. 80-1]; [ISO, Sec. 6.1.2.2, Sec. 6.7, Sec. 6.7.2, Sec. G.5.11]; [Rationale, Sec. 3.1.2.2]; [H&S, Sec. 4.8 pp. 101-104, Sec. 9.2.3 p. 267]; [CT&P, Sec. 4.2 pp. 54-56]. 1.4 extern, extern int f(); int f(); : [ISO, Sec. 6.1.2.2, Sec. 6.5.1]; [Rationale, Sec. 3.1.2.2]; [H&S, Secs. 4.3,4.3.1 pp. 75-6]. 1.5 auto 20.32 : [K&R1, Sec. A8.1 p. 193]; [ISO, Sec. 6.1.2.4, Sec. 6.5.1;]; [H&S, Sec. 4.3 p. 75, Sec. 4.3.1 p. 76].

1 3 1.6 typedef struct { char *item; NODEPTR next; } *NODEPTR; C C ; [K&R2, 6.5 ] NODEPTR next typedef, ( struct node ), next struct node *, typedef, : struct node { char *item; struct node *next; }; typedef struct node *NODEPTR; typedef, 2.1 : [K&R1, Sec. 6.5 p. 101]; [K&R2, Sec. 6.5 p. 139]; [ISO, Sec. 6.5.2, Sec. 6.5.2.3]; [H&S, Sec. 5.6.1 pp. 132-3] 1.7 N 3 : 1. char *(*(*a[n])())(); 2. typedef : typedef char *pc; /* */ typedef pc fpc(); /* */ typedef fpc *pfpc; /* */ typedef pfpc fpfpc(); /* */ typedef fpfpc *pfpfpc; /* */ pfpfpc a[n]; /* */ 3. cdecl, C C : cdecl> declare a as array of pointer to function returning pointer to function returning pointer to char char *(*(*a[])())(), cdecl, ( ) 18.1

1 4 C C ( ), cdecl : [K&R2, Sec. 5.12 p. 122]; [ISO, Sec. 6.5ff (esp. Sec. 6.5.4)]; [H&S, Sec. 4.5 pp. 85-92, Sec. 5.10.1 pp. 149-50] 1.8,, ( ) (int) ( ), ( ) 11.4 15.1 : [K&R1, Sec. 4.2 p. 70]; [K&R2, Sec. 4.2 p. 72]; [ISO, Sec. 6.3.2.2]; [H&S, Sec. 4.7 p. 101]. 1.9 main() void main() 11.11 11.16 ( ) 1.10, (, ), =0,, 0.0 (, 5 ) (, ), ( );, malloc() realloc(), calloc(), ( 7.26 5 ) : [K&R1, Sec. 4.9 pp. 82-4]; [K&R2, Sec. 4.9 pp. 85-86]; [ISO, Sec. 6.5.7, Sec. 7.10.3.1, Sec. 7.10.5.3]; [H&S, Sec. 4.2.8 pp. 72-3, Sec. 4.6 pp. 92-3, Sec. 4.6.2 pp. 94-5, Sec. 4.6.3 p. 96, Sec. 16.1 p. 386.]

1 5 1.11 int f() { char a[] = "Hello, world!";} ANSI, (automatic aggregates, ) 11.28 1.12 char *p = malloc(10); ( ) 1.13 char a[] = "string literal"; char *p = "string literal"; p[i], ( char a[] ),,,,, ( 6 ), p, 1.11 6.1 6.2 6.6 : [K&R2, Sec. 5.5 p. 104]; [ISO, Sec. 6.1.4, Sec. 6.5.7]; [Rationale, Sec. 3.1.4]; [H&S, Sec. 2.7.4 pp. 31-2] 1.14, extern int func(); int (*fp)() = func;, (, ), ( ) ( ) 1.8 4.8

1 6

2 2.1 struct x1 {... }; typedef struct {... } x2; ; struct x1, x2,, struct 2.2 struct x {... }; x thestruct; C C++ 2.1 2.3 1.6 2.4 C? ( ), ( ) 2.5 C,,, (, ),,,, C++ 7

2 8 2.6 : struct name { int namelen; char namestr[1];}; namestr, Dennis Ritchie C C,, ; :... char namestr[maxsize]; MAXSIZE name,,, C99, : [Rationale, Sec. 3.5.4.2]; [C9X, Sec. 6.5.2.1] 2.7 (, == ), C ( 2.10) ;, : [K&R2, Sec. 6.2 p. 129]; [Rationale, Sec. 3.3.9]; [H&S, Sec. 5.6.2 p. 133] 2.8 C ; C99 (compound literals);, plotpoint(), plotpoint((struct point){1, 2}); (designated initializers) (C99 ), : plotpoint((struct point){.x=1,.y=2}); 4.6 : [C9X, Sec. 6.3.2.5, Sec. 6.5.8]

2 9 2.9 / / fwrite() : fwrite(&somestruct, sizeof somestruct, 1, fp); fread() ( 2.10 20.3),,,,, b ; 12.30, ( ), : [H&S, Sec. 15.13 p. 381] 2.10,,?,,, ( ) (, char ), 2 4 ( #pragma; 11.21), 20.3 : [K&R2, Sec. 6.4 p. 138]; [H&S, Sec. 5.6.4 p. 135] 2.11 sizeof,?,,, sizeof 2.10 : [H&S, Sec. 5.6.7 pp. 139-40] 2.12 ANSI C <stddef.h> offsetof(), offsetof(struct s, f) f s,, : 292-3] #define offsetof(type, f) ((size_t) \ ((char *)&((type *)0)->f - (char *)(type *)0)) 100% ; : [ISO, Sec. 7.1.6]; [Rationale, Sec. 3.5.4.2]; [H&S, Sec. 11.1 pp.

2 10 2.13 offsetof() ( 2.12) structp, f, offsetf, f : *(int *)((char *)structp + offsetf) = value; 2.14, core dump : struct list { char *item; struct list *next; } /* main */ main(argc, argv) {... } main(),,,, main(), (, C ) 10.8 16.4 : [CT&P, Sec. 2.3 pp. 21-2] 2.15 ANSI C, C99, : [K&R2, Sec. 6.8 pp. 148-9]; [ISO, Sec. 6.5.7]; [C9X, Sec. 6.5.8]; [H&S, Sec. 4.6.7 p. 100] 2.16 #define C, : ;, ;,, ; : [K&R2, Sec. 2.3 p. 39, Sec. A4.2 p. 196]; [ISO, Sec. 6.1.2.5, Sec. 6.5.2, Sec. 6.5.2.2, Annex F]; [H&S, Sec. 5.5 pp. 127-9, Sec. 5.11.2 p. 153]

2 11 2.17,,,

2 12

3 3.1 : a[i] = i++; i++ i i,, ( a[i] ) (, K&R, C, 11.32 : [K&R1, Sec. 2.12]; [K&R2, Sec. 2.12]; [ISO, Sec. 6.3]; [H&S, Sec. 7.12 pp. 227-9] 3.2, int i=7; printf("%d\n", i++ * i++); 49, 56 ++ --, ( ANSI C,, 3.7), (,, ; 3.7, 11.32 ) ( C ); K&R,, : [K&R1, Sec. 2.12 p. 50]; [K&R2, Sec. 2.12 p. 54]; [ISO, Sec. 6.3]; [H&S, Sec. 7.12 pp. 227-9]; [CT&P, Sec. 3.7 p. 47]; [PCS, Sec. 9.5 pp. 120-1] 13

3 14 3.3 int i = 3; i = i++;, 3, 4, ; 3.1, 3.7 11.32, i++ ++i i+1 i 1, i=i+1, i+=1, i++ ++i,, 3.10 3.4 : a ˆ= b ˆ= a ˆ= b a b a,, : int a = 123, b = 7654; a ^= b ^= a ^= b; SCO C (icc) b 123, a 0 3.1 3.7 20.14 3.5,. f() + g() * h(),, : [K&R1, Sec. 2.12 p. 49, Sec. A.7 p]; [K&R2, Sec. 2.12 pp. 52-3, Sec. A.7 p. 200.] 3.6 && while((c = getchar())!= EOF && c!= \n ) : (, && ),,, (? : ) ( 3.7) : [K&R1, Sec. 2.6 p. 38, Secs. A7.11-12 pp. 190-1]; [K&R2, Sec. 2.6 p. 41, Secs. A7.14-15 pp. 207-8]; [ISO, Sec. 6.3.13, Sec. 6.3.14, Sec. 6.3.15]; [H&S, Sec. 7.7 pp. 217-8, Sec. 7.8 pp. 218-20, Sec. 7.12.1 p. 229]; [CT&P, Sec. 3.7 pp. 46-7]

3 15 3.7 ( &&? :, ),, ANSI/ISO C :,, i = i+1, a[i] = i++ ( 3.1) 3.8 : [ISO, Sec. 5.1.2.3, Sec. 6.3, Sec. 6.6, Annex C]; [Rationale, Sec. 2.1.2.3]; [H&S, Sec. 7.12.1 pp. 228-9] 3.8, a[i] = i++; a[], i 1,, 3.2, 3.3, 11.32 11.35 3.9 ++i i++ C, : ++i i, ; i++ i, 3.10, ++i i++,,, C++, 3.3 3.11 int a = 100, b = 100; long int c = a * b; C, int, long c, long : long int c = (long int)a * b;

3 16, (long int)(a * b),, : [K&R1, Sec. 2.7 p. 41]; [K&R2, Sec. 2.7 p. 44]; [ISO, Sec. 6.2.1.5]; [H&S,, ]; [CT&P, Sec. 3.9 pp. 49-50] 3.12 ((condition)? a : b) = complicated expression;? :,,,,? :, : *((condition)? &a : &b) = complicated_expression; : [ISO, Sec. 6.3.15]; [H&S, Sec. 7.1 pp. 179-180]

4 4.1, char *p; *p = malloc(10); p, *p, (, ), : p = malloc(10);, * : *p = H ; 1.7, 7.1, 7.5 8.3 : [CT&P, Sec. 3.1 p. 28] 4.2 *p++ p p ++ --, *p++ *(p++), p p p, (*p)++, ++*p : [K&R1, Sec. 5.1 p. 91]; [K&R2, Sec. 5.1 p. 95]; [ISO, Sec. 6.3.2, Sec. 6.3.3]; [H&S, Sec. 7.4.4 pp. 192-3, Sec. 7.5 p. 193, Secs. 7.5.7,7.5.8 pp. 199-200] 4.3 char * int, ((int *)p)++; C,, ;, (rvalue), ++ (,, ) : p = (char *)((int *)p + 1);, p char *, p += sizeof(int);,,, : [K&R2, Sec. A7.5 p. 205]; [ISO, Sec. 6.3.4]; [Rationale, Sec. 3.3.2.4]; [H&S, Sec. 7.1 pp. 179-80] 17

4 18 4.4, void f(int *ip) { static int dummy = 5; ip = &dummy;} : int *ip; f(ip); C, ( ), 4.5 4.7 4.5 void**, C void*,, ;, void* void**, 4.6 extern int f(int *); int f(&5); C99, : f((int[]){5}); C99, ;, : int five = 5; f(&five); 2.8, 4.4 20.1 4.7 C, C,, &, ( 6.4 ), C C++, 4.4 20.1 : [K&R1, Sec. 1.8 pp. 24-5, Sec. 5.2 pp. 91-3]; [K&R2, Sec. 1.8 pp. 27-8, Sec. 5.2 pp. 95-7]; [ISO, Sec. 6.3.2.2]; [H&S, Sec. 9.5 pp. 273-4]

4 19 4.8, * ( ) : int r, func(), (*fp)() = func; r = (*fp)();, (, 1.14) fp r = fp(); ANSI C, *, 1.14 : [K&R1, Sec. 5.12 p. 116]; [K&R2, Sec. 5.11 p. 120]; [ISO, Sec. 6.3.2.2]; [Rationale, Sec. 3.3.2.2]; [H&S, Sec. 5.8 p. 147, Sec. 7.4.3 p. 190] 4.9 int char *,,, 13.1, 8.4, 19.29

4 20

5 (null) 5.1,,, &, malloc(),, malloc(), : ; 1.10 7.1 7.26,,,, ( 5.2 5.5 5.6) [K&R1, Sec. 5.4 pp. 97-8]; [K&R2, Sec. 5.4 p. 102]; [ISO, Sec. 6.2.2.3]; [Rationale, Sec. 3.2.2.3]; [H&S, Sec. 5.3.2 pp. 121-3] 5.2, 0,,, 0 char *p = 0; if(p!= 0) 5.3,, 0, 0, Unix execl execl("/bin/sh", "sh", "-c", "date", (char *)0); (char *),, 0 ( Unix ) 21

5 (null) 22,,,, 0 ( 15.3) : 0: :, [K&R1, Sec. A7.7 p. 190, Sec. A7.14 p. 192]; [K&R2, Sec. A7.10 p. 207, Sec. A7.17 p. 209]; [ISO, Sec. 6.2.2.3]; [H&S, Sec. 4.6.3 p. 95, Sec. 6.2.7 p. 171] 5.3 if(p) 0 C, 0,, if(expr) expr, if((expr)!= 0) p expr if(p) if(p!= 0), 0, ;,! :!expr (expr) 0:1 ((expr) == 0) if(!p) if(p == 0) if(p),, ( ; 17.8) 9.2 [K&R2, Sec. A7.4.7 p. 204]; [ISO, Sec. 6.3.3.3, Sec. 6.3.9, Sec. 6.3.13, Sec. 6.3.14, Sec. 6.3.15, Sec. 6.6.4.1, Sec. 6.6.5]; [H&S, Sec. 5.3.2 p. 122]

5 (null) 23 5.4 NULL,, 0 NULL ( <stdio.h> ), 0 ((void *)0) ( 5.6) 0 0 NULL NULL ; NULL 0, 0,, NULL ( 0 ) 5.2 0 NULL ( NULL 0 ) NULL ; 5.7 [K&R1, Sec. 5.4 pp. 97-8]; [K&R2, Sec. 5.4 p. 102]; [ISO, Sec. 7.1.6, Sec. 6.2.2.3]; [Rationale, Sec. 4.1.5]; [H&S, Sec. 5.3.2 p. 122, Sec. 11.1 p. 292] 5.5, NULL : 0 ( 0; 5.4), 0 NULL,, 0 NULL 0 0 5.2 5.8 5.14 [ISO, Sec. 7.1.6]; [Rationale, Sec. 4.1.5] 5.6 NULL #define NULL ((char *)0) NULL,, NULL, ( ), FILE *fp = NULL;, ANSI C NULL #define NULL ((void *)0) (, ), NULL (, ASCII NUL ; 5.7), ANSI ( ; 5.2), [Rationale, Sec. 4.1.5]

5 (null) 24 5.7 NULL 0, NULL, 0,, 0 ( 9.2 17.8) C, NULL 0, 0 NULL ( 0 ), ; ( ) 0 0 0, NULL, (, ANSI NULL ((void *)0),, ASCII (NUL) NULL, #define NUL \0 [K&R1, Sec. 5.4 pp. 97-8]; [K&R2, Sec. 5.4 p. 102] 5.8 NULL,, NULL ( 0) ( NULL, ), NULL 0 0 ( ) NULL 5.5 9.2 5.9 #define Nullptr(type) (type *)0,, ; 5.2 9.1 10.1 5.10 NULL 0, (null) null NULL, : 1., 5.1 2. ( ),, C,

5 (null) 25 3., 0 ( 5.2) 4. NULL, 0 ( 5.4) 5. ASCII (NUL),,, ; 6. (null string), ("") C, ( \0 ),, ( null pointer, ), 0 3, NULL 4 5.11 C ( ) (NULL) if(p == 0) p, 0, ( 5.10 ) C ( Pascal, nil ) nil,, C nil 0,, 0, 0, 0, 5.12 : 1., 0 NULL 2. 0 NULL,, ( ), (, 2 ; ) 5.1 5.2 5.4, 5.3 5.7 5.10 5.11

5 (null) 26 5.13, 0,,,,, ( calloc() ; 7.26), int ( 5.11, nil, 0 ) 5.14,, PL/I, Prime 50 07777, 0 0, 0 C, TCNP ( C ) C Prime (char *) (int *) Data General Eclipse MV ( ), C char * void *, Honeywell-Bull 06000 ( ) CDC Cyber 180 (ring), 48 ( 11 ) 0xB00000000000 1 CDC 1,, HP 3000 ;, char * void * Symbolics Lisp, ; <NIL, 0> ( <, > ) C, 8086 (PC ) 16 32, 64 Cray 48 int *; char * 16 [K&R1, Sec. A14.4 p. 211] 5.15, MS-DOS, ( 0 ) 16.7

6 6.1 char a[6], extern char *a, extern char * T T extern char a[ ] 64-5] [ISO, Sec. 6.5.4.2]; [CT&P, Sec. 3.3 pp. 33-4, Sec. 4.5 pp. 6.2 char a[ ] char *a ( 6.4) char a[6] 6, a, a, 6 char *p,, p :, ( 5.1 1.10) char a[] = "hello"; char *p = "world"; : +---+---+---+---+---+---+ a: h e l l o \0 +---+---+---+---+---+---+ +-----+ +---+---+---+---+---+---+ p: *======> w o r l d \0 +-----+ +---+---+---+---+---+---+ x, x[3], a[3], a 3,. p[3], p,, 3, a[3] a ( ) 3, p[3] p 3., a[3] p[3] l, a p 27

6 28, 1.13 [K&R2, Sec. 5.5 p. 104]; [CT&P, Sec. 4.5 pp. 64-5] 6.3, C C,, : T ( ); T,,, &a[0], sizeof &,, a[i],, p[i], 6.2, : p = a; p[3] a[3] 6.6 6.11 [K&R1, Sec. 5.3 pp. 93-6]; [K&R2, Sec. 5.3 p. 99]; [ISO, Sec. 6.2.2.1, Sec. 6.3.2.1, Sec. 6.3.6]; [H&S, Sec. 5.4.1 p. 124] 6.4,,,, void f(char a[]) {... },,. void f(char *a) {... },, ;,

6 29 6.18 [K&R1, Sec. 5.3 p. 95, Sec. A10.1 p. 205]; [K&R2, Sec. 5.3 p. 100, Sec. A8.6.3 p. 218, Sec. A10.1 p. 226]; [ISO, Sec. 6.5.4.3, Sec. 6.7.1, Sec. 6.9.6]; [H&S, Sec. 9.3 p. 271]; [CT&P, Sec. 3.3 pp. 33-4] 6.5, 179] ANSI C, : [ISO, Sec. 6.2.2.1]; [Rationale, Sec. 3.2.2.1]; [H&S, Sec. 7.1 p. 6.6,, ( malloc), (, ),, ( 6.3),, malloc ( [ ] ) 6.11 6.13, sizeof 1.13 20.12 6.7,,, 6.2 6.3 6.6 6.8, 5["abcdef"] C, 1, C, a e,, a[e] *((a)+(e)) C, C, : [Rationale, Sec. 3.3.2.1]; [H&S, Sec. 5.4.1 p. 124, Sec. 7.4.1 pp. 186-7] 1, 1897, (Virginia),,, Yes, Virginia, there is a Santa Claus, http://www.hymnsandcarolsofchristmas.com/ santa/virginia s question.htm

6 30 6.9, arr, arr &arr C, &arr T, ANSI C, &arr &, C, ( & ) T, 6.3, 6.10 6.15 : [ISO, Sec. 6.2.2.1, Sec. 6.3.3.2]; [Rationale, Sec. 3.3.3.2]; [H&S, Sec. 7.5.6 p. 198] 6.10,,, T T ( 6.3), ;,,, 6.15, int (*ap)[n]; N ( 1.7), N,,, 6.9 : [ISO, Sec. 6.2.2.1] 6.11 ( 6.3), malloc #include <stdlib.h> int *dynarray; dynarray = malloc(10 * sizeof(int)); ( malloc ), dynarry[i] (i 0 9) sizeof 1.12 6.13 7.9 6.12, ; C, C99 (VLA), ;

6 31, ( gcc ) C99 gcc, malloc(), free() 6.11, 6.13, 6.16, 7.19 7.27 : [ISO, Sec. 6.4, Sec. 6.5.4.2]; [C9X, Sec. 6.5.5.2] 6.13, : #include <stdlib.h> int **array1 = malloc(nrows * sizeof(int *)); for(i = 0; i < nrows; i++) array1[i] = malloc(ncolumns * sizeof(int));,, malloc sizeof(*array1) sizeof(**array1) sizeof(int *) sizeof(int),, : int **array2 = malloc(nrows * sizeof(int *)); array2[0] = malloc(nrows * ncolumns * sizeof(int)); for(i = 1; i < nrows; i++) array2[i] = array2[0] + i * ncolumns;, arrayx[i][j] (for 0 <= i <nrows 0 <= j <ncolumns), : int *array3 = malloc(nrows * ncolumns * sizeof(int));,, array3[i * ncolumns + j] i, j,,, 6.16 : int (*array4)[ncolumns] = malloc(nrows * sizeof(*array4));,, ( ; 7.20) 6.17 6.15, C99 : [C9X, Sec. 6.5.5.2]

6 32 6.14 : int realarray[10]; int *array = &realarray[-1]; array 1 ( Numerical Recipes in C ), C, ;,, ( ), : [K&R2, Sec. 5.3 p. 100, Sec. 5.4 pp. 102-3, Sec. A7.7 pp. 205-6]; [ISO, Sec. 6.3.6]; [Rationale, Sec. 3.2.2.3] 6.15, ( 6.3) ( C ),, ; 6.10 : int array[nrows][ncolumns]; f(array); : void f(int a[][ncolumns]) {... } void f(int (*ap)[ncolumns]) /* ap */ {... }, ( 6.3 6.4);,, NROWS, NCOLUMNS (, ), 6.9 6.12 : [K&R1, Sec. 5.10 p. 110]; [K&R2, Sec. 5.9 p. 113]; [H&S, Sec. 5.4.3 p. 126] 6.16 [0][0],

6 33 void f2(int *aryp, int nrows, int ncolumns) {... array[i][j] is accessed as aryp[i * ncolumns + j]... } 6.15 : f2(&array[0][0], NROWS, NCOLUMNS);,, ANSI C ;, x >= NCOLUMNS, &array[0][0][x] C99, C99, VLA gcc, 6.13 6.15, 6.17, 6.12 : [ISO, Sec. 6.3.6]; [C9X, Sec. 6.5.5.2] 6.17 int array[nrows][ncolumns]; int **array1; /* */ int **array2; /* */ int *array3; /* " " */ int (*array4)[ncolumns]; 6.13, void f1a(int a[][ncolumns], int nrows, int ncolumns); void f1b(int (*a)[ncolumns], int nrows, int ncolumns); void f2(int *aryp, int nrows, int ncolumns); void f3(int **pp, int nrows, int ncolumns); f1a() f1b(), f2(), f3() ( 6.15 6.16), : f1a(array, NROWS, NCOLUMNS); f1b(array, NROWS, NCOLUMNS); f1a(array4, nrows, NCOLUMNS); f1b(array4, nrows, NCOLUMNS); f2(&array[0][0], NROWS, NCOLUMNS); f2(*array, NROWS, NCOLUMNS); f2(*array2, nrows, ncolumns); f2(array3, nrows, ncolumns); f2(*array4, nrows, NCOLUMNS); f3(array1, nrows, ncolumns); f3(array2, nrows, ncolumns);,, ncolumns NCOLUMNS :

6 34 f1a((int (*)[NCOLUMNS])(*array2), nrows, ncolumns); f1a((int (*)[NCOLUMNS])(*array2), nrows, ncolumns); f1b((int (*)[NCOLUMNS])array3, nrows, ncolumns); f1b((int (*)[NCOLUMNS])array3, nrows, ncolumns); f2() &array[0][0] ( *array) ; 6.16,, C,, 6.13 6.13 array1 array2 f3() 6.18, sizeof ( 6.4), : [H&S, Sec. 7.5.2 p. 195]

7 7.1 char *answer; printf("type something:\n"); gets(answer); printf("you typed \"%s\"\n", answer); answer, gets(),,, answer,, answer 1.10 5.1 :,, #include <stdio.h> #include <string.h> char answer[100], *p; printf("type something:\n"); fgets(answer, sizeof answer, stdin); if((p = strchr(answer, \n ))!= NULL) *p = \0 ; printf("you typed \"%s\"\n", answer); fgets() gets(), array 12.20, fgets() gets() \n malloc() answer 7.2 strcat(). char *s1 = "Hello, "; char *s2 = "world!"; char *s3 = strcat(s1, s2); 7.1, C C (, ), malloc() strcat() ;, : char s1[20] = "Hello, "; 35

7 36 strcat(), s1, s3 ; strcat(), s1 strcat() : s1,, 1.13 : [CT&P, Sec. 3.2 p. 32] 7.3 strcat char * ( ),,,, Unix ANSI C, ( ) (, 6.3 6.4) time() ( 13.10) stat() 7.4 char *p; strcpy(p, "abc");, p, 11.35 7.5, char *p; (,, ) ;,, sizeof(char*) 7.1 7.2 7.6,,,, : char *itoa(int n) { char retbuf[20]; /* */ sprintf(retbuf, "%d", n); return retbuf; /* */ }

7 37 static char retbuf[20];,, 7.7, 12.19 20.1 : [ISO, Sec. 6.1.2.4] 7.7 ( 7.6 ),, malloc(), ( ) 20.1 7.8 malloc(), : <stdlib.h> malloc() 1.8 : [H&S, Sec. 4.7 p. 101] 7.9 malloc ANSI/ISO C void *, ( ) ANSI/ISO C,,, malloc() ; 7.8 (,, C++, C C++ void * ) : [H&S, Sec. 16.1 pp. 386-7] 7.10 malloc(), void * int * C++ C 7.9 7.11 char *p = malloc(strlen(s) + 1); strcpy(p, s); malloc((strlen(s) + 1) * sizeof(char)) sizeof(char),, sizeof(char) 1, sizeof(char), size t 8.5 : [ISO, Sec. 6.3.3.4]; [H&S, Sec. 7.5.2 p. 195]

7 38 7.12 6.11 6.13 7.13 malloc, : [ISO, Sec. 7.10.3] 7.14, : double *array = malloc(300 * 300 * sizeof( double )); malloc() null,,, malloc(), 300 * 300 90,000, sizeof(double) 16 int, size t (malloc() ) 32, int 16, 300 * (300 * sizeof(double)) ( 3.11),, 32, 19.27 7.15 PC 8 640K PC, 640K, MS-DOS 19.27 7.16, malloc malloc() bug, malloc, malloc ; bug malloc(strlen(s)) strlen(s) + 1, malloc,,, 7.25 7.23, 16.7 18.2 7.17, malloc(), C

7 39, : struct list *listp, *nextp; for(listp = base; listp!= NULL; listp = nextp) { nextp = listp->next; free(listp); } nextp, listp = listp-> next : [K&R2, Sec. 7.8.5 p. 167]; [ISO, Sec. 7.10.3]; [Rationale, Sec. 4.10.3.2]; [H&S, Sec. 16.2 p. 387]; [CT&P, Sec. 7.10 p. 95] 7.18 free() (, ) free(),,, C 4.4,,,,,, : [ISO, Sec. 7.10.3]; [Rationale, Sec. 3.2.2.3] 7.19 malloc(), free(),,, malloc(), malloc() free() 7.20,,, free() malloc(), ( ) malloc(), free() malloc() 7.21

7 40 7.21,, ANSI/ISO C : [ISO, Sec. 7.10.3.2] 7.22,, malloc/free, malloc() 7.23 free() malloc/free, 7.24 malloc,, 7.25 realloc() ANSI C, realloc(..., 0),,, realloc() : [ISO, Sec. 7.10.3.4]; [H&S, Sec. 16.3 p. 388] 7.26 calloc() malloc() calloc free() calloc(), cfree() calloc(m, n) p = malloc(m * n); memset(p, 0, m * n);, ( 5 ) free() calloc() : [ISO, Sec. 7.10.3 to 7.10.3.2]; [H&S, Sec. 16.1 p. 386, Sec. 16.2 p. 386]; [PCS, Sec. 11 pp. 141,142]

7 41 7.27 alloca() alloca(),, alloca alloca(),, fgets(alloca(100), 100, stdin), alloca(),, C99 (VLA), alloca() 7.19 : [Rationale, Sec. 4.10.3]

7 42

8 8.1 strcat(string,! );, strcat() C, 8.4 ;!, strcat(string, "!"); 1.13, 7.2 16.6 : [CT&P, Sec. 1.5 pp. 9-10] 8.2 char *string;... if(string == "value") { /* string matches value */... } C, C (, ) == string value,,,, strcmp(): if(strcmp(string, "value") == 0) { /* string matches "value" */ } 8.3 char a[] = "Hello, world!"; char a[14]; a = "Hello, world!";, strcpy() : strcpy(a, "Hello, world!"); 1.13, 4.1 7.2 43

8 44 8.4 ( ), C, :, 0-9, 0,, 0 13.1 20.8 8.5 : sizeof( a ) 2 1 (, sizeof(char)), C int, sizeof( a ) sizeof(int), C++ 7.11 : [ISO, Sec. 6.1.3.4]; [H&S, Sec. 2.7.3 p. 29]

9 9.1 C #define enum true false C, / ( int, char, int, ) #define true/false, ( 2.16 17.8) #define TRUE 1 #define YES 1 #define FALSE 0 #define NO 0 enum bool {false, true}; enum bool {no, yes}; 1 0,, #define TRUE (1==1) #define FALSE (!TRUE) #define Istrue(e) ((e)!= 0), 9.2, 5.9 10.1 9.2 C, TRUE 1 1 C,,,, 1 0, if((a == b) == TRUE) ( TRUE 1),, TRUE FALSE TRUE FALSE, ( isupper(), isalpha() ), 1 (, if((a == b) 45

9 46 == TRUE) if(a == b), if(((a == b) == TRUE) == TRUE)?) TRUE FALSE ( ),, TRUE FALSE ( NULL), ( 5.3 5.8 ) TRUE FALSE ( YES NO), C TRUE FALSE, 1 0 5.7 : [K&R1, Sec. 2.6 p. 39, Sec. 2.7 p. 41]; [K&R2, Sec. 2.6 p. 42, Sec. 2.7 p. 44, Sec. A7.4.7 p. 204, Sec. A7.9 p. 206]; [ISO, Sec. 6.3.3.3, Sec. 6.3.8, Sec. 6.3.9, Sec. 6.3.13, Sec. 6.3.14, Sec. 6.3.15, Sec. 6.6.4.1, Sec. 6.6.5]; [H&S, Sec. 7.5.4 pp. 196-7, Sec. 7.6.4 pp. 207-8, Sec. 7.6.5 pp. 208-9, Sec. 7.7 pp. 217-8, Sec. 7.8 pp. 218-9, Sec. 8.5 pp. 238-9, Sec. 8.6 pp. 241-4]; What the Tortoise Said to Achilles 9.3 p, if(p). 5.3

10 C 10.1 : #define begin { #define end } 17 10.2,,, ( 3.4 20.14 ) ( ),, ( ), C typeof, 10.3,,, (, ), if/else if else, : #define MACRO(arg1, arg2) do { \ /* declarations */ \ stmt1; \ stmt2; \ /*... */ \ } while(0) /* ; */, 0, lint,,,,, 10.22 DEBUG() : [H&S, Sec. 3.3.2 p. 45]; [CT&P, Sec. 6.3 pp. 82-3] 47

10 C 48 10.4,.c,.h (.h ), (.h) : ( #defines) typedef ( 1.4),,.c 1.3,.c,.c 1.3 10.5 : [K&R2, Sec. 4.5 pp. 81-2]; [H&S, Sec. 9.2.3 p. 267]; [CT&P, Sec. 4.6 pp. 66-7] 10.5, : (Indian Hill Style Guide, 17.7) ; ;, ; makefile, (, ); grep ( tags ), ; : #ifndef HFILENAME_USED #define HFILENAME_USED...... #endif ; Makefile (, ) 17.8 : [Rationale, Sec. 4.1.2] 10.6 #include <> #include "" <>, ""

10 C 49 10.7 ; 11.32, <> "", ( ) : [K&R2, Sec. A12.4 p. 231]; [ISO, Sec. 6.8.2]; [H&S, Sec. 3.4 p. 55] 10.8, 2.14, 11.28 16.1 10.9, 13.18 10.10,,,,, <stdlib.h> <malloc.h>,,,,. ( 19 ), 18.18 10.11 #if ; #if, 20.15 : [K&R2, Sec. 4.11.3 p. 91; ISO Sec. 6.8.1]; [H&S, Sec. 7.11.1 p. 225]

10 C 50 10.12 sizeof #if,, ANSI <limits.h>, (configure), ; 1.1 10.13 #include #ifdef #ifdef #define : [ISO, Sec. 6.8.3, Sec. 6.8.3.4]; [H&S, Sec. 3.2 pp. 40-1] 10.14 typdef #ifdef, ( MY TYPE DEFINED) typdef 10.12 : [ISO, Sec. 5.1.1.2, Sec. 6.8.1]; [H&S, Sec. 7.11.1 p. 225] 10.15 #if (, ) : [ISO, Sec. 6.8.1]; [H&S, Sec. 7.11.1 p. 225] 10.16, #ifdef #include #ifdef, unifdef, rmifdef scpp (selective C preprocessor) 18.18 10.17, gcc -E -dm,, Unix, ( unix ) ( ),

10 C 51 10.18, #define Paste(a, b) a/**/b ( Reiser),, ANSI ( K&R ), ANSI ##, 52] #define Paste(a, b) a##b 11.18 : [ISO, Sec. 6.8.3.3]; [Rationale, Sec. 3.8.3.3]; [H&S, Sec. 3.3.9 p. 10.19 #define TRACE(n) printf("trace: %d\n", n) TRACE(count); printf("trace: %d\count", count); 11.19 10.20 #, 11.18 11.19 10.21, C,, make(1) C, Unix m4 10.22, printf() #define DEBUG(args) (printf("debug: "), printf args) if(n!= 0) DEBUG(("n is %d\n", n)); gcc (DEBUG1, DEBUG2, ), :

10 C 52 #define DEBUG(args) (printf("debug: "), printf(args)) #define _, DEBUG("i = %d" _ i); C99... ( ), VA ARGS,, 15.4 15.5,, #define printf myprintf : [C9X, Sec. 6.8.3, Sec. 6.8.3.1]

11 ANSI/ISO C 11.1 ANSI C 1983, (ANSI) X3J11 C, 1989 12 14 ANSX3.159-1989 1990 ANSI C, C++ ( ) ( ) ANSI C C,, ISO/IEC 9899:1990, ( ANSI/ISO 9899-1990 [1992]) X3.159 ISO, 1994, 1 (TC1) 40,, 1 (NA1) 50, 1995, TC2,, C99,, C99 ANSI, (Rational),, 11.2 18 www.ansi.org (PDF) American National Standards Institute 11 W. 42nd St., 13th floor New York, NY 10036 USA (+1) 212 642 4900 Global Engineering Documents 15 Inverness Way E Englewood, CO 80112 USA (+1) 303 397 2715 (800) 854 7179 (U.S. & Canada),, Geneva ISO, : 53

11 ANSI/ISO C 54 ISO Sales Case Postale 56 CH-1211 Geneve 20 Switzerland URL http://www.iso.ch comp.std.internat FAQ, Standards.Faq Herbert Schild ANSI C ISO 9899 ; Osborne/McGraw-Hill, ISBN 0-07-881952-0, 40 :, http://www.lysator.liu.se/c/schildt.html Clive Feather ( ) ANSI ftp://ftp.uu.net/doc/standards/ansi/ X3.159-1989 ftp ( 18.18), http://www. lysator.liu.se/c/rat/title.html Silicon Press, ISBN 0-929306-07-4 C9X ISO/IEC JTC1/SC22/WG14, http://www.dkuug.dk/jtc1/sc22/wg14/ 11.3 11.3 ( C9X ): http://www.lysator. liu.se/c/index.html, http://www.dkuug.dk/jtc1/sc22/wg14/ http://www. dmk.com/ 11.4 ANSI extern int func(float); int func(x) float x; {... extern int func(float); int func(x) float x;, C ( ANSI C),,, : int func(float x) {... } extern int func(double);

11 ANSI/ISO C 55,,, (char, short int float) 1.8 : [K&R1, Sec. A7.1 p. 186]; [K&R2, Sec. A7.3.2 p. 202]; [ISO, Sec. 6.3.2.2, Sec. 6.5.4.3]; [Rationale, Sec. 3.3.2.2, Sec. 3.5.4.3]; [H&S, Sec. 9.2 pp. 265-7, Sec. 9.4 pp. 272-3] 11.5,, 11.4,, : [ISO, Sec. 6.7.1, Sec. 6.9.5]; [H&S, Sec. 9.2.2 pp. 265-7, Sec. 9.2.5 pp. 269-70] 11.6 extern int f(struct x *p); x C, ( ),, : struct x; x,, x x : [ISO, Sec. 6.1.2.1, Sec. 6.1.2.6, Sec. 6.5.2.3] 11.7 : const int n = 5; int a[n]; const ; ( ) const C C++, #define ( enum) : [ISO, Sec. 6.4]; [H&S, Secs. 7.11.2,7.11.3 pp. 226-7] 11.8,

11 ANSI/ISO C 56 char *p = "Hello, world!"; p 1.13 11.9 const char *p char * const p const char *p ( char const *p ), ; char * const p ( ),, ; 1.7 : [ISO, Sec. 6.5.4.1]; [Rationale, Sec. 3.5.4.1]; [H&S, Sec. 4.4.4 p. 81] 11.10 const char ** char ** const-t T ( T ), ( ),, (, (const char **)),, : [ISO, Sec. 6.1.2.6, Sec. 6.3.16.1, Sec. 6.5.3]; [H&S, Sec. 7.9.1 pp. 221-2] 11.11 main() int main(), int main(void) int main(int argc, char *argv[]) ( argc argv ) 11.12 11.16 : [ISO, Sec. 5.1.2.2.1, Sec. G.5.1]; [H&S, Sec. 20.1 p. 416]; [CT&P, Sec. 3.10 pp. 50-51] 11.12 main() void, main main() int, exit(), return (, void main(), C ) /

11 ANSI/ISO C 57, main() ;, main(),,, : [ISO, 5.1.2.2.1, Sec. G.5.1]; [H&S, Sec. 20.1 p. 416]; [CT&P, Sec. 3.10 pp. 50-51] 11.13 main() envp ( ) getenv(), environ ( ) : [ISO, Sec. G.5.1]; [H&S, Sec. 20.1 pp. 416-7] 11.14 main() void, exit() return, / main(), ; main(), ( ) ( ; 11.12), void main(), 11.15 void main() BC++4.5 void main() ( DEC C V4.1 gcc) void main() 11.16 C void main() void main(), 11.17 main(), exit(status) status main(), main() return ; 16.4, main(),

11 ANSI/ISO C 58 : [K&R2, Sec. 7.6 pp. 163-4]; [ISO, Sec. 5.1.2.2.3] 11.18 ANSI #, #define Str(x) #x #define Xstr(x) Str(x) #define OP plus char *opname = Xstr(OP); opname plus OP ## ( ) : [ISO, Sec. 6.8.3.2, Sec. 6.8.3.5] 11.19 warning: macro replacement within a string literal ANSI / #define TRACE(var, fmt) printf("trace: var = fmt\n", var) TRACE(i, %d); printf("trace: i = %d\n", i);, K&R C, # (ANSI ): #define TRACE(var, fmt) \ printf("trace: " #var " = " #fmt "\n", var) 11.18 : [H&S, Sec. 3.3.8 p. 51] 11.20 #ifdef ANSI C, #if, #ifdef #ifndef " C,, /* */ 20.18 10.21 : [ISO, Sec. 5.1.1.2, Sec. 6.1]; [H&S, Sec. 3.2 p. 40]

11 ANSI/ISO C 59 11.21 #pragma, #pragam, ( ) : ( lint /* NOTREACHED */ ), : [ISO, Sec. 6.8.6]; [H&S, Sec. 3.7 p. 61] 11.22 #pragma once ; 10.5 #ifndef, 11.23 a[3] = "abc";, ANSI C ( ANSI ) 3, a, b c, \0 C strcpy, printf %s,, abc, 4 : [ISO, Sec. 6.5.7]; [H&S, Sec. 4.6.4 p. 98] 11.24 void*, char *, 4.3 : [ISO, Sec. 6.1.2.5, Sec. 6.3.6]; [H&S, Sec. 7.6.2 p. 204] 11.25 memcpy() memmove(), memmove() memcpy(),, memmove() : [K&R2, Sec. B3 p. 250]; [ISO, Sec. 7.11.2.1, Sec. 7.11.2.2]; [Rationale, Sec. 4.11.2]; [H&S, Sec. 14.3 pp. 341-2]; [PCS, Sec. 11 pp. 165-6] 11.26 malloc(0) 0 ANSI/ISO ; : [ISO, Sec. 7.10.3]; [PCS, Sec. 16.1 p. 386]

11 ANSI/ISO C 60 11.27 ANSI ANSI/ISO C ANSI 6, C99 31 : [ISO, Sec. 6.1.2, Sec. 6.9.1]; [Rationale, Sec. 3.1.2]; [C9X, Sec. 6.1.2]; [H&S, Sec. 2.5 pp. 22-3] 11.28 ANSI, 1.11, 10.8, 11.29 16.1 11.29 ASNI/ISO ANSI ANSI, ANSI,, gcc 11.28, 12.22 13.19 11.30 C ANSI C, protoize unprotoize C ANSI C FSF GNU C ; 18.3 GNU GhostScript ansi2knr ANSI C, ANSI C K&R C ; 11.4 11.28, lint 1992 3 comp. sources.misc CPROTO cextract 18.18 ; 11.4 11.31 ANSI Frobozz Magic C ANSI, gcc, gcc ;, 11.35

11 ANSI/ISO C 61 11.32 (implementation-defin-ed) (unspecified) (undefined) : ;, ( ),, ; 3.2,,, 3.8 11.34 (locale-specific) : [ISO, Sec. 3.10, Sec. 3.16, Sec. 3.17]; [Rationale, Sec. 1.6] 11.33,, :, : [ISO, Sec.]; [Rationale, Sec. 1.7] 11.34, ANSI, C,,,,,, 11.35 : [Rationale, Sec. 1.1]

11 ANSI/ISO C 62 11.35 i = i++, ANSI,,,, 7.4, 11.31, 11.32 11.34

12 12.1 char c; while((c = getchar())!= EOF)..., getchar int getchar(), EOF getchar char, EOF, EOF ( char ), : [K&R1, Sec. 1.5 p. 14]; [K&R2, Sec. 1.5.1 p. 16]; [ISO, Sec. 6.1.2.5, Sec. 7.9.1, Sec. 7.9.7.5]; [H&S, Sec. 5.1.3 p. 116, Sec. 15.1, Sec. 15.6]; [CT&P, Sec. 5.1 p. 70]; [PCS, Sec. 11 p. 157] 12.2 EOF, EOF, C EOF,, Control-D Control-Z 12.3 while(!feof(infp)) { fgets(buf, MAXLINE, infp); fputs(buf, outfp); } C,, C I/O Pascal,, fgets() NULL,, feof() : [K&R2, Sec. 7.6 p. 164]; [ISO, Sec. 7.9.3, Sec. 7.9.7.1, Sec. 7.9.10.2]; [H&S, Sec. 15.14 p. 382] 12.4, fflush(stdout), 63

12 64 \n fflush, stdout 12.21 : [ISO, Sec. 7.9.5.2] 12.5 19.1 12.6 printf % \%, : %% \%, \, printf 19.21 : [K&R1, Sec. 7.3 p. 147]; [K&R2, Sec. 7.2 p. 154]; [ISO, Sec. 7.9.6.1] 12.7 printf %lf, scanf() %lf, printf() %f printf %f, printf,, printf() printf() %Lf, 12.11 15.2 : [K&R1, Sec. 7.3 pp. 145-47, Sec. 7.4 pp. 147-50]; [K&R2, Sec. 7.2 pp. 153-44, Sec. 7.4 pp. 157-59]; [ISO, Sec. 7.9.6.1, Sec. 7.9.6.2]; [H&S, Sec. 15.8 pp. 357-64, Sec. 15.11 pp. 366-78]; [CT&P, Sec. A.1 pp. 121-33] 12.8 size t, long, printf, printf,, printf("%lu", (unsigned long)sizeof(thetype)); 12.9 printf, %8d printf("%*d", width, x) 12.14

12 65 : [K&R1, Sec. 7.3]; [K&R2, Sec. 7.2]; [ISO, Sec. 7.9.6.1]; [H&S, Sec. 15.11.6]; [CT&P, Sec. A.1] 12.10 <locale.h>, printf() locale : [ISO, Sec. 7.4]; [H&S, Sec. 11.6 pp. 301-4] 12.11 scanf("%d", i) scanf() scanf("%d", &i) 12.12 char s[30]; scanf("%s", s); & ; & scanf(), &, &, 6.3 6.4 12.13 double d; scanf("%f", &d); 12.7 printf(), scanf() %lf, %f 12.14 scanf() : scanf() ANSI, scanf 12.15 %d\n scanf,, \n scanf, 12.18 : [K&R2, Sec. B1.3 pp. 245-6]; [ISO, Sec. 7.9.6.2]; [H&S, Sec. 15.8 pp. 357-64]

12 66 12.16 scanf %d, gets(), gets() scanf %d, gets(), scanf() gets(), ; scanf scanf(), 12.18 12.20 : [ISO, Sec. 7.9.6.2]; [H&S, Sec. 15.8 pp. 357-64] 12.17, scanf(), scanf(),,, scanf(): scanf() scanf %d %f x, scanf() x 12.18 : [ISO, Sec. 7.9.6.2]; [H&S, Sec. 15.8 pp. 357-64] 12.18 scanf() scanf() 12.15, 12.16 12.17, %s gets() ( 12.20), scanf(),, scan formatted,,,, scanf() ; fgets(), sscanf() strtol(), strtok() atoi() ; 13.4 scanf,, %s, : [K&R2, Sec. 7.4 p. 159] 12.19 sprintf sprintf() sprintf(), %s, strlen(), %d

12 67 ((sizeof(int) * CHAR_BIT + 2) / 3 + 1) /* +1 for - */ CHAR BIT <limits.h>, ;, sprintf,, fprintf(), fprintf, 19.14,, sprintf(),, %.Ns %s, %.*s, 12.9, sprintf(), snprintf() snprintf(buf, bufsize, "You typed \"%s\"", answer); snprintf() stdio, GNU 4.4bsd C99, C99 snprintf() sprintf() C99 snprintf(), 0 nch = snprintf(null, 0, fmtstring, /* */ ); ( ) asprintf(), bsd GNU C, malloc, : char *buf; asprintf(&buf, "%d = %s", 42, "forty-two"); /*, buf malloc */ : [C9X, Sec. 7.13.6.6] 12.20 gets() fgets(), gets(), fgets() gets(),,, 7.1 fgets() gets() : [Rationale, Sec. 4.9.7.2]; [H&S, Sec. 15.7 p. 356] 12.21 printf() errno ENOTTY stdout, stdio, stdout

12 68, errno ENOTTY, errno errno 0 : [ISO, Sec. 7.1.4, Sec. 7.9.10.3]; [CT&P, Sec. 5.4 p. 73]; [PCS, Sec. 14 p. 254] 12.22 fgetops/fsetops ftell/fseek fgetops() fsetops() ftell() fseek() ( ),, 20 (2 31 1) fgetpos() fsetpos() fpos t,, fgetpos() fsetpos fgetpos() gsetpos() 1.2 : [K&R2, Sec. B1.6 p. 248]; [ISO, Sec. 7.9.1, Secs. 7.9.9.1,7.9.9.3]; [H&S, Sec. 15.5 p. 252] 12.23, fflush(stdin) fflush() flush, fflush : [ISO, Sec. 7.9.5.2]; [H&S, Sec. 15.2] 12.24 fflush(), scanf() ( 12.16-12.17), scanf(), 12.18 while((c = getchar())!= \n && c!= EOF) /* */ ; curses flushinp(),,, ( ), ; 19.1 19.2 : [ISO, Sec. 7.9.5.2]; [H&S, Sec. 15.2] 12.25 fopen() 19.21 19.22

12 69 12.26 r+,,, fseek,, + fseek fflush,,, 19.17 : [ISO, Sec. 7.9.5.3] 12.27 stdin stdout freopen(), 12.28 12.28 freopen(), stdout ( stdin), freopen() ( ), ( ), freopen(), dup(), dup2() FILE, 12.29, printf, : #include <stdio.h> #include <stdarg.h> void f2printf(file *fp1, FILE *fp2, char *fmt,...) { va_list argp; va_start(argp, fmt); vfprintf(fp1, fmt, argp); va_end(argp); va_start(argp, fmt); vfprintf(fp2, fmt, argp); va_end(argp); } f2printf() fprintf(), 15.5

12 70 12.30 0x0a 0x0d, 0x1a, EOF rb fopen(),,, wb / :, I/O : [ISO, Sec. 7.9.5.3]; [H&S, Sec. 15.2.1 p. 348]

13 13.1 ( atoi ) itoa() sprintf() sprintf(), ; 7.6 ; 8.4 12.19 sprintf() ( %ld %f) : [K&R1, Sec. 3.6 p. 60]; [K&R2, Sec. 3.6 p. 64] 13.2 strncpy() \0 strncpy(), \0 strncpy \0, strncpy(), \0 strncat strncpy : (, *dest = \0 ), strncat() strncpy() sprintf(dest, "%.*s", n, source) ( ), memcpy() strncpy() 13.3 toupper() toupper() tolower() toupper() tolower(), ANSI/ISO C, : [ISO, Sec. 7.3.2]; [H&S, Sec. 12.9 pp. 320-1]; [PCS, p. 182] 71

13 72 13.4 main() argc argv strtok(),,, : [K&R2, Sec. B3 p. 250]; [ISO, Sec. 7.11.5.8]; [H&S, Sec. 13.7 pp. 333-4]; [PCS, p. 178] 13.5 Unix ed grep,,,, <regex.h> <regexp.h> regcmp/regex, regcomp/regexec, re comp/re exec regexp ftp://ftp.cs.toronto.edu/pub/regexp.shar.z Henry Spencer regexp, GNU rx 18.18 ( globbing ) Unix, shell,, MS-DOS, argv ( MS-DOS VMS) 19.25 20.2 13.6 strcmp(), qsort(), qsort,, strcmp(), strcmp() : /* */ int pstrcmp(const void *p1, const void *p2) { return strcmp(*(char * const *)p1, *(char * const *)p2); } const void *, ( ),, strcmp() char* qsort [K&R2] 5.11 119-20, : [ISO, Sec. 7.10.5.2]; [H&S, Sec. 20.5 p. 419]

13 73 13.7 qsort(), qsort(), (const void*), 13.6 : int mystructcmp(const void *p1, const void *p2) { const struct mystruct *sp1 = p1; const struct mystruct *sp2 = p2; /* sp1->whatever sp2->... */ mystruct sp1 = p1 sp2 = p2 ; p1 p2 void,,, 13.6, sp1 = *(struct mystruct * const *)p1,,, 4.5 : [ISO, Sec. 7.10.5.2]; [H&S, Sec. 20.5 p. 419] 13.8,, ( ),,, qsort(), : [Knuth, Sec. 5.2.1 pp. 80-102, Sec. 5.2.4 pp. 159-168]; [Sedgewick, Sec. 8 pp. 98-100, Sec. 12 pp. 163-175] 13.9, [Knuth],,,, : 19.30 19.31 : [Knuth, Sec. 5.4 pp. 247-378]; [Sedgewick, Sec. 13 pp. 177-187] 13.10 C time(), ctime(), localtime() / strftime() :

13 74 #include <stdio.h> #include <time.h> int main() { time_t now; time(&now); printf("it s %s", ctime(&now)); return 0; } strftime(), 19.36 : [K&R2, Sec. B10 pp. 255-7]; [ISO, Sec. 7.12]; [H&S, Sec. 18] 13.11 localtime() time t struct tm, ctime() time t, struct tm time t ANSI C mktime(), struct tm time t time t, strptime(), strftime() partime() ( RCS ) getdate() (, C ) 18.18 : [K&R2, Sec. B10 p. 256]; [ISO, Sec. 7.12.2.3]; [H&S, Sec. 18.4 pp. 401-2] 13.12 N ANSI/ISO C mktime() difftime() mktime(), struct tm, tm mday, mktime(), time t mktime() time t, difftime() time t, time t time t, 1970 2037 ; time t Unix Posix tm mday int, 32,736,, 24, 86400 Julian, Julian : Snippets ( 18.16); Simtel/Oakland ( JULCAL10.ZIP, 18.18) Date conversions [Burki]

13 75 13.11, 20.27 20.28 : [K&R2, Sec. B10 p. 256]; [ISO, Secs. 7.12.2.2,7.12.2.3]; [H&S, Secs. 18.4,18.5 pp. 401-2]; [Burki] 13.13 C : rand(),, ; sci.math.num-analysis FAQ : r250, RANLIB FSULTRA ( 18.18), Marsaglia, Matumoto Nishimura Mersenne Twister, Don Knuth : [K&R2, Sec. 2.7 p. 46, Sec. 7.8.7 p. 168]; [ISO, Sec. 7.10.2.1]; [H&S, Sec. 17.7 p. 393]; [PCS, Sec. 11 p. 172]; [Knuth, Vol. 2 Chap. 3 pp. 1-177]; [P&M] 13.14 rand() % N /* */ 0 N 1,, 13.16 : (int)((double)rand() / ((double)rand_max + 1) * N), : rand() / (RAND_MAX / N + 1) RAND MAX, N RAND MAX RAND MAX ANSI #define <stdlib.h>, RAND MAX, C rand() RAND MAX, rand() 0 1, 0 N 1, N : [K&R2, Sec. 7.8.7 p. 168]; [PCS, PCS Sec. 11 p. 172] 13.15, rand() srand(),, :

13 76 #include <stdlib.h> #include <time.h> srand((unsigned int)time((time_t *)NULL));,, time() time t,, : 19.36, srand() ; rand() srand() : [K&R2, Sec. 7.8.7 p. 168]; [ISO, Sec. 7.10.2.2]; [H&S, Sec. 17.7 p. 393] 13.16 /, rand() % 2, 0, 1, 0, 1, 0,, ; 13.14 : [Knuth, Sec. 3.2.1.1 pp. 12-14] 13.17 Marsaglia Knuth : #include <stdlib.h> #include <math.h> double gaussrand() { static double V1, V2, S; static int phase = 0; double X; if(phase == 0) { do { double U1 = (double)rand() / RAND_MAX; double U2 = (double)rand() / RAND_MAX; V1 = 2 * U1-1; V2 = 2 * U2-1; S = V1 * V1 + V2 * V2; } while(s >= 1 S == 0); X = V1 * sqrt(-2 * log(s) / S); } else X = V2 * sqrt(-2 * log(s) / S);

13 77 phase = 1 - phase; return X; }, 20.36 : [Knuth, Sec. 3.4.1 p. 117]; [Marsaglia&Bray]; [Press et al., Sec. 7.2 pp. 288-290] 13.18, #inlude,,,, #include 10.10, 11.29, 13.19, 14.3 19.39 13.19,, ( ) ;,, Unix, -l 13.20 13.20 end Unix, end,, end 13.18 13.19 13.21 printf, C printf(), printf()

13 78

14 14.1 float 3.1, printf 3.0999999, 0.1, 0.0001100110011... 3.1,,,. /, printf 14.6 14.2, #include <math.h>, double atof(), <stdlib.h> 14.3 : [CT&P, Sec. 4.5 pp. 65-6] 14.3, #include <math.h>, undefined: sin ( sin ) (math library), Unix Linux, bug, -lm 13.18, 13.19 14.2 14.4, 14.2.,,,,, ; 14.5 79

14 80 C, (CPU) ( 11.34),, : [K&P, Sec. 6 pp. 115-8]; [Knuth, Volume 2 chapter 4]; [Goldberg] 14.5, double a, b;... if (a == b) /* */ #include <math.h> if (fabs(a - b) <= epsilon * fabs(a)) epsilon a 0 : [Knuth, Sec. 4.2.2 pp. 217-8] 14.6 (int)(x + 0.5) (int)(x < 0? x - 0.5 : x + 0.5) 14.7 C C pow(), <math.h>, 14.8 <math.h> M PI, π π,, 4*atan(1.0) acos(-1.0) : [PCS, Sec. 13 p. 237]

14 81 14.9 IEEE NaN IEEE, <math.h>, <ieee.h> <nan.h>, isnan() NaN #define isnan(x) ((x)!= (x)) IEEE C99 isnan(), fpclassify(), sprintf(),, NaN Inf 19.38 : [C9X, Sec. 7.7.3] 14.10 C, C99 2.8, 14.11 : [C9X, Sec. 6.1.2.5, Sec. 7.8] 14.11 (FFT) ( ) Ajay Shah URL ftp://ftp.math.psu.edu/pub/faq/numcomp-free-c 18.8, 18.10. 18.16, 18.18 14.12 Turbo C, floating point formats not linked ( ), Turbo C ( Richie PDP-11 ), printf() scanf(), %e %f %g, Borland, ( sqrt(), ) comp.os.msdos.programmer FAQ

14 82

15 15.1 printf(), #include <stdio.h> printf(),,,,... : [ISO, Sec. 6.3.2.2, Sec. 7.1.7]; [Rationale, Sec. 3.3.2.2, Sec. 4.1.6]; [H&S, Sec. 9.2.4 pp. 268-9, Sec. 9.6 pp. 275-6] 15.2 %f printf(), float double : char short int int, float double,, 11.4 printf %f double, %c int, %hd 12.7, 12.13 : [ISO, Sec. 6.3.2.2]; [H&S, Sec. 6.3.5 p. 177, Sec. 9.4 pp. 272-3] 15.3 n long int, printf("%d", n); ANSI, 1 5.2, 11.4, 12.7 15.2 15.4 <stdarg.h> 1 : ( gcc),, (printf, scanf... ), gcc -Wall, : warning: int format, long int arg (arg 2) 83

15 84, malloc #include <stdlib.h> /* malloc, NULL, size_t */ #include <stdarg.h> /* va_ */ #include <string.h> /* strcat */ char *vstrcat(const char *first,...) { size_t len; char *retbuf; va_list argp; char *p; if(first == NULL) return NULL; len = strlen(first); va_start(argp, first); while((p = va_arg(argp, char *))!= NULL) len += strlen(p); va_end(argp); retbuf = malloc(len + 1); /* +1 \0 */ if(retbuf == NULL) return NULL; /* */ (void)strcpy(retbuf, first); va_start(argp, first); /* */ while((p = va_arg(argp, char *))!= NULL) (void)strcat(retbuf, p); va_end(argp); return retbuf; } : char *str = vstrcat("hello, ", "world!", (char *)NULL); ; 5.2, 15.3, malloc : [K&R2, Sec. 7.3 p. 155, Sec. B7 p. 254]; [ISO, Sec. 7.8]; [Rationale, Sec. 4.8]; [H&S, Sec. 11.4 pp. 296-9]; [CT&P, Sec. A.3 pp. 139-141]; [PCS, Sec. 11 pp. 184-5, Sec. 13 p. 242]

15 85 15.5 printf(), printf() vprintf(), vfprintf() vsprintf() error(),, error: #include <stdio.h> #include <stdarg.h> void error(const char *fmt,...) { va_list argp; fprintf(stderr, "error: "); va_start(argp, fmt); vfprintf(stderr, fmt, argp); va_end(argp); fprintf(stderr, "\n"); } : [K&R2, Sec. 8.3 p. 174, Sec. B1.2 p. 245]; [ISO, Secs. 7.9.6.7,7.9.6.8,7.9.6.9]; [H&S, Sec. 15.12 pp. 379-80]; [PCS, Sec. 11 pp. 186-7] 15.6 scanf(), scanf() C99 vscanf(), vfscanf() vsscanf(), C99 : [C9X, Secs. 7.3.6.12-14] 15.7 nargs(),, printf, %d,,, 0-1, 5.2 15.4 exec1() vstrcat(),, : [PCS, Sec. 11 pp. 167-8]

15 86 15.8 C, va start() 263] int f(...) {... } 15.9 : [ISO, Sec. 6.5.4, Sec. 6.5.4.3, Sec. 7.8.1.1]; [H&S, Sec. 9.2 p. 15.9 float, va arg(argp, float) : float ( ) double, char short int int va arg(arpg, float) va arg(arpg, double), va arg(argp, int) char, short int, va start() 11.4 15.2 : [ISO, Sec. 6.3.2.2]; [Rationale, Sec. 4.8.1.2]; [H&S, Sec. 11.4 p. 297] 15.10 va arg() va arg() typedef, 1.7 : [ISO, Sec. 7.8.1.2]; [Rationale, Sec. 4.8.1.2] 15.11,,,, va list vfprintf(), 15.5, va list, 15.12

15 87 15.12, (Steve Summit),, (void *),, main() argv 19.35

15 88

16 16.1,, #if/#ifdef/#ifndef/#else/#endif, 2.14, 10.8 11.28 16.2 myprocedure; /* */ C,, : myprocedure(); 16.3,, main() ( ),,,, malloc(), 1.11 11.12, 16.4, 16.5 18.4 16.4, main() main(), 2.14, 10.8, 11.12 11.14 setbuf() setvbuf() atexit() 7.6 11.17 : [CT&P, Sec. 5.3 pp. 72-3] 89

16 90 16.5,,, :, 7.1, 16,, a * b / c, 3.11, 3.1 3.5, int, 1.8, 11.4 15.1, 5 malloc/free : malloc malloc, 7.16 7.17, 16.7 printf(), %d long int, 12.7 unsigned int,, 7.14 19.27,, sprinf(), 7.1 12.19 typedef, size t, 14.1 14.4 lint 16.3, 16.4 18.4 16.6 : char *p = "hello, worl!"; p[0] = H ;, : char a[] = "hello, world!"; 1.13 : [ISO, Sec. 6.1.4]; [H&S, Sec. 2.7.4 pp. 31-2]

16 91 16.7 Segmentation violation, Bus error General protection fault,, : ( );, ( 5.2 5.15) ( 7.1 7.2); malloc ( 7.16);,, scanf() ( 12.11) fprintf() ( FILE *) 16.3 16.4

16 92

17 17.1 C K&R, :,,, ( ),, K&R, (Indian Hill Style Guide), 17.7, 10.4 : [K&R1, Sec. 1.2 p. 10]; [K&R2, Sec. 1.2 p. 10] 17.2 if(!strcmp(s1, s2)),,,,! ( ),, : #define Streq(s1, s2) (strcmp((s1), (s2)) == 0) 17.8 17.3 if (0 == x) if (x == 0) : if (x = 0) ==, : if (0 = x), = : [H&S, Sec. 7.6.5 pp. 209-10] 93

17 94 17.4 extern int func ((int, int));, ANSI,, #ifdef STDC #define (proto) proto #else #define (proto) () #endif 17.5 printf(), (void) printf(), lint, (void) :, ( ), strcpy() strcat(), : [K&R2, Sec. A6.7 p. 199]; [Rationale, Sec. 3.3.4]; [H&S, Sec. 6.2.9 p. 172, Sec. 7.13 pp. 229-30] 17.6 (Hungarian Notation), Charles Simonyi ( ),,, : [Simonyi&Heller] 17.7 (Indian Hill Style Guide) ftp ftp.cs.washington.edu ftp.cs.toronto.edu ftp.cs.umd.edu pub/cstyle.tar.z (, (Indian Hill Guide)) doc/programming ( Henry Spencer C ( 10 Commandments for C Programmers )) pub/style-guide

17 95 : The Elements of Programming Style [K&P], Plum Hall Programming Guidelines [Plum], C Style: Standards and Guidelines [Straker] [21] 18.7 17.8 goto,,,, goto,,, goto, goto, goto, goto,,,, ( 5.3, 5.7, 9.2 10.5),,

17 96

18 URL,, 18.1 ( 18.18) C cflow, cxref, calls, cscope, xscope, ixfw C / cb, indent, GNU indent, vgrind CVS, RCS, SCCS C ( ) obfus, shroud, opqcp make makedepend, cc -M cpp -M ccount, Metre, lcount, csize; McCable and Associates C UNIX wc, grep -c ";" C (cdecl) comp.sources.unix 14 ( 18.18) [K&R2] 11.30 malloc 18.2 C 10.16 11.30 20.23 C (lint) 18.5 C 18.3,, Usenet comp.compilers comp.software-eng 18.3 18.18 97

18 98 18.2 malloc malloc Conor P. Cahill dbmalloc, comp.sources.misc 1992 32 leak comp.sources.unix 27 ; Snippets JMalloc.c, JMalloc.h; MEMDEBUG (ftp://ftp.crpht.lu/pub/sources/memdebug); Electric Fence 18.18, malloc : CodeCenter (Saber-C) from Centerline Software (http://www.centerline.com/) Insight (now Insure?), from ParaSoft Corporation (http://www.parasoft. com/) Purify, from Rational Software (http://www-306.ibm.com/software/rational/, Pure Software, IBM ) ZeroFault, from The ZeroFault Group (http://www.zerofault.com/) 18.3 GNU C (http://gcc.gnu.org/) C. djgpp (http://www.delorie.com/djgpp/) MS-DOS GCC, Macs Windwos GCC 1 lcc http://www.cs.virginia.edu/ lcc-win32/, http: //www.cs.princeton.edu/software/lcc/ Power C Mix Sotfware MS-DOS 1132 Commerce Drive, Richardson, TX 75801, USA, 214-783-6001. ftp://ftp.hitech.com.au/hitech/pacific MS-DOS C comp.compilers http://compilers.iecc.com/ FAQ 18.18 18.4, lint ( -a, -c, -h, -p ) C, 1 : Windows cygwin (http://www.cygwin.com/) MinGW (http: //http://www.mingw.org/)

18 99 16.5 16.7 18.5 18.5 ANSI lint PC-Lint FlexeLint Gimpel Software (http://www.gimpel. com/) Unix System V 4 lint ANSI UNIX Support Labs System V ( C ) ANSI lint Splint ( lclint, http://www.splint.org/) lint, lint gcc -Wall -pedantic 18.6 ANSI lint,, lint,, lint lint, 1.3 10.4 18.7 C : Tom Torfs http://cprog.tomsweb.net Christopher Sawtell C (Notes for C programmers) : ftp://svr-ftp.eng.cam.ac.uk/misc/sawtell C. shar, ftp://garbo.uwasa.fi/pc/c-lang/c-lesson.zip, http://www.fi.uib.no/fysisk/ Teori/KURS/OTHER/newzealand.html Time Love C (C for Programmers) http://www-h.eng.cam. ac.uk/help/tpl/languages/c/teaching C/ The Coronado Enterprises C tutorials Simtel pub/msdos/c, http://www.coronadoenterprises.com/tutorials/c/index.html Steve Holmes http://www.strath.ac.uk/it/docs/ccourse/ Martin Brown C http://www-isis.ecs.soton.ac.uk/ computing/c/welcome.html UNIX, shell, learn c

18 100, FAQ C, http://www.eskimo.com/ scs/cclass/cclass.html :,,,,,, C, http://www.lysator. liu.se/c/index.html Vinit Carpenter C C++, comp.lang.c comp.lang.c++, FAQ ( 20.36), http://www.cyberdiem.com/vin/learn.html 18.8 18.9 18.16 18.8, ftp://garbo.uwasa.fi/pc/c-lang/00index.txt, http: //www.eskimo.com/ scs/src/,,, 18.7, 18.10, 18.16 18.18 18.9 C C,,, Kernighan Richie The C programming Language ( K&R, [K&R2]) ; C, ;,,, : http://www.csd.uwo.ca/ jamie/.refs/.footnotes/c-annotes.html, http://www.eskimo.com/ scs/cclass/ cclass.html, http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html comp.lang.c K.N. King C: A Modern Approach Samuel P. Harbison Guy L. Steele C: A Reference Manual [H&S] C, FAQ, [CFAQ] C C++ (Association of C and C++, ACCU) C/C++ (http://www.accu.org/bookreviews/public/) 18.7

18 101 18.10 C GNU C (http://www.gnu.org/software/libc/) P.J. Plauger The Standard C Library [Plauger], 18.8, 18.16 18.18 18.11 C : http://www.cs.man.ac.uk/standard c/ index.html, http://www.dinkumware.com/htm cl/index.html 18.12 ANSI/ISO C 11.2 18.13 : defunc, 1993 12 comp.sources. misc (V41 i32,33), 1994 1 alt.sources URL : ftp://sunsite.unc.edu/pub/packages/development/libraries/defunc-1.3.tar. Z; parse, lamont.ldgo.columbia.edu S-Lang (ftp://amy.tch.harvard.edu/pub/slang), Cmm ( C C ) 18.18 20.4 18.14 C BNF YACC ANSI Jim Roskind ftp://ftp. eskimo.com/u/s/scs/roskind grammar.z ANSI C90 ftp://ftp.uu.net/usenet/net.sources/ansi.c.grammar.z, lexer FSF GNU C, K&R comp.compilers, 18.3 : [K&R1, Sec. A18 pp. 214-219]; [K&R2, Sec. A13 pp. 234-239]; [ISO, Sec. B.2]; [H&S, pp. 423-435 Appendix B] 18.15 C Plum Hall ( Cardiff, NJ, Hawaii) ; Ronald Guilmette RoadTest TM ( ftp://netcom.com/pub/ rfg/roadtest/announce.txt); Nullstone (http://www.

18 102 nullstone.com) FSF GNU C (gcc) C Kahan (ftp://netlib.att.com/netlib/paranoia), C 18.16 Bob Stout SNIPPETS (ftp://ftp.brokersys.com/pub/ snippets http://www.brokersys.com/snippets/) Lars Wirzenius publib (ftp://ftp.funet.fi/pub/languages/c/publib/) 14.11, 18.7, 18.8, 18.10 18.18 18.17 : quad, BSD Unix libc (ftp.uu.net, /systems/unix/bsd-sources/.../src/lib/libc/quad/*); GNU MP libmp ; MIRACL (http://indigo.ie/ mscott/); David Bell Landon Curt Noll calc ; Unix libmp.a 14.11 18.18 18.18,,, : ftp.uu.net, archive.umich.edu, oak. oakland.edu, sumex-aim.stanford.edu wuarchive.wustl.edu FSF GNU ftp.gnu.org,, ftp ftp, ftp-by-mail, (WWW),, archie,, Alta Vista, Excite Yahoo Usenet, comp.sources.unix comp.sources.misc, : ftp://gatekeeper.dec.com/pub/usenet/comp.sources.unix/, ftp://ftp.uu. net/usenet/comp.sources.unix/ comp.archives

18 103 ftp,, FAQ How to find sources ( ) 14.11, 18.8, 18.10 18.16

18 104

19 19.1 RETURN, C,, ( ),,, RETURN, ( getchar() ),,, ( getchar() ),, ( MS-DOS, VMS ), ( Unix, VMS ), ( ),, ( read(), getchar() ), ( ),, ( ),, comp.lang.c C,, comp.unix.questions comp.os.msdos.programmer,, Unix,,,, curses cbreak(), noecho() getch(),, getpass() Unix, ioctl(), 105

19 106 CBREAK RAW, System V POSIX ICANON, c cc[vmin] c cc[vtime], ECHO, system() stty,, <sgtty.h> tty(4), System V, <termio.h> termio(4), POSIX, <termios.h> termios(4) MS-DOS, getch() getche(), BIOS VMS, (SMG$), curses, $QIO IO$ READVBLK, IO$M NOECHO VMS,,, setbuf() setvbuf() sdtin,, 1), ( ), 2), 3),, ( 20.36) 19.2 : [PCS, Sec. 10 pp. 128-9, Sec. 10.1 pp. 130-1]; [POSIX, Sec. 7] 19.2,,, curses nodelay(),, select poll, ioctl FIONREAD, c cc[vtime], kbhit(), rdchk(), open() fcntl() O NDELAY 19.1 19.3, \r, \b, fflush() : [ISO, Sec. 5.2.2] 19.4 ( ) termcap, terminfo curses, MS-DOS, clrscr() gotoxy()

19 107 : ( \f ), ( ), : system() ( 19.30) [PCS, Sec. 5.1.4 pp. 54-60, Sec. 5.1.5 pp. 60-62] 19.5, terminfo, termcap, curses ASCII, ( ESC [ \033 ] ) keypad(), curses MS-DOS,, 0 ( 0 ), DOS : 72, 80, 75, 77, 59 68 : [PCS, Sec. 5.1.4 pp. 56-7] 19.6,, FAQ X Windown, MD-DOS, Macintosh, [PCS, Sec. 5.5 pp. 78-80] 19.7 ( comm ) Unix, /dev, ( 19.1 19.2) MS-DOS, stdaux, COM1, BIOS,, Joe Campbell C ( C Programmer s Guide to Serial Communications ) 19.8 Unix, popen() ( 19.31) lp lpr, /dev/lp MS-DOS, stdprn ( ), PRN LPT1, ( ), : [PCS, Sec. 5.3 pp. 72-74]

19 108 19.9 ( 19.8), ASCII, 033 ( 27), ESC[J: fprintf(ofd, "\033[J"); 19.10, :, inport outport ( ), (memory-mapped I/O) 19.29 19.11, Unix (plot(3) plot(5)) Robert Maier GNU libplot, (http://www.gnu.org/software/plotutils/plotutils.html) OpenGL, GKS PHIGS MS-DOS, VESA BGI,, 19.9. C, (Macintosh, X Window, Microsoft Windows),, FAQ : [PCS, Sec. 5.4 pp. 75-77] 19.12 GIF JPEG, http://www. ijg.org/files/ JPEG 19.13, ( ), stat(), access() fopen() fopen(),,,,

19 109 fopen(), access, Unix UID,,,,,, O EXCL, [PCS, Sec. 12 pp. 189,213]; [POSIX, ec. 5.3.1, Sec. 5.6.2, Sec. 5.6.3.] 19.14, C, Unix stat() Unix stat(), fseek(), ftell(), fstat(), : fstat(), stat() ; ftell(),,,, fseek SEEK END filesize() filelength(), C,, : [ISO, Sec. 7.9.9.4]; [H&S, Sec. 15.5.1]; [PCS, Sec. 12 p. 213]; [POSIX, Sec. 5.6.2] 19.15 Unix POSIX stat(), 19.14 19.16 BSD ftruncate(), chsize(), fcntl F FREESP MS-DOS, write(fd, "", 0),, 19.17 19.17 ( ),,, 12.26 19.16

19 110 19.18, Unix,,, (pipe) (, ),, ( fopen() ) 19.19 C remove() ANSI Unix, remove(), unlink() [K&R2, Sec. B1.1 p. 242; ISO Sec. 7.9.4.1]; [H&S, Sec. 15.15 p. 382]; [PCS, Sec. 12 pp. 208,220-221]; [POSIX, Sec. 5.5.1, Sec. 8.2.4] 19.20 system(), 19.30 ( fopen() ),, : [K&R2, Sec. 1, Sec. 7] 19.21 fopen("c:\ newdir \file.dat", "r") \n \f,,, \, fopen() ( ),, : fopen("c:\\newdir\\file.dat", "r"), MS-DOS,, fopen("c:/newdir/file.dat", "r"),, #include,

19 111 19.22 fopen() : "$HOME/.profile" " /.myrcfile" Unix, $HOME shell fopen() 19.23 MS-DOS Abort, Retry, Ignore?, DOS, 24H comp.os.msdos.programmer FAQ 19.24 Too many open files (, : ; stdio FILE MS-DOS, CONFIG.SYS, handle stdio FILE ( ) 19.25 C opendir() readdir(), POSIX, Unix MS-DOS, VMS MS- DOS FINDFIRST FINDNEXT,, MS Windows FindFirstFile FindNextFile readdir(),, stat(), 13.5 : cite[sec. 8.6 pp. 179-184]kr2; [PCS, Sec. 13 pp. 230-1]; [POSIX, Sec. 5.1]; [Schumacher, ed., Sec. 8] 19.26, 19.27 64K,,,

19 112 64K, C 32K, C99 64K,,,, 6.13 PC ( 8086), 64K 640K, huge ( ),, malloc halloc() farmalloc(), 32 ( djgpp, 18.3), DOS, : [ISO, Sec. 5.2.4.1]; [C9X, Sec. 5.2.4.1] 19.28 DGROUP data allocation exceeds 64K (DGROUP 64K), 64K, MS-DOS (, ),, ( ),,,, Microsoft /Gt 19.29 ( ),, : unsigned int *magicloc = (unsigned int *)0x12345678;, *magiloc, volatile MS-DOS,, MK FP : [K&R1, Sec. A14.4 p. 210]; [K&R2, Sec. A6.6 p. 199]; [ISO, Sec. 6.3.4;]; [Rationale, Sec. 3.3.4]; [H&S, Sec. 6.2.7 pp. 171-2] 19.30 C (, ) system(),, ( ),, system(), sprintf()

19 113,, exec spawn ( execl, execv, spawnl, spawnv ) 19.31 : [K&R1, Sec. 7.9 p. 157]; [K&R2, Sec. 7.8.4 p. 167, Sec. B6 p. 253]; [ISO, Sec. 7.10.4.5]; [H&S, Sec. 19.2 p. 407]; [PCS, Sec. 11 p. 179] 19.31, Unix popen(), stdio, ( ),, pclose() popen(), system(), Unix, popen(), pipe(), dup(), fork() exec() : [PCS, Sec. 11 p. 169] 19.32 arg[0], arg[0],, : [K&R1, Sec. 5.11 p. 111]; [K&R2, Sec. 5.10 p. 115]; [ISO, Sec. 5.1.2.2.1]; [H&S, Sec. 20.1 p. 416.] 19.33 19.32, ( ),,, 19.34 Unix /,,, Unix, ( setenv() putenv() ),, MS-DOS,, MS-DOS FAQ

19 114 19.35 malloc,, BSD Unix, system() ld -A SunOS System V -ldl, VMS, LIB$FIND IMAGE SYMBOL GNU dld 15.12 19.36, : clock(), delay(), ftime(), getimeofday(), msleep(), nap(), napms(), nanaosleep(), setitimer(), sleep(), Sleep(), times() usleep Unix, wait() select() poll() ( ) MS-DOS,, clock() ANSI clock(), CLOCKS PER SEC 1,, clock(),,,,,,,, sleep(), select() poll() alarm() setitimer(), : long int i; for (i = 0; i < 1000000; ++i) ;,,, : [H&S, Sec. 18.1 pp. 398-9]; [PCS, Sec. 12 pp. 197-8,215-6]; [POSIX, Sec. 4.5.2] 19.37 control-c signal() #include <signal.h> singal(sigint, SIG_IGN);, : extern void func(int); signal(sigint, func);

19 115, func() ( Unix), extern void func(int); if(signal(sigint, SIG_IGN)!= SIG_IGN) signal(sigint, func);, signal,, 19.1,,, ( ) MS-DOS, setcbrk() ctrlbrk() : [ISO, Secs. 7.7,7.7.1]; [H&S, Sec. 19.6 pp. 411-3]; [PCS, Sec. 12 pp. 210-2]; [POSIX, Secs. 3.3.1,3.3.4] 19.38, matherr(), ( <math> ), signal() ( 19.37) SIGFPE 14.9 : [Rationale, Sec. 4.5.1] 19.39 socket /, C : Douglas Comer Internetworking with TCP/IP W. R. Stevens UNIX Network Programming, Unix Socket FAQ (http://www.developerweb.net/ sock-faq/), Beej s Guide to Network Programming (http://www.ecst.csuchico. edu/ beej/guide/net/) :, -lsocket -lnsl ; 13.18 19.40 BIOS ISR TSR ( MS-DOS PC ) comp.os.msdos.programmer FAQ ; Ralf Brown 19.41, union REGS int86() MS-DOS

19 116 19.42 near far, ;, DOS Windows 19.43, ANSI!, ANSI/ISO C ;, POSIX (IEEE 1003.1, ISO/IEC 9945-1), ( Unix) POSIX, ANSI, #ifdef

20 20.1,,, 4.4 7.6 20.2 main(), argv 8.2, 13.5 19.25 : [K&R1, Sec. 5.11 pp. 110-114]; [K&R2, Sec. 5.10 pp. 114-118]; [ISO, Sec. 5.1.2.2.1]; [H&S, Sec. 20.1 p. 416]; [PCS, Sec. 5.6 pp. 81-2, Sec. 11 p. 159, pp. 339-40 Appendix F]; [Schumacher, ed., Sec. 4 pp. 75-85] 20.3, ( ASCII), fprintf(), fscanf(),,,,,, I/O Sun XDR (RFC 1014) OSI ASN.1 ( CCITT X.409 ISO 8825 Basic Encoding Rules ) CDF netcdf HDF 2.10 12.30 : [PCS, Sec. 6 pp. 86, 88] 20.4 char * int one_func(), two_func(); int red_func(), blue_func(); struct { char *name; int (*funcptr)(); } symtab[] = { "one_func", one_func, 117

20 118 "two_func", two_func, "red_func", red_func, "blue_func",blue_func, };, 2.13, 18.13 19.35 : [PCS, Sec. 11 p. 168] 20.5 int char,, char #include <limits.h> /* for CHAR_BIT */ #define BITMASK(b) (1 << ((b) % CHAR_BIT)) #define BITSLOT(b) ((b) / CHAR_BIT) #define BITSET(a, b) ((a)[bitslot(b)] = BITMASK(b)) #define BITTEST(a, b) ((a)[bitslot(b)] & BITMASK(b)) <limits.h>, CHAR BIT 8 : [H&S, Sec. 7.6.7 pp. 211-216] 20.6 int x = 1; if(*(char *)&x == 1) printf("little-endian\n"); else printf("big-endian\n"); 10.15 20.7 : [H&S, Sec. 6.1.2 pp. 163-4] 20.7 V7 Unix swap(),, ; 20.6 ( BSD ntohs() ), ( ),,,, 20.6 : [PCS, Sec. 11 p. 179]

20 119 20.8,,,, 0 0x ( ) I/O, printf scanf, (%d, %o %x ); strtol() strtoul(),, ( strtol ) I/O, I/O, 2.9 8.4 13.1 : [ISO, Secs. 7.10.1.5,7.10.1.6] 20.9 printf() 20.8 strtol() 20.10 1 20.11) ( 20.11,,,,,,,,,,,,, profiling, ( I/O ),,, (, ),,,, ;

20 120,,, Kernighan Plauger The Elements of Programming Style [K&P], Jon Bentley Writing Efficient Programs [Bentley] 20.12 ++i i = i +1,,,,,,,,,,,, i = i + 1,, ++i, i += 1 i = i + 1,, 3.10 20.13,, i/=2 i, ( : i ), 20.14 a ^= b; b ^= a; a ^= b;, : int t = a; a = b; b = t;, ( EXCH ), XOR 3.4 10.2

20 121 20.15,,, strcmp() if/else 10.11, 20.16 20.26 : [K&R1, Sec. 3.4 p. 55]; [K&R2, Sec. 3.4 p. 58]; [ISO, Sec. 6.6.4.2]; [H&S, Sec. 8.7 p. 248] 20.16 case ( ) switch, case, case,, if/else 20.15 : [K&R1, Sec. 3.4 p. 55]; [K&R2, Sec. 3.4 p. 58]; [ISO, Sec. 6.6.4.2]; [Rationale, Sec. 3.6.4.2]; [H&S, Sec. 8.7 p. 248] 20.17 return, C,, C,,,, sizeof : [K&R1, Sec. A18.3 p. 218]; [ISO, Sec. 6.3.3, Sec. 6.6.6]; [H&S, Sec. 8.9 p. 254.] 20.18 C C PL/I, C, #ifdef #if 0, ( 11.20) /* */,, ( C ) // C99 : [K&R1, Sec. A2.1 p. 179]; [K&R2, Sec. A2.2 p. 192]; [ISO, Sec. 6.1.9, Annex F; Rationale Sec. 3.1.9]; [H&S, Sec. 2.2 pp. 18-9]; [PCS, Sec. 10 p. 130]

20 122 20.19 C a+++++b C,,,, : a ++ ++ + b : [K&R1, Sec. A2 p. 179]; [K&R2, Sec. A2.1 p. 192]; [ISO, Sec. 6.1]; [H&S, Sec. 2.3 pp. 19-20] 20.20 C,,, gcc ( qsort ), (static),,, qsort 20.21 assert() <assert.h>,,,,, : assert(p!= NULL);, malloc() fopen() : [K&R2, Sec. B6 pp. 253-4]; [ISO, Sec. 7.2]; [H&S, Sec. 19.1 p. 406] 20.22 C FORTRAN (C++, BASIC, Pascal, Ada, LISP),,, FORTRAN, Glenn Geers FORT.gz, ftp suphys.physics.su.oz.au src Burkhard Burow cfortran.h C/FORTRAN ftp zebra.desy.de http://www-zeus.desy.de/ burow

20 123 C++, C C : [H&S, Sec. 4.9.8 pp. 106-7] 20.23 Pascal Fortran ( LISP, Ada, awk, C) C : p2c Dave Gillespie Pascal C, comp.sources. unix 1990 ( 21 ); ftp://csvax.cs.caltech.edu/pub/p2c-1. 20.tar.Z ptoc Pascal C, Pascal (comp.sources.unix, 10, 13 ) f2c Fortran C, Bell Labs, Bellcore Carnegie Mellon f2c : email send index from f2c netlib@research.att.com research!netlib ( ftp netlib.att.com netlib/f2c ) FAQ 11.30 18.18 20.24 C++ C C++ C C++ C, C, C C++, ANSI C C++,,, C C++, C C++, C C++ ; 8.5 20.18 : [H&S, p. xviii, Sec. 1.1.5 p. 6, Sec. 2.8 pp. 36-7, Sec. 4.9 pp. 104-107] 20.25 strcmp,, Sun Wu Udi Manber AGREP A Fast Approximate Pattern- Matching Tool [AGREP], soundex, ( ),

20 124 : [Knuth, Sec. 6 pp. 391-2 Volume 3]; [AGREP] 20.26, ( ) ( ),, ; : [K&R2, Sec. 6.6]; [Knuth, Sec. 6.4 pp. 506-549 Volume 3]; [Sedgewick, Sec. 16 pp. 231-244] 20.27, mktime() localtime() ( 13.11 13.12, tm hour 0, DST ( ) ); Zeller congruence ( sci.math FAQ); Tomohiko Sakamoto : int dayofweek(int y, int m, int d) /* 0 = Sunday */ { static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; y -= m < 3; return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7; } 13.12 20.28 : [ISO, Sec. 7.12.2.3] 20.28 (year%4 == 0) 2000 (2000 ), year % 4 == 0 && (year % 100!= 0 year % 400 == 0) ; 4000 13.12 20.29 :, (, ): char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}"; main(){printf(s,34,s,34);}

20 125, #include <stdio.h>, " 34, ASCII James Hu #define q(k)main(){return!puts(#k"\nq("#k")");} q(#define q(k)main(){return!puts(#k"\nq("#k")");}) 20.30 (Duff s Device), Tom Duff Lucasfilm, : register n = (count + 7) / 8; /* count > 0 assumed */ switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } count from to (, ) swtich 8, ( count 8 ), case swtich C, Duff C swtich,,,, 20.31 C (IOCCC) html ; http://www.ioccc.org/index. Usenix, ( 1984 ) ftp.uu.net ( 18.18), pub/ioccc/ ; http://www.ioccc.org/index.html

20 126 20.32 [K&R1] entry, FOR- TRAN, ( ), ANSI C 1.5 : [K&R2, p. 259 Appendix C] 20.33 C C Ken Thompson B, B Martin Richards BCPL (Basic Combined Programming Language), BCPL CPL (Combined Programming Language Cambridge Programming Language), C P (BCPL ) D,, C++ 20.34 char C char : char, character ); care car ( 20.35 lvalue rvalue, lvalue ;, 6.5 rvalue, 20.36 FAQ ( ) ftp.eskimo.com u/s/scs/c-faq/., FAQ comp.lang.c,, ( ),, FAQ comp.answers news.answers news.answers FAQ, ; : rtfm.mit.edu ( pub/usenet/news.answers/c-faq/ pub/usenet/comp. lang.c/); ftp.uu.net ( usenet/news.answers/c-faq/) ftp, rtfm.mit.edu FAQ : help mail-server@rtfm.mit.edu, news.answers meta-faq

20 127 FAQ (HTML) ; URL http://www. eskimo.com/ scs/c-faq/top.html Usenet FAQ http://www.faqs.org/faqs/ FAQ Addison-Wesley : C Programming FAQs: Frequently Asked Questions (ISBN 0-201-84519-9) [CFAQ] http:// www.eskimo.com/ scs/c-faq/book/errata.html ftp://ftp.eskimo.com/u/s/ scs/ftp/c-faq/book/errata HTML http://c-faq-chn.sourceforge.net/ PDF

20 128

21 Jamshid Afshar, Lauri Alanko, Michael B. Allen, David Anderson, Jens Andreasen, Tanner Andrews, Sudheer Apte, Joseph Arceneaux, Randall Atkinson, Kaleb Axon, Daniel Barker, Rick Beem, Peter Bennett, Mathias Bergqvist, Wayne Berke, Dan Bernstein, Tanmoy Bhattacharya, John Bickers, Kevin Black, Gary Blaine, Yuan Bo, Mark J. Bobak, Anthony Borla, Dave Boutcher, Alan Bowler, breadbox@muppetlabs.com, Michael Bresnahan, Walter Briscoe, Vincent Broman, Robert T. Brown, Stan Brown, John R. Buchan, Joe Buehler, Kimberley Burchett, Gordon Burditt, Scott Burkett, Eberhard Burr, Burkhard Burow, Conor P. Cahill, D Arcy J.M. Cain, Christopher Calabrese, Ian Cargill, Vinit Carpenter, Paul Carter, Mike Chambers, Billy Chambless, C. Ron Charlton, Franklin Chen, Jonathan Chen, Raymond Chen, Richard Cheung, Avinash Chopde, Steve Clamage, Ken Corbin, Dann Corbit, Ian Cottam, Russ Cox, Jonathan Coxhead, Lee Crawford, Nick Cropper, Steve Dahmer, Jim Dalsimer, Andrew Daviel, James Davies, John E. Davis, Ken Delong, Norm Diamond, Jamie Dickson, Bob Dinse, dlynes@plenary-software, Colin Dooley, Jeff Dunlop, Ray Dunn, Stephen M. Dunn, Andrew Dunstan, Michael J. Eager, Scott Ehrlich, Arno Eigenwillig, Yoav Eilat, Dave Eisen, Joe English, Bjorn Engsig, David Evans, Andreas Fassl, Clive D.W. Feather, Dominic Feeley, Simao Ferraz, Pete Filandr, Bill Finke Jr., Chris Flatters, Rod Flores, Alexander Forst, Steve Fosdick, Jeff Francis, Ken Fuchs, Tom Gambill, Dave Gillespie, Samuel Goldstein, Willis Gooch, Tim Goodwin, Alasdair Grant, W. Wesley Groleau, Ron Guilmette, Craig Gullixson, Doug Gwyn, Michael Hafner, Zhonglin Han, Darrel Hankerson, Tony Hansen, Douglas Wilhelm Harder, Elliotte Rusty Harold, Joe Harrington, Guy Harris, John Hascall, Adrian Havill, Richard Heathfield, Des Herriott, Ger Hobbelt, Sam Hobbs, Joel Ray Holveck, Jos Horsmeier, Syed Zaeem Hosain, Blair Houghton, Phil Howard, Peter Hryczanek, James C. Hu, Chin Huang, Jason Hughes, David Hurt, Einar Indridason, Vladimir Ivanovic, Jon Jagger, Ke Jin, Kirk Johnson, David Jones, Larry Jones, Morris M. Keesan, Arjan Kenter, Bhaktha Keshavachar, James Kew, Bill Kilgore, Darrell Kindred, Lawrence Kirby, Kin-ichi Kitano, Peter Klausler, John Kleinjans, Andrew Koenig, Thomas Koenig, Adam Kolawa, Jukka Korpela, Przemyslaw Kowalczyk, Ajoy Krishnan T, Anders Kristensen, Jon Krom, Markus Kuhn, Deepak Kulkarni, Yohan Kun, B. Kurtz, Kaz Kylheku, Oliver Laumann, John Lauro, Felix Lee, Mike Lee, Timothy J. Lee, Tony Lee, Marty Leisner, Eric Lemings, Dave Lewis, Don Libes, Brian Liedtke, Philip Lijnzaad, James D. Lin, Keith Lindsay, Yen-Wei Liu, Paul Long, Patrick J. LoPresti, Christopher Lott, Tim Love, Paul Lutus, Mike McCarty, Tim McDaniel, Michael MacFaden, Allen Mcintosh, J. Scott McKellar, Kevin McMahon, Stuart MacMartin, John 129

21 130 R. MacMillan, Robert S. Maier, Andrew Main, Bob Makowski, Evan Manning, Barry Margolin, George Marsaglia, George Matas, Brad Mears, Wayne Mery, De Mickey, Rich Miller, Roger Miller, Bill Mitchell, Mark Moraes, Darren Morby, Bernhard Muenzer, David Murphy, Walter Murray, Ralf Muschall, Ken Nakata, Todd Nathan, Taed Nelson, Pedro Zorzenon Neto, Daniel Nielsen, Landon Curt Noll, Tim Norman, Paul Nulsen, David O Brien, Richard A. O Keefe, Adam Kolawa, Keith Edward O hara, James Ojaste, Max Okumoto, Hans Olsson, Thomas Otahal, Lloyd Parkes, Bob Peck, Harry Pehkonen, Andrew Phillips, Christopher Phillips, Francois Pinard, Nick Pitfield, Wayne Pollock, Polver@aol.com, Dan Pop, Don Porges, Claudio Potenza, Lutz Prechelt, Lynn Pye, Ed Price, Kevin D. Quitt, Pat Rankin, Arjun Ray, Eric S. Raymond, Christoph Regli, Peter W. Richards, James Robinson, Greg Roelofs, Eric Roode, Manfred Rosenboom, J.M. Rosenstock, Rick Rowe, Michael Rubenstein, Erkki Ruohtula, John C. Rush, John Rushford, Kadda Sahnine, Tomohiko Sakamoto, Matthew Saltzman, Rich Salz, Chip Salzenberg, Matthew Sams, Paul Sand, DaviD W. Sanderson, Frank Sandy, Christopher Sawtell, Jonas Schlein, Paul Schlyter, Doug Schmidt, Rene Schmit, Russell Schulz, Dean Schulze, Jens Schweikhardt, Chris Sears, Peter Seebach, Gisbert W. Selke, Patricia Shanahan, Girija Shanker, Clinton Sheppard, Aaron Sherman, Raymond Shwake, Nathan Sidwell, Thomas Siegel, Peter da Silva, Andrew Simmons, Joshua Simons, Ross Smith, Thad Smith, Henri Socha, Leslie J. Somos, Eric Sosman, Henry Spencer, David Spuler, Frederic Stark, James Stern, Zalman Stern, Michael Sternberg, Geoff Stevens, Alan Stokes, Bob Stout, Dan Stubbs, Tristan Styles, Richard Sullivan, Steve Sullivan, Melanie Summit, Erik Talvola, Christopher Taylor, Dave Taylor, Clarke Thatcher, Wayne Throop, Chris Torek, Steve Traugott, Brian Trial, Nikos Triantafillis, Ilya Tsindlekht, Andrew Tucker, Goran Uddeborg, Rodrigo Vanegas, Jim Van Zandt, Momchil Velikov, Wietse Venema, Tom Verhoeff, Ed Vielmetti, Larry Virden, Chris Volpe, Mark Warren, Alan Watson, Kurt Watzka, Larry Weiss, Martin Weitzel, Howard West, Tom White, Freek Wiedijk, Stephan Wilms, Tim Wilson, Dik T. Winter, Lars Wirzenius, Dave Wolverton, Mitch Wright, Conway Yee, James Youngman, Ozan S. Yigit, and Zhuo Zang Mark Brader, Vinit Carpenter, Stephen Clamage, Jutta Degener, Doug Gwyn, Karl Heuer, and Joseph Kent. Addison- Wesley Debbie Lafferty Tom Stone Karl Heuer, Jutta Degener, Mark Brader, ( Steve Johnson ) FAQ Steve Summit scs@eskimo.com

[ANSI] [Rationale] American National Standards Institute, American National Standard for Information Systems Programming Language C, ANSI X3.159-1989, ( 11.2 American National Standards Institute, Rationale for American National Standard for Information Systems Programming Language C 11.2 [Bentley] Jon Bentley, Writing Efficient Programs, Prentice-Hall, 1982, ISBN 0-13-970244-X. [Burki] David Burki, Date Conversions, The C Users Journal, February 1993, pp. 29-34. [LINT] Ian F. Darwin, Checking C Programs with lint, O Reilly, 1988, ISBN 0-937175-30-7. [Goldberg] [H&S] David Goldberg, What Every Computer Scientist Should Know about Floating-Point Arithmetic, ACM Computing Surveys, Vol. 23 #1, March, 1991, pp. 5-48. Samuel P. Harbison and Guy L. Steele, Jr., C: A Reference Manual, Fourth Edition, Prentice-Hall, 1995, ISBN 0-13-326224-3. [There is also a fifth edition: 2002, ISBN 0-13-089592-X.] [PCS] Mark R. Horton, Portable C Software, Prentice Hall, 1990, ISBN 0-13-868050-7. [POSIX] [ISO] [C9X] Institute of Electrical and Electronics Engineers, Portable Operating System Interface (POSIX) Part 1: System Application Program Interface (API) [C Language], IEEE Std. 1003.1, ISO/IEC 9945-1. International Organization for Standardization, ISO 9899:1990 11.2 International Organization for Standardization, WG14/N794 Working Draft 11.1 11.3 131

132 [K&P] [K&R1] [K&R2] [Knuth] Brian W. Kernighan and P.J. Plauger, The Elements of Programming Style, Second Edition, McGraw-Hill, 1978, ISBN 0-07-034207-5. Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Prentice-Hall, 1978, ISBN 0-13-110163-3. Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Second Edition, Prentice Hall, 1988, ISBN 0-13- 110362-8, 0-13-110370-9. 18.9 Donald E. Knuth, The Art of Computer Programming. Volume 1: Fundamental Algorithms, Third Edition, Addison- Wesley, 1997, ISBN 0-201-89683-4. Volume 2: Seminumerical Algorithms, Third Edition, 1997, ISBN 0-201-89684-2. Volume 3: Sorting and Searching, Second Edition, 1998, ISBN 0-201- 89685-0. [CT&P] Andrew Koenig, C Traps and Pitfalls, Addison-Wesley, 1989, ISBN 0-201-17928-8. [Marsaglia&Bray] G. Marsaglia and T.A. Bray, A Convenient Method for Generating Normal Variables, SIAM Review, Vol. 6 #3, July, 1964. [P&M] Stephen K. Park and Keith W. Miller, Random Number Generators: Good Ones are Hard to Find, Communications of the ACM, Vol. 31 #10, October, 1988, pp. 1192-1201 (also technical correspondence August, 1989, pp. 1020-1024, and July, 1993, pp. 108-110). [Plauger] P.J. Plauger, The Standard C Library, Prentice Hall, 1992, ISBN 0-13-131509-9. [Plum] [Press et al.] Thomas Plum, C Programming Guidelines, Second Edition, Plum Hall, 1989, ISBN 0-911537-07-4. William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery, Numerical Recipes in C, Second Edition, Cambridge University Press, 1992, ISBN 0-521-43108-5. [Sedgewick] Robert Sedgewick, Algorithms in C, Addison-Wesley, 1990, ISBN 0-201-51425-7. (A new edition is being prepared; the first two volumes are ISBN 0-201-31452-5 and 0-201-31663-3.)

133 [Simonyi&Heller] Charles Simonyi and Martin Heller, The Hungarian Revolution, Byte, August, 1991, pp. 131-13. [Straker] [CFAQ] [Expert] [AGREP] David Straker, C Style: Standards and Guidelines, Prentice Hall, ISBN 0-13-116898-3. Steve Summit, C Programming FAQs: Frequently Asked Questions, Addison- Wesley, 1995, ISBN 0-201-84519-9. [The book version of this FAQ list; see also http://www.eskimo.com/ scs/c-faq/ book/errata.html.] Peter van der Linden, Expert C Programming: Deep C Secrets, Prentice Hall, 1994, ISBN 0-13-177429-8. Sun Wu and Udi Manber, AGREP A Fast Approximate Pattern- Matching Tool, USENIX Conference Proceedings, Winter, 1992, pp. 153-162. [Schumacher, ed.] Schumacher, ed., Software Solutions in C [IHSG] 17.7 18.9