2015 3 13 1 2 2 3 2.1.......................... 3 2.2............................. 3 3 4 3.1............................... 4 3.2 double float......................... 5 3.3 main....................... 6 3.4............................ 6 4 7 5 7 5.1................................. 9 5.2............................... 9 6 9 6.1 Makefile................................ 9 6.2............................. 12 6.3................................. 14 7 15
1 1 1) Makefile make Makefile 2) main 1 3) 4) main a) b) c) main d) main 1 enum { False, True } ; 2 void Usage ( ) { 3 puts ( Usage :....... ) 4 } 5 6 int main ( ) 7 { 8 int s f l a g=f a l s e ; 9 i f ( ) { 10 Usage ( ) ; 11 e x i t ( 1 ) ; 12 } 13 i f ( s? ) { 14 s f l a g=true ; / s / 15 } 16 17 / / 18 } 5) 6) Segmentation Fault 7) C++ ( 2
2 C++ ) 8) gcc Wall O2 Wall O2 2 2.1 [ ] C,, 2.2 1 1 #include <s t d i o. h> 2 int z =1; 3 4 void func ( int x ) 5 { 6 s t a t i c int y=8; 7 extern int z ; 8 p r i n t f ( %d,%d,%d\n, ( x)++,y++,z++); 9 } 10 int main ( void ) 11 { 12 int x=2, y=5, z =4; 13 func (&y ) ; 14 p r i n t f ( %d,%d,%d\n, x++,y++,z++); 15 func (&z ) ; 16 p r i n t f ( %d,%d,%d\n, x++,y++,z++); 17 return ( 0 ) ; 18 } 2 3
3 1 #include <s t d i o. h> 2 3 void func ( int n1, int np2 ) 4 { 5 n1= np2 ; 6 np2 =8; 7 np2=&n1 ; 8 } 9 10 int main ( void ) 11 { 12 int n1=0, n2 =5; 13 int np1, np2, tmp ; 14 15 np1=&n2 ; 16 n2++; 17 tmp=&n1 ; 18 ( tmp)++; 19 np2=tmp ; 20 ( tmp)++; 21 22 p r i n t f ( n1=%d, n2=%d, np1=%d, np2=%d\n, n1, n2, np1, np2 ) ; 23 24 func ( np1, np2 ) ; 25 p r i n t f ( np1=%d, np2=%d\n, np1, np2 ) ; 26 } 3 3.1 ( ) 1 #define MAX 10 const 1 const int MAX=10; #define const 4
3.2 double float 3 1) #define const 2 (#define const ) 1 #include <s t d i o. h> 2 #define MIN 2 3 int main ( void ) 4 { 5 p r i n t f ( %d,3/min ) ; 6 } 2) a #define 1 #include <s t d i o. h> 2 const int MAX=3; 3 int main ( void ) 4 { 5 int a [MAX], i ; 6 for ( i =0; i <MAX; i ++) a [ i ]=0; 7... 8 } 3.2 double float 1) double x=0.0 0.1 10 1.0 2) double float 3) 1 #include <s t d i o. h> 2 3 int main ( void ) 4 { 5 double x =0.0; 6 while ( 1 ) { 7 x+=0.1; 8 i f ( x==1.0) break ; 9 } 10 5
3.3 main 3 11 return ( 0 ) ; 12 } 4) x=1.0 0.1 10.0 x double float time ($ time <command name>) ) double float float float 3.3 main 1) showarg 1 $. / showarg a b c 2 4 3. / showarg a b c 2) 2 ($./sum 1 2 3 ) 2 1 $. / sum 1 2 Usage : sum <num1> <num2> main double atof() 3.4 1) n calloc int a[n] i < n a[ i]=i a [] array a) b) 6
5 2) n int a[n ][ n] a array2 4 1) mylib.c a) fropen Failed to open ( ) 1 FILE fropen ( char fname ) b) fwopen Failed to open 1 FILE fwopen ( char fname ) 2) mylib.c ( ) mylib.h 3) make clean.o Makefile 5 1) 0 1 genrand 100 (UNIX ) 2) getdist b) h (Usage) 1 $. / g e t d i s t h 2 Usage : g e t d i s t [ o p t ion ] < f i l e > 7
5 3 option : 4 h ) Show t h i s message 5 n ) with l i n e number 6... ( )... c) a d) g (, * ) 0.1 1 0 0.1: 2 0. 1 0. 2 : 3 0. 2 0. 3 : 4... #define e) n f) C gcc getopt getopt g) Usage h) mylib.c i) make getdist getdist Makefile 3) getdist.c FILE( ) result.dat FILE #ifdef 4) gcc, DFILE #define FILE make fgetdist getdist.c, fgetdist make getdist getdist Makefile 8
5.1 6 5.1 1) fropen ( ) frpopen, mylib.c, mylib.h 2) getdist getdist2 1 $ genrand 100 g e t d i s t 2 2 $ genrand 100 g e t d i s t 2 g 3 $ g e t d i s t 2 < f i l e name> 4 $ g e t d i s t 2 g < f i l e name> 5.2 1) ẋ = x a) b) (x(t)) e 2) ( d x dt y ) = ( 0 1 1 0 ) ( x y a) b) ((x(t), y(t)) ) 6 6.1 Makefile Makefile 9
6.1 Makefile 6 1 arg : arg. c # arg arg. c 2 gcc o arg arg. c # 3 4 mycat : mycat. c # mycat mycat. c 5 gcc o mycat mycat. c # Makefile # Makefile arg 1 $ make arg arg arg arg.c Makefile Makefile $ 1 GCC = gcc 2 CFLAGS= Wall O2 3 L o a d l i b s= lm # 4 5 main : main. o mycat. o # main main. o mycat. o 6 ${GCC} ${CFLAGS} o $@ main. o mycat. o ${ L o a d l i b s } 7 8 arg : arg. o # arg arg. o 9 ${GCC} ${CFLAGS} o $@ $@. o ${ L o a d l i b s } 10 11 main. o : mycat. h main. c 12 ${GCC} ${CFLAGS} c o $@ main. c 13 14 mycat. o : mycat. c 15 ${GCC} ${CFLAGS} c o $@ mycat. c 16 17 c l e a n : 18 rm f. o 3 GCC, CFLAGS, Loadlibs ${GCC}, ${CFLAGS}, ${Loadlibs} 6 $@ 5 : ( main) 10
6.1 Makefile 6 make main Makefile main main.o mycat.o main.o main.o mycat.h, main.c main.o mycat.h main.c main.o main.o (.o) 1 $ make c l e a n Makefile Makefile 1 GCC = gcc 2 CFLAGS= Wall 3 L o a d l i b s= 4 5 main : main. o mycat. o 6 ${GCC} ${CFLAGS} o $@ main. o mycat. o ${ L o a d l i b s } 7 8 arg : arg. o 9 ${GCC} ${CFLAGS} o $@ $@. o ${ L o a d l i b s } 10 11 main. o : mycat. h main. c 12 13. c. o : 14 ${GCC} ${CFLAGS} c o $@ $< 15 16 c l e a n : 17 rm f. o.c.o.c.o mycat.o mycat.c.c.o 1 mycat. o : mycat. c 2 ${GCC} ${CFLAGS} c o $@ $< $@ : mycat.o $< : mycat.c mycat.o main.o mycat.h main.c 11
6.2 6.c.o 6.2 C 6.2.1 1 #define #define 1 #define MAX 100 MAX 100 1 #define MAX 10 2 int main ( void ) 3 { 4 int i ; 5 char buf [MAX] ; 6 for ( i =0; i <MAX; i ++) buf [ i ]=0; 7 } 6.2.2 1) x 1 #define s q r ( x ) ( ( x ) ( x ) ) () 2) 2 1 #define maxof ( x, y ) ( ( x>y )? x : y ) 3) 1 #define loop ( n ) for ( i =0; i <n;++ i ) 12
6.2 6 int i ; C++ OK 1 #define loop ( n ) for ( int i =0; i <n;++ i ) 4) 1 #define loop ( i, n ) for ( i =0; i <n;++ i ) 6.2.3 1 #i f d e f 2... 3 #e l i f 4... 5 #endif #ifdef DEBUG 1 #define DEBUG 2 int main ( void ) 3 { 4 #i f d e f DEBUG 5 p r i n t f ( ) ; 6 #endif 7 } #ifndef 1 #ifndef 2... // 3 #endif 1 #i f == 1 2... 3 #e l i f == 2 13
6.3 6 4... 5 #else 6... 7 #endif 6.3 UNIX ( data.txt) less 1 $ l e s s data. t x t main ( UNIX ) 1 $ cat data. t x t l e s s cat data.txt less cat less less data.txt 1.... 2 int main ( int argc, char argv ) 3 { 4 char f i l e n a m e ; 5 FILE fp ; 6 7 f i l e n a m e=argv [ argc 1]; / / 8 9 i f ( f i l e n a m e [0]== argc==1 ) { / 1 f i l e n a m e 10 1 ( ) / 11 fp = s t d i n ; 12 } else { / / 13 fp = fopen ( filename, r ) ; 14 i f ( fp == NULL ) { 15 f p r i n t f ( s t d e r r, %s. \ n, f i l e n a m e ) ; 16 e x i t ( 1 ) ; / / 14
7 17 } 18 } 19.... 20 } 7 1) nishii@sci.yamaguchi-u.ac.jp 2) 3) 15