Xemacs Emacs



Similar documents

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裵²ó ¨¡ À©¸æ¹½Â¤¡§¾ò·ïʬ´ô ¨¡

£Ã¥×¥í¥°¥é¥ß¥ó¥°ÆþÌç (2018) - Â裶²ó ¨¡ À©¸æ¹½Â¤¡§·«¤êÊÖ¤· ¨¡

新版明解C言語 実践編

新・明解C言語 ポインタ完全攻略

ex12.dvi

卒 業 研 究 報 告.PDF

解きながら学ぶC++入門編

mstrcpy char *mstrcpy(const char *src); mstrcpy malloc (main free ) stdio.h fgets char *fgets(char *s, int size, FILE *stream); s size ( )

Original : Hello World! (0x0xbfab85e0) Copy : Hello World! (0x0x804a050) fgets mstrcpy malloc mstrcpy (main ) mstrcpy malloc free fgets stream 1 ( \n

double float

(2 Linux Mozilla [ ] [ ] [ ] [ ] URL 2 qkc, nkc ~/.cshrc (emacs 2 set path=($path /usr/meiji/pub/linux/bin tcsh b

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

‚æ4›ñ

18 C ( ) hello world.c 1 #include <stdio.h> 2 3 main() 4 { 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.o

PowerPoint プレゼンテーション

joho07-1.ppt

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

解きながら学ぶC言語

I117 7 School of Information Science, Japan Advanced Institute of Science and Technology

(K&R 2.9) ~, &,, >>, << 2. (K&R 5.7) 3. (K&R 5.9) 4. (K&R 5.10) (argc argv atoi(), atof() ) 5. (K&R 7.5) (K&R 7.6) - FILE, stdin, stdout, std

:30 12:00 I. I VI II. III. IV. a d V. VI

+ +

USB ID TA DUET 24:00 DUET XXX -YY.c ( ) XXX -YY.txt() XXX ID 3 YY ID 5 () #define StudentID 231

tuat1.dvi

Microsoft Word - C.....u.K...doc

: CR (0x0d) LF (0x0a) line separator CR Mac LF UNIX CR+LF MS-DOS WINDOWS Japan Advanced Institute of Science and Technology

II ( ) prog8-1.c s1542h017%./prog8-1 1 => 35 Hiroshi 2 => 23 Koji 3 => 67 Satoshi 4 => 87 Junko 5 => 64 Ichiro 6 => 89 Mari 7 => 73 D

prog-text.dvi

10

untitled

ex14.dvi

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

数値計算

超初心者用

:30 12:00 I. I VI II. III. IV. a d V. VI

ohp03.dvi

PC Windows 95, Windows 98, Windows NT, Windows 2000, MS-DOS, UNIX CPU

£Ã¥×¥í¥°¥é¥ß¥ó¥°(2018) - Âè11²ó – ½ÉÂꣲ¤Î²òÀ⡤±é½¬£² –

新・明解C言語 実践編

r08.dvi

ohp08.dvi


‚æ2›ñ C„¾„ê‡Ìš|

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

r07.dvi

r03.dvi

ohp07.dvi

Fortran90/95 [9]! (1 ) " " 5 "Hello!"! 3. (line) Fortran Fortran 1 2 * (1 ) 132 ( ) * 2 ( Fortran ) Fortran ,6 (continuation line) 1

/* sansu1.c */ #include <stdio.h> main() { int a, b, c; /* a, b, c */ a = 200; b = 1300; /* a 200 */ /* b 200 */ c = a + b; /* a b c */ }

kiso2-06.key

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() malloc 2 #include <stdio.h> #include

新版明解C言語入門編

2008 IIA (program) pro(before)+gram(write) (artificial language) (programming languege) (programming) (machine language) (assembly language) ( )

1 4 2 EP) (EP) (EP)

スライド タイトルなし

数値計算

I 2 tutimura/ I 2 p.1/??

C

PowerPoint Presentation

1 1.1 C 2 1 double a[ ][ ]; 1 3x x3 ( ) malloc() 2 double *a[ ]; double 1 malloc() dou

橡Pro PDF

joho09.ppt


1 return main() { main main C 1 戻り値の型 関数名 引数 関数ブロックをあらわす中括弧 main() 関数の定義 int main(void){ printf("hello World!!\n"); return 0; 戻り値 1: main() 2.2 C main

1.1 1 C IIA $ cd comp3a %endminipage ~/comp3a mkdir $ mkdir comp3a $ cd comp3a C.c Emacs Cntrol x Control s 2 Emacs Control-x Control-f Control-

C 2 / 21 1 y = x 1.1 lagrange.c 1 / Laglange / 2 #include <stdio.h> 3 #include <math.h> 4 int main() 5 { 6 float x[10], y[10]; 7 float xx, pn, p; 8 in

untitled

C¥×¥í¥°¥é¥ß¥ó¥° ÆþÌç

1.ppt

C による数値計算法入門 ( 第 2 版 ) 新装版 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 新装版 1 刷発行時のものです.

program.dvi

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

BW BW

9 ライブラリ関数 ( 教科書 P.84) C 言語には使用頻度が高そうな機能は関数として用意されており, それらは幾つかのライブラリに分類されている. それらの総称を C 言語標準ライブラリと呼ぶ. 次を参照のこと.

I ASCII ( ) NUL 16 DLE SP P p 1 SOH 17 DC1! 1 A Q a q STX 2 18 DC2 " 2 B R b

C言語によるアルゴリズムとデータ構造

I J

Informatics 2010.key

Microsoft Word - Cプログラミング演習(8)

計算機プログラミング

Informatics 2014

TEX American Mathematical Society PostScript Adobe Systems Incorporated

Microsoft PowerPoint - CproNt11.ppt [互換モード]

2 P.S.P.T. P.S.P.T. wiki 26

tuat2.dvi

C B


lexex.dvi

A 30 A A ( ) 2 C C (, machine language) C (C compiler) ( ) Mac Apple Xcode Clan

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

Microsoft Word - Cプログラミング演習(12)

Informatics 2015

Excel ではじめる数値解析 サンプルページ この本の定価 判型などは, 以下の URL からご覧いただけます. このサンプルページの内容は, 初版 1 刷発行時のものです.

Microsoft PowerPoint - CproNt02.ppt [互換モード]

ex01.dvi

9 8 7 (x-1.0)*(x-1.0) *(x-1.0) (a) f(a) (b) f(a) Figure 1: f(a) a =1.0 (1) a 1.0 f(1.0)

3.1 stdio.h iostream List.2 using namespace std C printf ( ) %d %f %s %d C++ cout cout List.2 Hello World! cout << float a = 1.2f; int b = 3; cout <<

num2.dvi

i

Transcription:

1 5 1.1.... 5 1.1.1............ 6 1.2 Xemacs.......... 6 1.2.1 Emacs....... 7 1.3............. 8 2 10 2.1..................... 10 2.1.1................ 10 2.2..................... 10 2.2.1................ 10 2.2.2............ 11 2.2.3 void................ 11 2.3 printf().......... 12 2.4 main.............. 13 3 14 3.1..................... 14 3.2 (relational operartor)....... 14 3.3 (logical operartor)........ 14 3.4................. 14 3.4.1 if...else.............. 14 3.5 switch...case............... 16 3.6.................. 17 4 loop 18 4.1 while................... 18 4.1.1 while................. 18 4.1.2 do...while............. 19 4.2 for..................... 19 4.2.1.............. 19 4.3.................. 20 4.3.1 break................. 20 4.3.2 continue............... 21 4.3.3 goto................. 21 4.3.4 return................ 21 4.4............ 21 5 23 5.1............ 23 5.2............ 23 5.2.1...... 23 5.3.................. 24 5.4 1.......... 25 5.5.................. 25 5.5.1...... 25 5.5.2.......... 26 6 28 6.1............. 28 6.2................. 28 6.3............... 29 6.4................. 29 6.4.1.............. 30 6.4.2....... 30 6.5................... 31 6.6................. 32 7 34 7.1 char............ 34 7.2................. 35 7.2.1............ 35 7.2.2 copy duplicate. 35 7.2.3 compare..... 35 7.2.4 concatenate.... 37 7.2.5....... 37 7.3................. 37 7.4................. 37 8 39 8.1................. 39 8.2................. 39 8.2.1. 39 8.3.............. 39 8.3.1................ 40 8.4............ 40 8.4.1....... 40 8.5.... 41 8.5.1........... 42 8.6............... 42 9 44 9.1............ 44 9.2................. 44 9.3.......... 44 9.3.1......... 44 9.3.2.......... 44 9.4 typedef.................... 45 9.5 3........... 45 9.6 struct tm........ 47 10 48 10.1 #include................... 48 10.2 #define.................... 48

11 49 11.1 FILE fopen(). 49 11.2 : fclose()....... 49 11.3...... 49 11.4 fflush()... 49 11.5........ 50 11.6 fread() fwrite()................... 50 12 system() 51 13 C99 52 13.1 complex.h tgmath.h.......... 52 14 53 14.1 (sorting)................. 53 14.1.1........... 53 14.1.2............ 54 14.1.3........... 54 C.5.3 diff.................. 66 C.5.4 date.................. 66 C.5.5 sort.................. 67 C.6............... 67 C.6.1 tar................... 67 C.6.2 gzip, gunzip.............. 67 C.6.3 bzip2, bunzip2............ 68 C.6.4 compress, uncompress........ 68 C.6.5 lha,zip................. 68 C.6.6 mtools................. 68 C.6.7 split.................. 68 C.6.8 uuencode, uudecode......... 68 C.7 bc....................... 69 71 57 A 57 A.1..................... 57 A.1.1........... 57 B 58 B.1............... 59 B.2 PAD...................... 59 B.2.1 pad2ps................ 59 C Linux UNIX 60 C.1 Login/Logout................. 60 C.1.1 login................. 60 C.1.2 logout................. 60 C.1.3................ 60 C.2.......... 60 C.2.1........... 60 C.2.2.......... 61 C.2.3......... 61 C.3 UNIX............ 61 C.3.1........... 62 C.3.2.. 62 C.3.3..... 63 C.3.4............ 64 C.4 > >>...... 65 C.5........ 66 C.5.1 indent................. 66 C.5.2 grep.................. 66

C C UNIX OS (Operating sysytem) C C C C C 1999 C C () C & Q&A C Study C C 13 lesson C C/C++ C The C Programming Language, Second Edition C B. W. Kernighan Dennis M. Ritchie C K&R Dennis M. Ritchie C C 12 n n C 1fefefefefefefefefefefe1 1fefefefefefefefefefefe1 1

C webmail Webmail webmail Ctrl + \ + xwnmo Xemacs Xemacs xwnmo webmail 1 Webmail webmail Mozilla Netscape WWW webmail URL http://webmail.mse.waseda.ac.jp Login Name Password login 1 C char 1 =8 256 ASCII 7 man ascii 2 3 (JIS) 8 7 JIS 7 JIS 7 JIS 8 Linux UNIX OS Enhanced Unix CodeMS-DOS/Win MAC JIS MS 1 webmail 2

C OS UNIX OS nkf, lv nkf Win32 man nkf EUC-J nkf -e filename > filename.euc mv filename.euc filename nkf nkf -e filename > filename JIS -e -s JIS -j 2 qkc qkc -e filename filename EUC-J UNICODE 255 4 2 32 40 ascii 1 4 4 ascii 1 (UTF8) MS-Win S-JIS EUC-J JIS UNICODE JIS UTF8 UNICODE 1 lv UNICODE UTF8 JIS, EUC-J, S-JIS, UTF8 man lv 1 OS OS Linux EUC (EUC-J) LF MS-Win JIS (S-JIS) CR+LF MAC JIS CR OS 1 3 OS (LF) (CR) OS unix2dos dos2unix MS- DOS/Win UNIX CR+LF UNIX LF dos2unix file UNIX DOS unix2dos file perl MS-DOS/Win CR+LF Linux LF CR perl -p -e "s/\r//" <dosfile >unixfile LF CR LF perl -p -e "s/\n/\r\n/" <unixfile >dosfile 3

C nkf UNIX file /etc/magic $ file ex0 ex0: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically linked, not stripped $ file ex0.c ex0.c: C program text $ file text00.tex text00.tex: International language text Enter 1.9 nkf UNIX nkf --unix dosfile > unixfile MS-DOS/Win UNIX nkf -OW --unix file 1fefefefefefefefefefefe1 Webmail Linux MS-DOS/Win nkf unix2dos dos2unix 1fefefefefefefefefefefe1 4

C 1 1 1.1 C GNU gcc foo.c bar $ gccfoo.c -o bar -lm -o a.out -lm -Wall -Wstrict-prototypes C C.c.C.cc.cpp c++ Command BASIC 2 source source source compile File object object link execute command Process execute running interpret running 2 Hello Wold! ( i ) 1 Xemacs Emacs ex02.c ( ii ) gcc hello (iii)./hello 1 ex02.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : printf("hello World!\n"); 8 : return (0); 9 : } C 3 main () {...... } main 5,6,8,9 7 printf("...") " \ \n \t column \020 8 \x0d 16 printf % 7,8 ; 5

C 1 1.1.1 Xemacs X gnome-term kterm rxvt etc. *term gcc ( i ) *term $ xemacs & & Xemacs (iii) 1 ( ) (iv) 3 Save ( v ) Xemacs *term eris061: $ C $ $ gcc ex02.c -o hello -lm UNIX OS (vi) ls(list) $ ls hello hello* hello 3 Xemacs ( ii ) Open Find file: ( ex02.c) (vii) hello./ $./hello *term UNIX secure PATH PATH./ PATH 4 Xemacs 1.2 Xemacs X Window System X X 6

C 1 xwnmo kinput2 Xemacs X Mozilla Web Mail X (Free)Wnn CannaSj3 SKK Wnn7 Atok MS-Windows xwnmo Wnn Emacs GNU Emacs Emacs X X X xwnmo Emacs Wnn egg xwnmo egg+wnn xwnmo 2 2 xwnmo Ctrl + \ Ctrl + w Space Ctrl + l Enter Ctrl + o Ctrl + i egg+wnn UNIX Wnn FAQ FAQ Linux (PDF) Linux Magazine 3 ( i ) ( ii ) (iii) 1.2.1 Emacs Emacs Emacs tutorial Emacs 3 3 Emacs Ctrl + g 1 Ctrl + f 1 Ctrl + b Ctrl + p Ctrl + n Ctrl + a Ctrl + e Alt + < Alt + > 1 Ctrl + v 1 Alt + v Ctrl + l Ctrl + d Ctrl + h Ctrl + k Alt + k Ctrl + y Home Page Down Page Up Back Space 7

C 1 4 Emacs tutorial Emacs Ctrl/ESC 5 UNIX 1 \t \v \r 2 q01.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : printf("\ttab\ttab\ttab\ttab\n"); 8 : printf("\tht\vvt\tht\vvt\n"); 9 : printf("cr\rcr\rcr\rcr\r\n"); 10 : 11 : return (0); 12 : } 2 1.3 3 ex06.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : printf("7 + 3 = %d\n", 7 + 3); 8 : printf("7-3 = %d\n", 7-3); 9 : printf("7 * 3 = %d\n", 7 * 3); 10 : printf("7 / 3 = %d\n", 7 / 3); 11 : printf("7 mod 3 = %d\n", 7 % 3); 12 : printf("7 + 3 = %f\n", 7. + 3); 13 : printf("7-3 = %f\n", 7. - 3); 14 : printf("7 * 3 = %e\n", 7. * 3); 15 : printf("7 / 3 = %e\n", 7. / 3); 16 : 17 : return (0); 18 : } %d %f %e int1 % int2 int1 int2 7/3 2 1 7.0 7. 3 3 4 q03.c q03.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : printf("7-3 * 4 = %d\n", 7-3 * 4); 8 : printf("(7-3) * 4 = %d\n", (7-3) * 4); 9 : printf("7 + 3 %% 4 = %d\n", 7 + 3 % 4); 10 : printf("(7 + 3) %% 4 = %d\n", (7 + 3) % 4); 11 : 12 : return (0); 13 : } 6 ex06.c 8

C 1 1fefefefefefefefefefefe1 X KDE GNOME Emacs21 C gcc gcc foo.c -o bar -lm C main #include <stdio.h> #include <stdlib.h> #include <math.h> int main(int argc, char **argv) {... } ; printf ( printf(""); printf(" + ", ); 5 Knoppix-3.1 KDE Emacs21 1fefefefefefefefefefefe1 1CD Linux MS-Windows MS-Windows C Linux C Linux MS-Windows Linux MS-Windows 1CD Linux CD-ROM X 1 KNOPPIX 5 CD-ROM 6 Knoppix-3.1 Xemacs GUI GNU Emacs Ver.21 egg+wnn KNOPPIX Knopper KNOPPIX/Math 9

C 2 2 C [] void void UNIX X Window System X XCreateSimpleWindow() my My 7 1 d do cpu_athlon 386_cpu flag-png US$ _arg1 INTEGER Int (tag) () 2.1 A Z a z 0 9 31 2.1.1 C auto break case char const continue default do double else enum extern float for goto if int long register return short singed sizeof static struct switch typedef union unsigned void volatile while inline C99 restrict C99 Bool C99 Complex C99 Imaginary C99 C ANSI(American National Standards Institute) X3.159 ISO 1990 ISO 1995 1999 1999 C99 C99 2.2 2.2.1 char short int int long int long long int 5 unsinged singed (/usr/include/)limit.h 4 32bit CPU 4 (32bit CPU) size Bool C99 1 0,1 char 1-128 127 0 255 unsigned char 1 0 255 signed char 1-128 127 short 2-32768 32767 unsigned short 2 0 65535 int 4-2147483648 2147483647 unsigned int 4 0 4294967295 long 4-2147483648 2147483647 unsigned long 4 0 4294967295 long long C99 8-9223372036854775808 9223372036854775807 unsigned 8 0 long lomg C99 18446744073709551615 math.h sin cos 1 do, 386 cpu, flag-png, US$ 10

C 2 char 255 char char ascii man ascii 2.2.2 float double long double IEEE754-1985 GCC (/usr/include/)ieee754.h 5 32bit CPU 5 (32bit CPU) size float 4 1.2E-38 3.4E+38 6 double 8 2.3E-308 1.7E+308 15 long double 10 3.4E-4932 1.1E+4932 19 float float CPU double float double C99 C C++ FORTRAN C ANSI C99 complex.h double _Complex x; long double _Complex y=3+4i; 2.2.3 void void void func(...) double func(void) void 8 [ ] 5 ex08.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : double r; 8 : 9 : printf("radius = "); 10 : scanf("%lf", &r); 11 : printf("area = %f\n", M_PI * r * r); 12 : & 13 : return (0); 14 : } ex08 $ gcc ex08.c -o ex08 -lm ex08 $./ex08 radius = 3 area = 28.274334 8 scanf("%lf",&r) &r r C 8 %lf 9 %f f float printf %f double scanf %f double %lf ( ) 10 M_PI π (/usr/include/)math.h 11

C 2 2.3 printf() 6 d, i int 10 o unsigned int 8 u unsigned int 10 x, X unsigned int 16 e, E double 10 f double 10 g, G double f e 1 a, A C99 double 16 2 c char, int int unsigned char s 3 NUL \0 p 16 n int * % % 1 man 3 printf 2 C99 3 char printf () ex08.c 11 printf("area = %f\n", M_PI*r*r); printf() man 3 printf (conversion specification) %[][][. ] %12.8f %20.15g %-8.6e %d %03d %80s %% %n %p %. 6 float 6 7 5 7 0 0-0 + + - # o 8 9 0 X,x 16 0x 0X a,a,e,e,f,g,g ex09u.c 6 ex09.c 4 : 5 : int main(void) 6 : { 7 : int i; 8 : double x; 9 : 10 : printf("integer i = "); 11 : scanf("%d", &i); 12 : printf("double x = "); 13 : scanf("%lf", &x); 14 : printf("\n"); 15 : 16 : printf("%%d\t i = %d\n", i); 17 : printf("%%8d\t i = %8d\n", i); 18 : printf("%%010d\t i = %010d\n", i); 19 : 20 : printf("%%f\t x = %f\n", x); 21 : printf("%%4.3f\t x = %4.3f\n", x); 22 : printf("%%e\t x = %e\n", x); 23 : printf("%%12.8g\t x = %12.8g\n", x); 24 : 25 : return (0); 26 : } 12

C 2 4 2 q04.c 5 q05.c 2.4 main scanf main **argv argv main foo arg1 arg2 arg3... foo argv[0] foo argv[1] arg1 argv[2] arg2... main int argc, char **argv aragc argc +1 10 main 7 ex10.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : double r; 8 : 9 : r = atof(argv[1]); 10 : printf("area = %f\n", M_PI * r * r); 11 : 12 : return (0); 13 : } [ ] ex10.c 9 atof() ascii to float double int atoi() long int atol() $./ex10 3 area = 28.274334 $./ex10 Segmentation fault (core dumped) atof 6 UNIX 7 x y : pow(x,y) 8 M PI math.h 9 3 3 3 a, b, c A Heron A = ( s(s a)(s b)(s c) s = a + b + c ) 2 scanf main **argv 1fefefefefefefefefefefe1 [] unsigned signed float double 1fefefefefefefefefefefe1 13

C 3 3 3.1 C 0 (false) Bool 1 0 (true) 1 3.2 (relational operartor) () (comprison) 1 0 int => =< 3.3 (logical operartor) 9C () 0 1 9 : 1 0 && (x y) (x y)! ( x) x && y x y x y 1 0 x y 1 0!x x 1 0 10 (p q) T F 10 p p q p q p q p q F T T T T T F T F F T F T F T F T T T F F F F T 8 : 1 0 < x < y x y 1 0 <= x <= y x y 1 0 > x > y x y 1 0 >= x >= y x y 1 0 == x == y x y 1 0!= x!= y x y 1 0 == = == = (exclusive OR) A B A B 3.4 if switch 3.4.1 if...else if ( ) [else ] else 14

C 3 { } 11 if... else main x 0 True 0 False 8 ex11.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : double x; 8 : 9 : x = atof(argv[1]); 10 : printf("x = %g --> ", x); 11 : if (x) { 12 : printf("true\n"); 13 : } else { 14 : printf("false\n"); 15 : } 16 : return (0); 17 : } 0 $ gcc -o ex11 ex11.c -lm $./ex11 1.4 x = 1.4 --> True $./ex11 0.0 x = 0 --> False $./ex11 1e-500 x = 0 --> False $./ex11 1e-200 x = 1e-200 --> True $./ex11 a x = 0 --> False 11 15 if (x) printf("true\n"); if (!x) printf("false\n"); 2 if 2 if 1 x x!=0 10 x x 3 True False 2.999...9 9 3 11 P x, y P (2,3) 2 q11.c 12 2 x, y x y x, y 9 ex12.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : double x, y; 8 : 9 : x = atof(argv[1]); 10 : y = atof(argv[2]); 11 : printf("(%4.2f,%4.2f) --> ", x, y); 12 : 13 : if (x > 0) { 14 : if (y > 0) { 15 : printf("1st quadrant\n"); 16 : } else if (y == 0) { 17 : printf("x-axis\n"); 18 : } else 19 : printf("4th quadrant\n"); 20 : } else if (x == 0) { 21 : if (y > 0 y < 0) { 22 : printf("y-axis\n"); 23 : } else 24 : printf("origin\n"); 25 : } else { /* x < 0 */ 26 : if (y > 0) { 27 : printf("2nd quadrant\n"); 28 : } else if (y == 0) { 29 : printf("x-axis\n"); 30 : } else 31 : printf("3rd quadrant\n"); 32 : } 33 : return (0); 34 : } [ ] ex12.c ex12u.c 15

C 3 $./ex12 1 0 (1.00,0.00) --> x-axis $./ex12 0 0 (0.00,0.00) --> origin $./ex12-2 -3 (-2.00,-3.00) --> 3rd quadrant [ ] ex13.c argv[1][0] argv[1] 1 8 inputc scanf(" %c", &c) scanf 1 "%c" 3.5 switch...case if... else switch... case switch () { case 1 : 1 [break;] case 2 : 2... default : d } ; : case default: case break; 13 a, A alive d, D dead other switch... case 10 ex13.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : char inputc; 8 : inputc = argv[1][0]; 9 : 10 : switch (inputc) { 11 : case A : break; 12 : case a : 13 : printf("alive\n"); 14 : break; 15 : case D : break; 16 : case d : 17 : printf("dead\n"); 18 : break; 19 : default: 20 : printf("other\n"); 21 : break; 22 : } 23 : return (0); 24 : } %c " %c" $./ex13 D dead $./ex13 a alive $./ex13 p other 12 if... else q12.c 13 1 2 x, y k (x + y)/2 j xy b q13.c 1 (charcter constant) int A 10 65 16 0x41 2 GCC abc sizeof 11 notes3-5.c 4 : 5 : int main(void) 6 : { 7 : int s = abc ; 8 : printf("size = %d\n", sizeof(s)); 9 : printf("%#x\t%d\n", s, s); 10 : return (0); 11 : } size 4 (long) int 16

C 3 1byte $ gcc -o charconst -Wall charconst.c charconst.c: In function main : charconst.c:7: warning: multi-character character constant $./charconst size = 4 0x616263 6382179 3.6 if... else a? b : c a b c a c b if... else b c 14 12 ex14.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i = atoi(argv[1]), n; 8 : 9 : printf("%d --> ", i); 10 : n = i? printf("true\n") : printf("false\n"); 11 : printf("printed characters = %d\n", n); 12 : 13 : return (0); 14 : } printf() True \n 5 6 $./ex14 12 12 --> True printed characters = 5 $./ex14 0 0 --> False printed characters = 6 15 3 3 2 16 x y P(x, y) OAB O A B (0,0) (1,0) (0,2) 17 ax 2 + bx + c = 0 q17.c 1fefefefefefefefefefefe1 > >= < <= ==!=! && 0 0 1 3 if... [else] switch... case a? b : c switch... case case 1fefefefefefefefefefefe1 14 2 if... else 17

C 4 LOOP 4 loop 4 4.1 while 4.1.1 while +... while () 15 imax 10 imax i i 2 13 ex15.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i, imax = atoi(argv[1]); 8 : i 9 : i = 10; 10 : while (i <= imax) { 11 : printf("i = %3d\t i**2 = %5d\n", i, i * i); 12 : i++; 13 : } 14 : 15 : return (0); 16 : } i [ ] while ex15.c 10 11 12 10 11... 10 12 i++ ++ i 1 increment operator 1 -- decrement operator 11 * x * y xy / x / y x/y + x + y x + y - x - y x y % x % y x mod y x,y ++ ++x, x++ x 1 -- --x, x-- x 1 postfix notation prefix notation 1 ++i --ii+1 i-1 i++ i--i i 1 (simple assignment operator) i = i+1; (compound assignment operator) i += 1; $./ex15 100 i = 10 iˆ2 = 100 i = 11 iˆ2 = 121 i = 12 iˆ2 = 144... i = 99 iˆ2 = 9801 i = 100 iˆ2 = 10000 $./ex15 9 $ 18

C 4 LOOP 10 1 18 i=10 i++ /* */ 4.1.2 do...while do... while while do while (); + 1 16 14 ex16.c do... while 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i, imax = atoi(argv[1]); 8 : 9 : i = 10; 10 : do { 11 : printf("i = %3d\t i**2 = %5d\n", i, i * i); 12 : i++; 13 : } while (i <= imax); 14 : 15 : return (0); 16 : } 1 10 i=10 do... while $./ex16 9 i = 10 iˆ2 = 100 4.2 for for 3 for (; ; ) while 1... 17 15 ex17.c while do... whlie for 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i, imax; 8 : 9 : imax = atoi(argv[1]); 10 : for (i = 10; i <= imax; i++) { 11 : printf("i = %3d\t i**2 = %5d\n", i, i * i); 12 : } 13 : return (0); 14 : } [ ] ex17.c 9 4.2.1 for 18 0 127 8 16 10 16 100(x64) 16 ex18.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i, j, n; 8 : 9 : for (i = 0; i < 16; i++) { 10 : for (j = 0; j < 8; j++) { 11 : n = 8 * i + j; 12 : printf("%3d(x%02x) ", n, n); 13 : } 14 : printf("\n"); 15 : } 16 : 17 : return (0); 18 : } 19

C 4 LOOP 19 q19.c 4.3 C break continue goto return break switch... case switch... case return ( ) 4.3.1 break break switch... case while () { }... break;...... switch () { }... break;...... do... while for 19 q Q 17 ex19.c 4 : 5 : int main(void) 6 : { 7 : char c; 8 : 9 : while (1) { 10 : printf("input a character "); 11 : scanf(" %c", &c); 12 : if (c == q c == Q ) { 13 : break; 14 : } 15 : printf("%c\n", c); 16 : } 17 : printf("now quit\n"); 18 : return (0); 19 : } [ ] ex19.c $./ex19 Input a character a a Input a character 1 1 Input a character Q Now quit $ [] ex19b.c break while = 10, 11, 12, 9 q, Q 18 ex19b.c 4 : 5 : int main(void) 6 : { 7 : char c; 8 : 9 : while (c!= q && c!= Q ) { 10 : printf("input a charactor "); 11 : scanf(" %c", &c); 12 : printf("%c\n", c); 13 : } 14 : printf("now quit\n"); 15 : return (0); 16 : } A B!A &&!B 9 $./ex19b Input a charactor Q Q Q Now quit $ 20 while switch... case while q20.c 20

C 4 LOOP 4.3.2 continue continue while for while () { }... continue;...... 20 do {... continue;... } while () ;... break c C continue 19 ex20.c 4 : 5 : int main(void) 6 : { 7 : char c; 8 : 9 : while (1) { 10 : printf("input a charactor "); 11 : scanf(" %c", &c); 12 : if (c == c c == C ) { 13 : continue; 14 : } else if (c == q c == Q ) { 15 : break; 16 : } 17 : printf("%c\n", c); 18 : } 19 : printf("now quit\n"); 20 : return (0); 21 : } [ ] ex20.c 4.3.3 goto goto goto 21 continue if... else swicth... case break while goto 20 ex21.c 4 : 5 : int main(void) 6 : { 7 : char c; 8 : 9 : while (1) { 10 : printf("input a charactor "); 11 : scanf(" %c", &c); 12 : switch (c) { 13 : case c : 14 : case C : break; 15 : 16 : case q : 17 : case Q : printf("now quit\n"); 18 : goto QUIT; 19 : 20 : default : printf("%c\n", c); 21 : break; 22 : } 23 : } 24 : QUIT: 25 : return (0); 26 : } [ ] ex21.c 4.3.4 return return return ; retrun value ( ) C 4.4 21

C 4 LOOP q22.c 22 1e-5 (1 10 5 ) 0 1 21 ex22.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i = 0, lmax; 8 : double dd = 1e-5, s, id; 9 : 10 : printf("dd = %.15f\n", dd); 11 : printf("lmax = %d\n", lmax = 1 / dd); 12 : for (s = 0; s <= 1.0; s += dd) { 13 : id = i * dd; 14 : if (i > lmax - 3) { 15 : printf("%6d, %.15f, %.15f\n", i, s, id); 16 : } 17 : i++; 18 : } 19 : return (0); 20 : } 1fefefefefefefefefefefe1 (loop) 3 while () do while (); for (; ; ) break, continue, goto 1fefefefefefefefefefefe1 $./ex22 dd = 0.000010000000000 lmax = 99999 99997, 0.999969999998084, 0.999970000000000 99998, 0.999979999998084, 0.999980000000000 99999, 0.999989999998084, 0.999990000000000 100000, 0.999999999998084, 1.000000000000000 1/(1e-5) 100000 1e-5 100000 1 1 1e-5 n ne-5 (n 10 5 ) 21 1 n 2 n k=1 k 2 = 1 n(n + 1)(2n + 1) 6 q21.c 22 e x e x x k = c k (x) = k! = 1 + x + x2 2! + x3 3! + k=0 k=0 x c k (x) < 1e-15 (1.0 10 15 ) exp(x) 22

C 5 5 array []; double x[128]; double 128 x[0], x[1], x[2], x[3]... subscript [ ] subscript operator 0 i [i-1] N N 1 N int N = 5; double x[n]; ANSI C99 GCC ANSI C99 5.1 = x[0] = 0.12; x[2] = 2*x[0] + x[1]; x[k] = x[i]*x[j]; double ax[50], ay[50]; ax = ay; 5.2 double x[3] = {0.1, 0.3, 0.5}; int kx[4] = {1, 2, 3, 4}; char str[6] = { H, e, l, l, o }; 0 GCC char 1 double x[2]; x[3] = {0.1, 0.2}; x = {0.1, 0.2}; 5.2.1 double x[] = {0.1, 0.3, 0.5}; int kx[] = {1, 2, 3, 4, 5, 6}; char str[] = { W, o, r, l, d }; kx 6 6 23

C 5 23 0 9 16 0 RAND_MAX rand() srand(seed) 22 ex23.c 4 : #include <time.h> 5 : 6 : int main(int argc, char **argv) 7 : { 8 : int i, xi[16]; 9 : 10 : srand(time(null)); 11 : for (i = 0; i < 16; i++) { < 16 12 : xi[i] = rand() % 10; 13 : printf("xi[%2d] = %d\n", i, xi[i]); 14 : } 15 : return (0); 16 : } 24 5.3 matrix2 M N A A[M][N] i, j A[i][j] A[M][N] N M A[i] i i + 1 N A[i][j] = (A[i])[j] i i + 1 j j + 1 24 0 N 2 1 N 23 ex24.c [ ] ex23.c 11 rand() % 0 9 srand(seed) seed srand() 9 1970 1 1 00:00:00 UTC time(null) NULL $./ex23 xi[ 0] = 9 xi[ 1] = 9 xi[ 2] = 0... xi[15] = 2 $./ex23 xi[ 0] = 6 xi[ 1] = 3 xi[ 2] = 8... xi[15] = 5 23 4 : #include <time.h> 5 : 6 : #define N 4 7 : 8 : int main(int argc, char **argv) 9 : { 10 : int i, j, n, A[N][N]; 11 : 12 : srand(time(null)); 13 : for (i = 0; i < N; i++) { 14 : for (j = 0; j < N; j++) { 15 : A[i][j] = -1; 16 : } 17 : } 18 : 19 : for (n = 0; n < N * N; n++) 20 : while (1) { 21 : i = rand() % N; 22 : j = rand() % N; 23 : if (A[i][j] == -1) { 24 : A[i][j] = n; 25 : break; 26 : } 27 : } 28 : 29 : for (i = 0; i < N; i++) { 30 : for (j = 0; j < N; j++) 31 : printf("%2d ", A[i][j]); 32 : printf("\n"); 33 : } 34 : 35 : return (0); 36 : } [ ] ex24.c 6 N #define N 4 N 4 # #include 24

C 5-1 20 26-1 -1 0 N 2 1 30 1 ex24u.c N = 4 0 15 $./ex24 9 0 14 3 6 13 15 10 12 4 8 11 2 5 1 7 $./ex24 9 5 10 0 8 2 12 7 1 6 14 15 11 4 3 13 25 q25.c 5.4 1 N M 2 NM 1 1 1 25 9 3 3 [ ] ex25.c 15,20 &A[i][j] & A[i][j] address: scanf() %p &x 16 (long) int 4 4 ex25.c 24 ex25.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i, j; 8 : int B[3][3] = 9 : { 10 : {8, 7, 6}, 11 : {5, 4, 3}, 12 : {2, 1, 0}}; 13 : int A[9] = 14 : {8, 7, 6, 5, 4, 3, 2, 1, 0}; 15 : 16 : 17 : printf("one dimensional array\n"); 18 : for (i = 0; i < 3; i++) 19 : for (j = 0; j < 3; j++) 20 : printf("%d ==> %p\n", A[3 * i + j], &A[3 * i + j]); 21 : 22 : printf("two dimensional array\n"); 23 : for (i = 0; i < 3; i++) 24 : for (j = 0; j < 3; j++) 25 : printf("%d ==> %p\n", B[i][j], &B[i][j]); 26 : 27 : return (0); 28 : } $./ex25 One dimensional array 8 ==> 0xbffff94c 7 ==> 0xbffff950 6 ==> 0xbffff954 5 ==> 0xbffff958 4 ==> 0xbffff95c 3 ==> 0xbffff960 2 ==> 0xbffff964 1 ==> 0xbffff968 0 ==> 0xbffff96c Two dimensional array 8 ==> 0xbffff970 7 ==> 0xbffff974 6 ==> 0xbffff978 5 ==> 0xbffff97c 4 ==> 0xbffff980 3 ==> 0xbffff984 2 ==> 0xbffff988 1 ==> 0xbffff98c 0 ==> 0xbffff990 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] 8 7 6 5 4 3 2 1 0 B[0][0] B[0][1] B[0][2] B[1][0] B[1][1] B[1][2] B[2][0] B[2][1] B[2][2] 5.5 5.5.1 p.110 25

C 5 26 USHRT_MAX n n 25 ex26.c 4 : #include <limits.h> 5 : 6 : int main(int argc, char **argv) 7 : { 8 : int i, n, flag ; 9 : int prime[ushrt_max] = {2,3}, ptr = 2; 10 : 11 : for (n = 5; n < USHRT_MAX ; n += 2) { 12 : flag = 1; default 13 : for (i = 1; prime[i]*prime[i] <= n; i++) { 14 : if (n % prime[i] == 0){ 15 : flag = 0; 16 : break; for 17 : } 18 : } 19 : if (flag) { 20 : prime[ptr] = n; 21 : ptr++; 22 : } 23 : } 24 : 25 : for (i = 0; i < ptr; i++) { 26 : printf("%5d, ",prime[i]); 27 : } 28 : printf("\n%d\n", ptr); 29 : return (0); 30 : } [ ] ex26.c unsigned short int USHRT_INT limits.h 32bit CPU GCC 65,535 (=1) flag 12 flag =015 19 flag ex26u.c 11 n += 2 (compound assignment operator) n + n 2 11 op x op= y x op= y x = x op y x += 1 x = x + 1 x /= a 5.5.2 x = x / a i %= 8 i = i % 8 x [k, k + ] h[k] x 27 [0,1) drand48() ( 0.5) R N = r 1 + r 2 + + r 12 12 r i [0, 1) 26 ex27.c 4 : #include <time.h> 5 : 6 : #define N 20 7 : 8 : int main(int argc, char **argv) 9 : { 10 : int i, imax, j, xi[n + 1] = 11 : {0}; 12 : double s; 13 : 14 : imax = atoi(argv[1]); 15 : srand48(time(null)); drand48() 16 : for (i = 0; i < imax; i++) { 17 : s = 0; 18 : for (j = 0; j < 12; j++) { 19 : s += drand48(); 20 : } 21 : s /= 12; 22 : xi[(int) floor(s * N)] += 1; 23 : } 24 : for (i = 0; i < N; i++) { 25 : printf("[%.2f:%.2f] = %6d\n", 26 : (i + 0.0) / N, (i + 1.0) / N, xi[i]); 27 : } 28 : return (0); 29 : } [ ] ex27.c 26

C 5 21 (int).. (cast operator) (int) C 25 (i+0.0)/n i/n / (double)i/n ex27.c $./ex27 1000000 [0.00:0.05] = 0 [0.05:0.10] = 0 [0.10:0.15] = 4 [0.15:0.20] = 83 [0.20:0.25] = 889 [0.25:0.30] = 6686 [0.30:0.35] = 28340 [0.35:0.40] = 80972 [0.40:0.45] = 159473 [0.45:0.50] = 223680 [0.50:0.55] = 223799 [0.55:0.60] = 159870 [0.60:0.65] = 80496 [0.65:0.70] = 28190 [0.70:0.75] = 6549 [0.75:0.80] = 901 [0.80:0.85] = 66 [0.85:0.90] = 2 [0.90:0.95] = 0 [0.95:1.00] = 0 drand48 : GCC drand48 2 48 drand48 rand 26 * 1 50 q26.c $./q26 300000 [0.00:0.05] = [0.05:0.10] = [0.10:0.15] = [0.15:0.20] = [0.20:0.25] = [0.25:0.30] = * [0.30:0.35] = ****** [0.35:0.40] = ***************** [0.40:0.45] = *********************************** [0.45:0.50] = ************************************************** [0.50:0.55] = ************************************************* [0.55:0.60] = *********************************** [0.60:0.65] = ***************** [0.65:0.70] = ****** [0.70:0.75] = * [0.75:0.80] = [0.80:0.85] = [0.85:0.90] = [0.90:0.95] = [0.95:1.00] = 1fefefefefefefefefefefe1 double x[50]; x[1] = 1.0; double x[2]={2e-3, 0.1}; int dd[]={3, 4, 5, 9} double A[6][7]; int C[2][3][3]; 1fefefefefefefefefefefe1 man 3 rand Mersenne Twister Home Page 27

C 6 6 C main library function inline C99 inline C90 6.1 void () () void double funcz(double) void funcv(char, double, double) funcz double 1 double funcv char 1 double 2 () { return ; void } 6.2 printf atof atof () pass by value double funcz(double z) double funcz(double z) { z z } int main(void) {... funcz(1.2); 1.2 z... x = 2.0; funcz(x); x 2.0 z funcz(x*x); x*x 4.0 z... } 28 2 main [ ] ex28.c 15 main exit() argv[0] 7,8 return 28

C 6 $./ex28-61 2.4-61 2.4 2.4 $./ex28-61./a.out x y 27 ex28.c 4 : 5 : double bigone(double s, double t) 6 : { 7 : if (s >= t) 8 : return (s); 9 : else 10 : return (t); 11 : } 12 : 13 : int main(int argc, char **argv) 14 : { 15 : double x, y; 16 : 17 : if (argc < 3) { 18 : printf(" %s x y\n", argv[0]); 19 : exit(0); 20 : } 21 : x = atof(argv[1]); 22 : y = atof(argv[2]); 23 : printf(" %g %g ", x, y); 24 : printf(" %g \n", bigone(x, y)); 25 : 26 : return (0); 27 : } 27 main q27.c 6.3 29 kterm main kterm xterm \E[H\E[2J \E ASCII ESC 10 27 16 0x1b [ ] ex29.c clr main infocmp clear=xxx XXX man terminfo libcurses Linux libncurses 28 ex29.c 4 : 5 : void clr(void) 6 : { 7 : char clear[8] = 8 : {27, [, H, 27, [, 2, J }; 9 : printf("%s", clear); 10 : } 11 : 12 : int main(int argc, char **argv) 13 : { 14 : clr(); 15 : return (0); 16 : } 28 n - n q28.c 6.4 scanf() & v &v return 12 29

C 6 12 6.4.1 x x &x A A[ ] A &A f f( ) f &f A[ ] A 1 &A[0] 30 main ex30u.c [ ] ex30.c maxof() 29 ex30.c 4 : #include <time.h> 5 : 6 : #define N 8 7 : 8 : int maxof(int a[], int imax) 9 : { 10 : int i, max = a[0]; 11 : 12 : for (i = 0; i < imax; i++) { 13 : if (a[i] >= max) 14 : max = a[i]; 15 : } 16 : return (max); 17 : } 18 : 19 : int main(int argc, char **argv) 20 : { 21 : int i, a[n]; 22 : 23 : srand(time(null)); 24 : for (i = 0; i < N; i++) { 25 : a[i] = rand() % 10000; 26 : if (i < N - 1) { 27 : printf("%4d,", a[i]); 28 : } else 29 : printf("%d\n", a[i]); 30 : } 31 : 32 : printf(" %d \n", maxof(a, N)); 33 : 34 : return (0); 35 : } $./ex30 8891, 1820, 2778, 4315, 676, 3435, 6270, 7928 8891 6.4.2 z &z double *x x x 31 1.0 main [ ] ex31.c 30 ex31.c 4 : 5 : void one(double *x) 6 : { 7 : *x = 1.0; 8 : } 9 : 10 : int main(int argc, char **argv) 11 : { 12 : double z = 10.0; 13 : 14 : printf("z = %f\n", z); 15 : one(&z); 16 : printf("z = %f\n", z); 17 : 18 : return (0); 19 : } 5 7 x main z x = &z z *x = *(&z) = z 15 one() z &z * * ptr *ptr ptr 30

C 6 x &x x *&x = x x *x 29 2 q29.c 30 main q30.c max, min void maxminof(int a[], int imax, int *max int *min) 6.5 recursive 32 [ ] ex32.c 31 ex32.c 4 : 5 : double fact(double x) 6 : { 7 : if (x > 1) { 8 : return (x * fact(x - 1)); 9 : } else { 10 : return (x); 11 : } 12 : } 13 : 14 : int main(int argc, char **argv) 15 : { 16 : int i; 17 : 18 : for (i = 1; i <= 25; i++) { 19 : printf("fact(%2d) = %.0f\n", i, fact(i)); 20 : } 21 : 22 : return (0); 23 : } $./ex32 fact( 1) = 1 fact( 2) = 2 fact( 3) = 6 fact( 4) = 24 fact( 5) = 120 fact( 6) = 720 fact( 7) = 5040...... fact(19) = 121645100408832000 fact(20) = 2432902008176640000 fact(21) = 51090942171709440000 fact(22) = 1124000727777607680000 fact(23) = 25852016738884978212864 fact(24) = 620448401733239409999872 fact(25) = 15511210043330986055303168 23 16 16 long double 20 31 n Stiring log n! n log n n q31.c gp 1000000! 33 n log(n!) n log n n 1000 5912 5907 10000 82109 82103 100000 1051299 1051292 f (x) [a, b] x i x i+1 f (x i ) 7 7 x i+1 x i = h = (b a)/n b a n 1 f (x) dx = h f (x i ) i=0 31

C 6 cos(x) [a, b] a, b n a = 0 b = x sin(x) [ ] ex33.c 32 ex33.c 4 : 5 : void hline(int len) 6 : { 7 : for (; len > 0; len--) 8 : printf("-"); 9 : printf("\n"); 10 : } 11 : 12 : double integ_cos(double a, double b, int n) 13 : { 14 : int i; 15 : double sum = 0, dx = (b - a) / n; 16 : 17 : for (i = 0; i < n; i++) 18 : sum += cos(a + i * dx) * dx; 19 : return (sum); 20 : } 21 : 22 : int main(int argc, char **argv) 23 : { 24 : int i, N = atoi(argv[1]); 25 : double x, numeric, err; 26 : 27 : hline(45); 28 : printf("x\t\tinteg[cos(x)]\terror\n"); 29 : hline(45); 30 : for (i = 0; i <= 32; i++) { 31 : x = 2 * M_PI / 32 * i; 32 : numeric = integ_cos(0, x, N); 33 : err = numeric / sin(x) - 1; 34 : printf("%.6f\t%+.6f\t%+.4e\n", x, numeric, err); 35 : } 36 : hline(45); 37 : 38 : return (0); 39 : } 14 22 integ_cos() line() 5 12 26 N main 35 = ( )/ x = 0, π, 2π sin(x) = 0 ex33u.c $./ex33 10000 --------------------------------------------- x int[cos(x)] error --------------------------------------------- 0.000000 +0.000000 +nan 0.196350 +0.195091 +9.6690e-07 0.392699 +0.382685 +3.9055e-06...... 2.945243 +0.195382 +1.4952e-03 3.141593 +0.000314 +2.5654e+12 3.337942-0.194760-1.6945e-03 3.534292-0.382343-8.8841e-04...... 5.890486-0.382661-5.8613e-05 6.086836-0.195084-3.0006e-05 6.283185-0.000000 +7.1605e-01 --------------------------------------------- 32 x i x i + h/2 q32.c 33 f (x) d f dx = lim f (x + h) f (x) h 0 h h x 3 dx3 dx = 3x2 6.6 C GNU GCC GNU C99 GCC /usr/include/math.h /usr/include/bits/mathcall.h man C99 USE_ISOC99 GNU USE_GNU #define USE_ISOC99 1 #define USE_GNU 1 #include <math.h> 32

C 6 15 double GCC 13 int C rand() 0 RAND_MAX srand(seed) rand( ) seed unsignedint void abs(n) n div(n,m) n/m div_t quot rem int random() rand() BSD4.3 srandom(seed) random() BSD4.3 C rint(x) C99 copysign(x,y) C99 drem(x,y) drand48() srand48() j0(x), j1(x), jn(n,x) y0(x), y1(x), yn(n,x) asinh(x) C99 acosh(x) C99 atanh(x) C99 x y x y [0.0, 1.0) 1 0 1 n 2 0 1 n arcsinh(x), arccosh(x) arctanh(x) cbrt(x) C99, hypot(x,y) C99 3 x, (x 2 + y 2 ) 1/2 expm1(x) C99, log1p(x) C99 e x 1, log(x + 1) lgamma(x) C99 log Γ(x) erf(x) C99 erfc(x) C99 Erf(x) 2 x e t2 dt π Erfc(x) 1 Erf(x) 0 1fefefefefefefefefefefe1 14 double C sin(x),cos(x),tan(x) sin(x), cos(x), tan(x) asin(x),acos(x) arccos(x), arcsin(x) atan(x), atan2(y, x) arctan(x), arctan(y/x) sinh(x),cosh(x),tanh(x) sinh(x), cosh(x), tanh(x) pow(x,y), sqrt(x) x y, x exp(x) frexp(x, *n) ldexp(x, n) log(x), log10(x) ceil(x), floor(x) fabs(x) fmod(x,y) modf(x,*y) e x y, n 2 n y = x ( 1 2 y < 1) 2 n x n log e (x) log 10 (x) x, x x x y x, y x 1fefefefefefefefefefefe1 33

C 7 7 C int double C 7.1 char char \0 \0 char abc[4]; char message[5] = { G, o, o, d, \0 }; char greeting[] = "Hello!" ; N char N + 1 greeting 6(Hello!)+1( \0 ) = 7 char abc a[0]= a ; a[1]= b ; a[2]= c ; a[3]= \0 ; 1 34 a[0] a[1] a[2] a[3] a b c \0 [ ] ex34.c 33 ex34.c 3 : 4 : int main(int argc, char **argv) 5 : { 6 : int i, len; 7 : char str[] = "A quick brown fox jumps" 8 : " over the lazy dog"; 9 : 10 : printf("str = %s \n", str); 11 : len = sizeof(str) - 1; 12 : 13 : for (i = 0; i < len; i++) { 14 : printf("[%c]", str[i]); 15 : } 16 : printf("\n"); 17 : 18 : str[10] = \0 ; 19 : printf("str = %s \n", str); 20 : str[10] = ; 21 : printf("str = %s \n", str); 22 : str[len] = + ; 23 : printf("str = %s \n", str); 24 : 25 : return (0); 26 : } 7,8 2 C 1 10 printf %s char 11 char sizeof char 1-1 ( \0 ) 13 15 1 [ ] 18 printf %s 22 printf $./ex34 str = A quick brown fox jumps over a lazy dog. [A][ ][q][u][i][c][k][ ][b][r][o][w][n][ ]... str = A quick br str = A quick br wn fox jumps over a lazy dog. @( = A quick br wn fox jumps over a lazy dog.+ 34

C 7 A quick brown... pangram... 1 7.2 7.2.1 \0 1 int length i = 0; while (A[i]!= \0 ){ } i++; length = i; strlen() $ man strlen STRLEN(3) Linux Programmer s Manual STRLEN(3) strlen - #include <string.h> size_t strlen(const char *s); strlen() s \0 strlen() s SVID 3, POSIX, BSD 4.3, ISO 9899 string(3) April 12, 1993 1 size_t unsigned int string man strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy, strncasecmp, strpbrk, strrchr, strsep, strspn, strstr, strtok, strxfrm, index, rindex 7.2.2 copy duplicate source destination dest char dest[128] = { \0 }; char src[] = "source strings";... i = 0; while ( src[i]!= \0 ) { } dest[i] = src[i]; i++; dest[i] = \0 ;... strcpy() strncpy() man strcpy char *strcpy(char *dest, const char *src); char *strncpy(char *dest, const char *src, size_t n); A[ ] A dest src 7.2.3 compare man ascii ascii 35

C 7 A z char [A-Z][a-z] s1 s2 i = 0; while (s1[i] == s2[i]) { } if (s1[i] == \0 ) break; i++; result = (int)s1[i] - (int)s2[i]; result result 0 0, 0 s1 s2 ascii C char ctype.h include isalpha(c) islower(c) isupper(c) isdigit(c) isxdigit(c) isalnum(c) isprint(c) isgraph(c) isspace(c) ispunct(c) iscntrl(c) inblank(c) C99 isascii(c) 16 10 16 10! #$ 7bit ASCII int true/false c char int BSD SVID GCC 17 / ( ) 35 strlen(), strcpy(), 17 / tolower(c) toupper(c) strcmp() mystrlen(), mystrcpy(), mystrcmp() [ ] ex35u.c 34 ex35.c 2 : #include <string.h> 3 : 4 : int mystrlen(char a[]) 5 : { 6 : int i = 0; 7 : 8 : while (a[i]!= \0 ) { 9 : i++; 10 : } 11 : return i; 12 : } 13 : 14 : int mystrcmp(char s1[], char s2[]) 15 : { 16 : int i = 0; 17 : 18 : while (s1[i] == s2[i]) { 19 : if (s1[i] == \0 ) 20 : break; 21 : i++; 22 : } 23 : return ((int) s1[i] - (int) s2[i]); 24 : } 25 : 26 : void mystrcpy(char dest[], char src[]) 27 : { 28 : int i = 0; 29 : 30 : while (src[i]!= \0 ) { 31 : dest[i] = src[i]; 32 : i++; 33 : } 34 : dest[i] = \0 ; 35 : } 36 : 37 : int main(void) 38 : { 39 : char src[] = "Dog as a devil deified," 40 : " lived as a god"; 41 : char cmp[] = "Dog as a devil deified,"; 42 : char dest[80] = 43 : { \0 }; 44 : 45 : printf("%d, %d\n", mystrlen(cmp), strlen(cmp)); 46 : printf(" %s \n", dest); 47 : mystrcpy(dest, src); 48 : printf(" %s \n", dest); 49 : 50 : printf("%d, %d\n", mystrcmp(cmp, src), 51 : strcmp(cmp, src)); 52 : 53 : return (0); 54 : } 36

C 7 my $./ex35 23, 23 Dog as a devil deified, lived as a god -32, -32 0 (10) 32 (10) 34 strcmp() strcasecmp() mystrcasecmp() q34.c 7.3 scanf() 1 getchar() putchar() 36 1 getchar(), putchar() BEL= \a [ ] ex36.c 35 ex36.c 7.2.4 concatenate $a = " " C $b = "" $c = $a+$b C C strcat() cat concatenate char *strcat(char *dest, const char *src); dest src dest \0 src dest src 35 mystrcat() strcat() q35.c 7.2.5 1 strchr, strrchr, index, rindex 1 1 strpbrk, strstr 2 : 3 : #define QUIT \a /* ascii BEL */ 4 : 5 : int main(void) 6 : { 7 : int ch; 8 : 9 : while ((ch = getchar())!= QUIT) { 10 : putchar(ch); 11 : } 12 : 13 : return (0); 14 : } 9 getchar() 1 unsigned char int int ch 10 putchar(ch) BEL= \a kterm Ctrl+G while $./ex36 abcdefghijklm!"#$%& () abcdefghijklm!"#$%& () ˆG BEL $ 36? q36.c 7.4 \n EOF (end of file) gets() puts() fgets(),fputs() 37

C 7 gets() fgets() man fgets() char *fgets(char *s, int size, FILE *stream); size - 1 FILE *stream stdin 37 fgets() 80 [ ] ex37.c fgets() 1fefefefefefefefefefefe1 C char \0 \0 char 1fefefefefefefefefefefe1 36 ex37.c 3 : #include <string.h> 4 : 5 : #define BUFSIZE 81 6 : 7 : int main(void) 8 : { 9 : int len, i; 10 : char buf[bufsize]; 11 : 12 : while (fgets(buf, BUFSIZE, stdin)!= NULL) { 13 : len = strlen(buf); 14 : for (i = len - 2; i >= 0; i--) { 15 : putchar(buf[i]); 16 : } 17 : putchar( \n ); 18 : } 19 : 20 : return (0); 21 : } stdin fgets() \n 14 (len-1)-1 $./ex37 Dog as a devil deified, lived as a god dog a sa devil,deified lived a sa god Madam I m adam mada m I madam $ Ctrl + d Ctrl + d Ctrl 37 + c q37.c 38

C 8 8 C C 18 18 x x &x &x x a a[ ] &a &a a f f( ) &f &f f a a[ ] a (=&a[0]) a (=&a[0]) a[0] p *p &p p A (*A)[ ] &A A F (*F)( ) &F F &x *(&x) = x 8.2 & * P O * *P O *P alias P 0x... 0x1234 O 0x1234 O *P = *0x1234 = 0x1234 = O *address = address 8.2.1 *, [ ], ( ) 3 * ( ), [ ] ( ) (*A)[] (*f)() *A[] *f() 8.1 & double x, y, a[10]; &x = &y; &x = &a[1]; &a[1] = &x; a = &x; 8.3 a *a a[0] &a a *&a *a f(); f &f * 39

C 8 8.3.1 sizeof char a[10]; char 1 sizeof(a) 10 8.4 OS Segmentation Fault double *px; double *px = 1.0; *px = 1.0; *px = 1.0; px px &px px * double rx; rx double *px; px = &rx; px rx *px = 1.0; *px(=rx) 38 18 (i) (ii) alias [ ] ex38.c 37 ex38.c 4 : 5 : double f(double x) 6 : { 7 : return (x * 100.0); 8 : } 9 : 10 : int main(int argc, char **argv) 11 : { 12 : double x = 1, a[3] = 13 : {10, 20, 30}, f(); 14 : double *px, (*pa)[3], (*pf) (); 15 : 16 : printf("&x = %p, *(&x) = %f\n", &x, *(&x)); 17 : printf("&a = %p, *(&a) = %p\n", &a, *(&a)); 18 : printf("&f = %p, *(&f) = %p\n", &f, *(&f)); 19 : printf("a = %p, *a = a[0] = %f\n", a, a[0]); 20 : px = &x; 21 : pa = &a; 22 : pf = &f; 23 : printf("px = %p, *px = %f\n", px, *px); 24 : printf("pa = %p, *pa = %p\n", pa, *pa); 25 : printf("pf = %p, *pf = %p\n", pf, *pf); 26 : 27 : printf("(*pf)(3.1) = %f\n", (*pf) (3.1)); 28 : printf("(*pa)[1] = %f\n", (*pa)[1]); 29 : 30 : return (0); 31 : } px, pa, pf x, a, f *px = x, (*pa) = a, (*f) = f $./ex38 &x = 0xbffff9a4, *(&x) = 1.000000 &a = 0xbffff98c, *(&a) = 0xbffff98c &f = 0x80483f0, *(&f) = 0x80483f0 a = 0xbffff98c, *a = a[0] = 10.000000 px = 0xbffff9a4, *px = 1.000000 pa = 0xbffff98c, *pa = 0xbffff98c pf = 0x80483f0, *pf = 0x80483f0 (*pf)(3.1) = 310.000000 (*pa)[1] = 20.000000 double rx; rx 8.4.1 double *px = &rx; *px = 1.0; *px(=rx) C 40

C 8 Fortran 7 39 4 N N #define [ ] ex39.c ex39u.c 38 ex39.c 3 : #include <time.h> 4 : 5 : #define N 32 6 : 7 : void stat(int a[], int n, int *max, 8 : int *min, double *av) 9 : { 10 : int i, sum = 0; 11 : 12 : *max = *min = a[0]; 13 : for (i = 0; i < n; i++) { 14 : if (a[i] >= *max) 15 : *max = a[i]; 16 : if (a[i] <= *min) 17 : *min = a[i]; 18 : sum += a[i]; 19 : } 20 : *av = (double) sum / n; 21 : } 22 : 23 : int main(int argc, char **argv) 24 : { 25 : int i, a[n], max, min; 26 : double average; 27 : 28 : srand(time(null)); 29 : for (i = 0; i < N; i++) { 30 : a[i] = rand() % 10000; 31 : printf("%4d, ", a[i]); 32 : if (i % 8 == 7) 33 : printf("\n"); 34 : } 35 : 36 : stat(a, N, &max, &min, &average); 37 : printf(" %4d ", max); 38 : printf(" %4d \n", min); 39 : printf(" %.2f \n", average); 40 : 41 : return (0); 42 : } $./ex39 3431, 5071, 7164, 9095, 1030, 737, 3476, 6708, 440, 8053, 8050, 5000, 4735, 262, 3360, 1014, 8256, 3403, 5090, 3094, 2355, 3828, 4471, 2290, 9320, 9938, 9885, 6550, 4653, 207, 1865, 8085, 9938 207 4716.12 8.5 p p+1 p+2, p+3,... char 1 long int 4 double 8 *(p+1), *(p+2), *(p+3),... a[] a a[0] a+i a[i] &a[i] a+i = &a[i] *(a+i) = *(&a[i]) = a[i] a[i] (*(a+(i))) GCC CPU C -S #include <stdio.h> int main(void){ int a[5]; a[1]; return (0);} #include <stdio.h> int main(void){ int a[5]; *(a+1); return (0);} 41

C 8 -S s ****.s 40 [ ] ex40.c 39 ex40.c 4 : 5 : int main(int argc, char **argv) 6 : { 7 : int i; 8 : char ca[] = "xyz"; 9 : double xa[] = 10 : {1, 3, 5}; 11 : 12 : for (i = 0; i < 3; i++) { 13 : printf("(%p): ca[%d] = %c, *(ca+%d) = %c\n", 14 : ca + i, i, ca[i], i, *(ca + i)); 15 : } 16 : for (i = 0; i < 3; i++) { 17 : printf("(%p): xa[%d] = %.3f, *(xa+%d) = %.3f\n", 18 : xa + i, i, xa[i], i, *(xa + i)); 19 : } 20 : 21 : return (0); 22 : } $./ex40 (0xbffff9a4): ca[0] = x, *(ca+0) = x (0xbffff9a5): ca[1] = y, *(ca+1) = y (0xbffff9a6): ca[2] = z, *(ca+2) = z (0xbffff98c): xa[0] = 1.000, *(xa+0) = 1.000 (0xbffff994): xa[1] = 3.000, *(xa+1) = 3.000 (0xbffff99c): xa[2] = 5.000, *(xa+2) = 5.000 8.5.1 char strlen() int strlen(char *s) { } char *p = s; while (*p!= \0 ) p++; retrun p - s; p++ p1, p2 p1 - p2 8.6 2 ( ) 2 main **argv 2 char argv argv char char 41 main char **argv char *argv[] 2 char [ ] ex41.c 42

C 8 40 ex41.c 3 : 4 : int main(int argc, char **argv) 5 : { 6 : char buf[10][128]; 7 : 8 : printf("argc: &buf\t &argv\t *buf[argc]\n"); 9 : while (argc-- > 0) { 10 : strcpy(buf[argc], argv[argc]); 11 : printf("%4d: %p %p %s\n", 12 : argc, buf[argc], argv[argc], buf[argc]); 13 : } 14 : 15 : return (0); 16 : } argc argv[argc] 2 buf 128 128 (10) = 0x80 (16) argv ( +1) $./ex41 1 ABCDEFGHIJ argc: &buf &argv *buf[argc] 2: 0xbffff57c 0xbffffb1c ABCDEFGHIJ 1: 0xbffff4fc 0xbffffb1a 1 0: 0xbffff47c 0xbffffb13./ex41 0x80 (+1) 1fefefefefefefefefefefe1 a i a[i] = *(a+i) 1fefefefefefefefefefefe1 43

C 9 9 9.1 struct struct [] { } [ ] ; club[0] = matuda; matuda club[0] club[1] = {"Namio Matuda", 46, 166}; 9.3 9.3.1.. strcpy(matuda.name,"mazda"); matuda.age = 50; club[1].age = matuda.age; scanf("%lf", &club[1].height); struct member { char name[40]; unsigned short int age; double height; } p1, p2; 3 name, age height member struct member p1 p2 struct member suzuki, satoh, oishi; struct member club[100], *who, func(); struct mmber struct member matuda = {"Namio Matuda", 46, 166}; 9.2 matuda 9.3.2 (* ). -> -> struct member *who strcpy(who->name,"someone Else"); who->height = 203.2; printf("%.2f\n", who->height); 42 T m σ κ element.dat fgets(buffer, size, 44

C 9 stdin) command file $ command <file Unix OS [ ] ex42.c 41 ex42.c 4 : 5 : struct iccd { 6 : int number; 7 : char symbol[3]; 8 : char name[20]; 9 : double mt; 10 : double ec; 11 : double tc; 12 : }; 13 : 14 : void print(struct iccd p) 15 : { 16 : printf( 17 : "%2d %s\t%-8s\t%8.3f %8.2g %6.1f\n", 18 : p.number, p.symbol, p.name, p.mt, p.ec, p.tc 19 : ); 20 : } 21 : 22 : struct iccd getdat(char buf[]) 23 : { 24 : struct iccd p; 25 : sscanf(buf, "%d%s%s%lf%lf%lf", &p.number, 26 : p.symbol, p.name, &p.mt, &p.ec, &p.tc); 27 : return (p); 28 : } 29 : 30 : int main(void) 31 : { 32 : int i = 0; 33 : char buf[80]; 34 : struct iccd element[100]; 35 : 36 : while (fgets(buf, 80, stdin)!= NULL) { 37 : element[i] = getdat(buf); 38 : print(element[i]); 39 : i++; 40 : } 41 : return (0); 42 : } 22 28 sscanf() return 37 36 fgets 1 char buf getdat() buf 14 20 print() ex42u.c $./ex42 < element.dat 3 Li Lithium 453.690 1.2e+07 82.0 4 Be Beryllium 1551.000 3.7e+07 220.0 11 Na Sodium 370.960 2.3e+07 125.0 12 Mg Magnesium 921.950 2.5e+07 153.0 13 Al Aluminium 933.520 4e+07 235.0 19 K Potassium 336.800 1.6e+07 109.0...... 81 Tl Thallium 576.650 6.7e+06 47.0 82 Pb Lead 600.652 5.2e+06 35.0 83 Bi Bismuth 544.450 1e+06 11.0 38 print() q38.c 39 1000 K q39.c 9.4 typedef struct typedef typedef unsigned short int USHRT; typedef struct {double x, y, z;] POINT3; USHRT unsigned short int 3 POINT3 USHRT i, j[100], f(...) POINT3 point *ppoint 9.5 3 3 3 typedef struct {double x, y, z;} Vec3; Vec3 2 Vec3 vadd(vec3 v1, Vec3 v2) { Vec3 v; v.x = v1.x + v2.x; v.y = v1.y + v2.y; v.z = v1.z + v2.z; return (v); } 45

C 9 1 #include 3 vector.h vector.h 42 vector.h 4 : 5 : #define deg2rad(deg) ((deg)*m_pi/180) 6 : #define rad2deg(rad) ((rad)*180/m_pi) 7 : 8 : typedef struct {double x, y, z;} Vec3; 9 : typedef struct {double r, t, p;} Pol3; 10 : 11 : Vec3 vadd(vec3 a, Vec3 b) 12 : { 13 : Vec3 v; 14 : v.x = a.x + b.x; 15 : v.y = a.y + b.y; 16 : v.z = a.z + b.z; 17 : return (v); 18 : } 19 : 20 : Vec3 vsub(vec3 a, Vec3 b) 21 : { 22 : Vec3 v; 23 : v.x = a.x - b.x; 24 : v.y = a.y - b.y; 25 : v.z = a.z - b.z; 26 : return (v); 27 : } 28 : 29 : double viprod(vec3 a, Vec3 b) 30 : { 31 : return a.x*b.x + a.y*b.y + a.z*b.z; 32 : } 33 : 34 : Vec3 voprod(vec3 a, Vec3 b) 35 : { 36 : Vec3 c; 37 : c.x = a.y*b.z - a.z*b.y; 38 : c.y = a.z*b.x - a.x*b.z; 39 : c.z = a.x*b.y - a.y*b.x; 40 : return (c); 41 : } 42 : 43 : double vabs(vec3 a) 44 : { 45 : return sqrt(viprod(a,a)); 46 : } 47 : 48 : double varg(vec3 a, Vec3 b) 49 : { 50 : return acos(viprod(a,b)/(vabs(a)*vabs(b))); 51 : } 52 : 53 : Vec3 pol3tovec3(pol3 pv) 54 : { 55 : Vec3 v; 56 : v.x = pv.r*sin(pv.t)*cos(pv.p); 57 : v.y = pv.r*sin(pv.t)*sin(pv.p); 58 : v.z = pv.r*cos(pv.t); 59 : return (v); 60 : } voprod() c = a b a b = ( a y b z a z b y, a z b x a x b z, a x b y a y b x ) a b = a b sin θ θ a, b a, b 1 2 a b pol3tovec3() (r, θ, φ) (x, y, z) Pol3 Vec3 43 1 10 3 A,B,C ABC a, b, c [ ] ex43.c ex43u.c 43 ex43.c 4 : #include <time.h> 5 : #include "vector.h" 6 : 7 : int main(int argc, char **argv) 8 : { 9 : int i; 10 : double s1, s2, a, b, c, s; 11 : Vec3 v[3], va, vb, vc; 12 : 13 : srand(time(null)); 14 : for (i = 0; i < 3; i++) { 15 : v[i].x = rand() % 10; 16 : v[i].y = rand() % 10; 17 : v[i].z = rand() % 10; 18 : printf( 19 : "v[%d].x = %f, v[%d].y = %f, v[%d].z = %f\n", 20 : i, v[i].x, i, v[i].y, i, v[i].z 21 : ); 22 : } 23 : va = vsub(v[1], v[0]); 24 : vb = vsub(v[2], v[1]); 25 : vc = vsub(v[0], v[2]); 26 : a = vabs(va); 27 : b = vabs(vb); 28 : c = vabs(vc); 29 : s = (a + b + c) / 2; 30 : 31 : s1 = vabs(voprod(va, vb)) / 2; 32 : s2 = sqrt(s * (s - a) * (s - b) * (s - c)); 33 : printf(" 1 = %f, 2 = %f\n", s1, s2); 34 : 35 : return (0); 36 : } 40 xy a = b = c sin θ 1 sin θ 2 sin θ 3 θ 1, θ 2, θ 3 a, b, c q40.c 46

C 9 44 ex44.c a θ 2 9.6 struct tm θ 3 c Unix UTC 1970 1 1 00:00:00 time(null) man time struct tm struct tm { int tm_sec; /* */ int tm_min; /* */ int tm_hour; /* */ int tm_mday; /* */ int tm_mon; /* */ int tm_year; /* */ int tm_wday; /* */ int tm_yday; /* */ int tm_isdst; /* */ }; gmtime(), localtime() time_t now; struct tm *nowtm;... now = time(null); nowtm = localtime(&now) nowtime nowtm->hour 44 struct tm time() localtime() [ ] ex44.c b θ 1 2 : #include <time.h> 3 : 4 : int main(void) 5 : { 6 : time_t now; 7 : struct tm *nowtm; 8 : 9 : now = time(null); 10 : nowtm = localtime(&now); 11 : 12 : printf( 13 : " %d %02d %02d " 14 : " %02d:%02d:%02d.\n", 15 : nowtm->tm_year + 1900, nowtm->tm_mon + 1, nowtm->tm_mday, 16 : nowtm->tm_hour, nowtm->tm_min, nowtm->tm_sec 17 : ); 18 : printf("%s", ctime(&now)); 19 : 20 : return (0); 21 : } 1900 1900 0 +1 ctime() Unix usleep() <unistd.h> BSD ftime() <sys/timeb.h> notes9-6.c 45 notes9-6.c 3 : #include <unistd.h> 4 : #include <time.h> 5 : #include <sys/timeb.h> 6 : 7 : int main(int argc, char **argv) 8 : { 9 : struct tm *nowtm; 10 : struct timeb nowb; 11 : int interval = 300000; 12 : 13 : if (argc > 1) 14 : interval = atoi(argv[1]); 15 : while (1) { 16 : ftime(&nowb); 17 : nowtm = localtime(&nowb.time); 18 : printf("%02d:%02d %03d\n", 19 : nowtm->tm_min, nowtm->tm_sec, nowb.millitm); 20 : usleep(interval); 21 : } 22 : return (0); 23 : } ( µs) 0.3 s Ctrl + c $./a.out 19:15 282 19:15 584 19:15 894... 47

C 10 10 C preprocessportability directive # #include #define #if #elif #else #endif #ifdef, #ifndef 10.1 #include #include <stdio.h> #include <math.h> #include "vector.h" <> "" 10.2 #define #define #define N 128 1 N+1 128+1 NN 128128 #define deg2rad(a) ((a)*m_pi/180) #define deg2rad(a) a*m_pi/180 deg2rad(x+1) 1/deg2rad(x) deg2rad(x+1) --> x+1*m_pi/180 1/deg2rad(x) --> 1/x*M_Pi/180 #define sqr(a) ((a)*(a)) sqr(x++) sqr(x++) --> ((x++)*(x++)) x++ 2 1fefefefefefefefefefefe1 struct struct [] { } []; typedef typedef 1fefefefefefefefefefefe1 48

C 11 11 C C stream FILE FILE stdin stdoutstderr stdin stdout stderr fopen() FILE 11.1 FILE fopen() 19 fopen() mode mode "r" NULL "r+" + NULL "w" "w+" + "a" "a+" + r,r+,w,w+,a,a+ t b UNIX OS FILE fopen() FILE *fp; fp = fopen(name, mode); name char mode 19 NULL 11.2 : fclose() fclose(fp) fp fflush() fclose 11.3 20 fp 20 int fgetc(fp) 1 EOF int getc(fp) 1 EOF int getchar() getc(stdin) EOF int ungetc(c,fp) 1 (c) EOF char *fgets(s,n,fp) 1 NULL char *gets(s) 1 NULL int fputc(c,fp) 1 (c) EOF int putc(c,fp) 1 (c) EOF int putchar(c) putc(c,stdout) EOF int fputs(s,fp) 1 EOF int puts(s) stdout 1 EOF fp c int s n int 11.4 fflush() fputc() fflush(fp) 45 element.dat 1 49

C 11 46 ex45.c 4 : #include <unistd.h> 5 : 6 : int main(void) 7 : { 8 : int c; 9 : char infile[] = "element.dat"; 10 : FILE *fp; 11 : 12 : if ((fp = fopen(infile, "r")) == NULL) { 13 : printf("can t open %s\n", infile); 14 : exit(0); 15 : } 16 : while ((c = fgetc(fp))!= EOF) { 17 : fputc(c, stdout); 18 : fflush(stdout); 19 : usleep(100000); 20 : } 21 : fclose(fp); 22 : 23 : return (0); 24 : } [ ] ex45.c 12 15 fopen() element.dat "r" 17 21 fgetc(fp) 1 fputc(c,stdout) fgetc() getc() fputc() putc() 19,20 1 fflush() 1 usleep() Ctrl + c 11.5 printf() double 8 64 1.2345678903456e-10 20 20 8 2 2 (Self-Discribing) HDF (Hierarchical Data Format) NetCDF (network Common Data Form) 11.6 fread() fwrite() fread() fwrite() size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream) fread() stream size nmemb ptr fwrite() ptr size nmemb stream void 46 2 [ ] ex46.c 22 double x fpc char.dat 23 fpb bin.dat x double 1 ex46u.c 2 $./ex46 $ ls -l char.dat bin.dat -rw-r--r--... 8192 6 28 23:36 bin.dat -rw-r--r--... 20394 6 28 23:36 char.dat bin.dat char.dat less char.dat 50

C 12 SYSTEM() 47 ex46.c 4 : 5 : int main(void) 6 : { 7 : double x; 8 : char filec[] = "char.dat"; 9 : char fileb[] = "bin.dat"; 10 : FILE *fpc, *fpb; 11 : 12 : if ((fpc = fopen(filec, "w+")) == NULL) { 13 : printf("can t open %s\n", filec); 14 : exit(0); 15 : } 16 : if ((fpb = fopen(fileb, "w+")) == NULL) { 17 : printf("can t open %s\n", fileb); 18 : exit(0); 19 : } 20 : for (x = 0; x < 1024; x++) { 21 : fprintf(fpc, "%.15f\n", x); 22 : fwrite(&x, sizeof(double), 1, fpb); 23 : } 24 : fclose(fpc); 25 : fclose(fpb); 26 : 27 : return (0); 28 : } 41 2 41 q42.c fprintf() fscanf() fwrite() fread() q42.c 48 q42.c 4 : 5 : int main(void) 6 : { 7 : int i; 8 : double xc, xb; 9 : char filec[] = "char.dat"; 10 : char fileb[] = "bin.dat"; 11 : FILE *fpc, *fpb; 12 : 13 : if ((fpc = fopen(filec, "r")) == NULL) { 14 : printf("can t open %s\n", filec); 15 : exit(0); 16 : } 17 : if ((fpb = fopen(fileb, "r")) == NULL) { 18 : printf("can t open %s\n", fileb); 19 : exit(0); 20 : } 21 : for (i = 0; i < 1024; i++) { 22 : fscanf(fpc, "%lf\n", &xc); 23 : fread(&xb, sizeof(double), 1, fpb); 24 : printf("xc = %4.1f,\txb = %4.1f\n", xc, xb); 25 : } 26 : fclose(fpc); 27 : fclose(fpb); 28 : 29 : return (0); 30 : } 12 system() OS OS system() system() Unix /bin/sh -c system(" ") 47 sytem() cal 49 ex47.c 3 : 4 : int main(void) 5 : { 6 : system("/usr/bin/cal"); 7 : return (0); 8 : } $./ex47 6 2003 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 51