1 C IIA 1 C IIA IIA 1.1 Mac OS X 1.1.1 Mac OS X Unicode(UTF-8) UTF-8 Jedit X( ) Emacs( ) Emacs Emacs Emacs [Finder] [] Emacs dock Jedit X C 1. Jedit X Dock drag & drop Jedit X [Finder] [] Jedit X Folder 2. Dock Jedit X 3. [Jedit X] [] 4. [ ] Jedit X 5. [ ] 6. [ ] (UTF-8) Unix(LF) 7. [] 1 8. [ ] 1.1.2 terminal emulator C (Terminal.app) UTF-8 [Finder] [] [ ] Dock drag and drop 1 1
1.1 1 C IIA 1.1.3 $ cd comp3a %endminipage ~/comp3a mkdir $ mkdir comp3a $ cd comp3a C.c Emacs Cntrol x Control s 2 Emacs Control-x Control-f Control-x Control-s 1.1.4 c cc ( ) C $ cc -o -o -o cc -o o tt a.out C cc -Wall(warning all) Wall $ cc -Wall -o 2 Emacs Control- 2
1.2 C 1 C IIA $././ 1.2 C C99(ISO/IEC9899:1999) ANSI C(C89) C99 JIS X 3010:2003 1.2.1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /******************************************* * add.c * * 2 10 * *******************************************/ int a,b,sum; printf("2 a,b \n"); printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); sum = a + b; printf("a+b=%d\n",sum); 1.1 add.c (1) (2) (3) main() (4) (5) (6) (7) (8) (9) return 1.2.2 C99 37 (keyword) 3 (identifier) 32( 4 ) ANSI C 5 C99 ( IIA ) 3 3
1.3 1 C IIA 1: C99 auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while inline restrict Bool Complex Imaginary 1.3 ( ) (type) IIA 2: C Bool Bool char char signed char short int int long int long long int unsigned char unsigned short int un- signed int unsigned long int unsigned long long int enum float double long double float Complex double Complex long double Complex (basic type) 1.4 ( ) 1.4.1 4
1.4 1 C IIA 1.1 add.c 19 0 1.4.2 ( ) (identifier) 1.2 add.c int a, b, sum 3 int a, b, sum 1.4.3 C99 ANSI C ( IIA ) 3: + - ++ -- + - * / % = += -= *= /= %= &&! < <= > >= ==!= () sizeof sizeof?: & * []. ->, ~ & ^ << >> &= ^= = >>= <<= # ## 1.3 add.c 13 15 &16 = + 1.4.3.1 1.4 add.c 16 sum = a + b a + b sum 5
1.5 1 C IIA ()( ) []( ) ++ --( ). ->( ) ++ --( ) + -( )!( ) ~( ) sizeof &( ) *( ) ()( ) * / %( ) + -( ) << >>() > >= < <=( ) ==!=( ) &( AND ) ^( OR ) ( OR ) &&( AND ) ( OR )?:( ) = += -= *= /= %= >>= <<= &= ^= = ( ),( ) 1.5 C99 1.5.1 1.2 0 1. if 2. if-else 3. switch 1.5.2 for ( 1 ; 2 ; 3) while 1; while ( 2) 3 6
4: if if ( ) 0 if-else if( ) 1 else 2 0 1 0 2 1 2 switch switch( ) 2 ( case : default : ) case default 1.3 n 1 n 1. for 2. while 3. do 1.5.3 [1] Al Kelley, Ira Pohl, A Book on C, Addison Wesley, 1998, C BP 2002 ANSI C [2] 1997 JIS 1997 ANSI C(ISO/IEC 9899:1990) JIS X3010-1993 [3] C2003 C99(ISO/IEC 9899:1999) JIS X 3010:2003 [4] C http://okuyama.mt.tama.hosei.ac.jp/unix/c/ C99 [5] C 3 SOFT BANK 2007 ANSI C 7
5: for for (i=0 ; i<n ; i++) i= 0, 1,..., N 1 i= N for for for (i=m ; i<= N ; i++) i= M, M + 1,..., N i= N + 1 for for for ( 1 ; 2 ; 3) 1 1 2 0 0 3 while while( ) 0 ( ) 0 1 do do while( ); 0 ( ) 0 1 6: continue continue; continue continue break break; break switch break switch goto goto ; return return; return ; 8
2 2 2.1 (module) (divide and conquer algorithm) (stepwise refinement) (module) C 2.2 ( ) C 2.3 main() 1 C 2.3.1 add() C 9
2.3 2 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 /****************************************** * add-func.c * * 2 double * ******************************************/ double add(double x,double y); double a; a=add(1.2,2.3); printf("a=%f\n",a); double add(double x,double y) double z; z=x+y; return z; 3 main() (8-16 ) printf() (13 ) add() (18-25 ) add() 1. 6 double add(double x,double y); (function prototype) ; 2. 12 add(1.2,2.3) (function call) add() 1.2,2.3 1.2 x 2.3 y 3. 12 a=add(1.2,2.3); add(1.2,2.3) 3.5 double a 4. 18-25 (function definition) add() 5. 19-25 (function body) add() 6. 20 double z; add() double z 7. 24 return z; return z add() 10
2.3 2 add-func.c $ cc -Wall add-func.c -o add-func $./add-func a=3.500000 main() (actual arguments/actual parameters) (formal arguments/formal parameters) ( ) return (return value) void printf() 1 (char ) hello.c printf() 1 "Hello World\n"int 12 ( \n 1 ) printf() /* function printf() */ int num; num=printf("hello World\n"); printf(" %d \n",num); 2.3.2 return return return ; return; return return ( ) add-func.c add() 11
2.3 2 double add(double x,double y) return x+y; return return; 0 100 double C2F(double cel); int c; printf(" \n"); printf("----------\n"); for(c=0;c<=100;c++) printf("%4d %5.2f\n", c, C2F((double)c) ); double C2F(double cel) return cel * 9.0 / 5.0 + 32.0; 2.3.3 main() return main() main() ( ) int 0 main() $? hello.c printf("hello, world\n"); 12
2.3 2 $ cc -Wall -o hello hello.c $./hello Hello, world $ echo $? 0 main() $? /* return_test.c */ int ret; printf("main "); scanf("%d", &ret); return ret; $ cc -Wall -o return_test return_test.c $./return_test main 3 $ echo $? 3 $./return_test main -1 $ echo $? 255 $? unsigned char 2.3.4 ( ) int void 13
2.4 () 2 void add-func.c add() double ( ) add double x,double y z=x+y;return z; add(double x,double y) 2.4 () ( ) (function prototype; ) () main() include printf() <stdio.h> printf() ( ); ; add-func.c /****************************************** * add-func1.c * * * ******************************************/ double add(double x,double y) double z; z=x+y; return z; double a; a=add(1.2,2.3); /* */ printf("a=%f\n",a); 14
2.5 2 2.5 3 maximum() int maximum(int a, int b, int c); int x, y, z, max; printf(" 3 :"); scanf("%d%d%d", &x, &y, &z); max = maximum(x, y, z); printf(" %d \n", max); int maximum(int a, int b, int c) int ret = a; if ( b > ret ) ret = b; if ( c > ret ) ret = c; return ret; $ cc maximum.c -o maximum -Wall $./maximum 3 :30 25 11 30 $./maximum 3 :15 8 33 33 2.6 : call by value call by reference ( ); 15
2.6 : call by value call by reference 2 (); getchar() getchar(); /****************************************** * add-func2.c * * * ******************************************/ double add(double x,double y); double a=1.2,b=2.3,c; c=add(a,b); /* */ printf("c=%f\n",c); double add(double x,double y) double z; z=x+y; return z; add() a b a b (call by value) (call by reference) 4 (call by value) 4 scanf() 2 & (call by reference) 16
2.6 : call by value call by reference 2 /* call by value.c */ void func(int x, int y); double x=1, y=2; printf("main x=%d, y=%d\n", x, y); func(x, y); printf("main x=%d, y=%d\n", x, y); void func(double x,double y) x=3; y=0; printf("func x=%d, y=%d\n", x, y); return ; 2.1 double 2 add() 2 add-func3.c 2.2 tax() main() tax() 2.3 f(x) = 1 1 + x 2 double f(double x) 0 1 0.1 f(0.0), f(0.1),..., f(1.0) 2.4 absolute(x) = x x 0 x x < 0 double absolute(double x) double absolute() void (); 17
2.6 : call by value call by reference 2 void void void void printedge(int size); /* */ int l,i,j; printf("input size :"); scanf("%d", &l); if ( l < 3 l > 80) printf("inpossible\n"); printedge(l); /* */ for(i=2;i<l;i++) printf("*"); for(j=2;j<l;j++) printf(" "); printf("*\n"); printedge(l); /* */ void printedge(int size) int i; for(i=1;i<=size;i++) printf("*"); printf("\n"); return; 18
2.7 2 2.7 C auto, register, extern, static 4 () 4 auto register extern static auto double x, y auto double x, y; auto register CPU CPU CPU register static extern 2 extern static static int counter(void); int i; for ( i=0; i<10; i++) counter(); 19
2.8 2 int counter(void) static int counter = 0; printf(" counter %d \n", ++counter); $ cc -Wall -o static_variable static_variable.c $./static_variable counter 1 counter 2 counter 3 counter 4 counter 5 counter 6 counter 7 counter 8 counter 9 counter 10 static counter 1 2.8 ( localize ) () void func(void); int x = 5; /* main */ printf("main x %d\n", x); func(); 20
2.9 2 func(); printf("main x %d\n", x); void func(void) int x = 20; /* func */ printf("\nfunc x %d\n", x); x++; printf("\nfunc x %d\n", x); $ cc -Wall -o scope scope.c $./scope main x 5 func x 20 func x 21 func x 20 func x 21 main x 5 x 2.9 <math.h> double double #include <math.h> $ cc -lm -o -lm libm.a, libm.dylib (sqrt(-1.2) ) 21
2.9 2 7: <math.h> double acos(double x); x () cos 1 x double asin(double x); x () sin 1 x double atan(double x); x () tan 1 x double cos(double x); x cos x double sin(double x); x sin x double tan(double x); x tan x double exp(double x); x e x double log(double x); x log e x double log10(double x); x log 10 x double pow(double x,double y); x y x y double sqrt(double x); x x double fabs(double x); x x double floor(double x); x [x] 2.1 /* diagonal.c */ #include <math.h> double x,y,d; printf("="); scanf("%lf",&x); printf("="); scanf("%lf",&y); d=sqrt(x*x+y*y); printf(" =%f\n",d); $ cc -lm diagonal.c -o diagonal $./diagonal =3 =4 =5.000000 2.5 2 ax 2 + bx + c = 0, (a 0) (D = b 2 4ac ) 2.2 /* sin_curve.c */ 22
2.10 2 #include <math.h> double x, y, Pi=3.14159; int i, y2; for(x=0.0; x<=2.0 * Pi; x+=0.1) y=sin(x); y2 = (int)((y + 1.0)/2.0 * 80.0 ); for(i=0;i<y2;i++) printf(" "); printf("*\n"); 2.6 2 (x 1, y 1 ), (x 2, y 2 ) distance.c double 2.10 5 int Fact( int n ); int n; n = 5; printf("factorial of %d is %d.\n", n, Fact(n)); int Fact( int n ) if ( n == 1 ) return 1; else return n * Fact( n - 1 ); Fact() Fact() Fact() 1 23
2.10 2 n 1 1 (recursive) n main() n Fact() n 2.7 for() 64 64 n A B n - 1 A C n A B C n - 1 C B A B C 1: #include<stdio.h> int hanoi(int disks, char a, char b, char c); int disks; char a = A, b= C, c= B ; printf("?:"); scanf("%d", &disks); while (disks > 65 disks < 0 ) printf("1 64 :"); scanf("%d", &disks); 24
2.10 2 hanoi(disks, a, b, c); int hanoi(int disks, char a, char b, char c) if (disks == 1) printf(" 1 %c %c \n", a, b); else hanoi(disks - 1, a, c, b); printf(" %d %c %c \n", disks, a, b); hanoi(disks - 1, c, b, a); 25