Similar documents

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

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

卒 業 研 究 報 告.PDF

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

untitled

10

‚æ4›ñ

超初心者用

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

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

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

ohp03.dvi

untitled

ohp08.dvi

r08.dvi

Java

r03.dvi

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 <<

新版明解C言語 実践編

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

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 Presentation

double float

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

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

/* do-while */ #include <stdio.h> #include <math.h> int main(void) double val1, val2, arith_mean, geo_mean; printf( \n ); do printf( ); scanf( %lf, &v

数値計算

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

ディジタル信号処理

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

tuat1.dvi

joho07-1.ppt

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

lexex.dvi

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

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

Battle Ship

ex12.dvi

スライド タイトルなし

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) * *

r07.dvi

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

ohp07.dvi

PowerPoint プレゼンテーション


+ +

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)* (

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

tuat2.dvi

1-4 int a; std::cin >> a; std::cout << "a = " << a << std::endl; C++( 1-4 ) stdio.h iostream iostream.h C++ include.h 1-4 scanf() std::cin >>

program.dvi

ファイル入出力

Java updated

ファイル入出力

pptx

untitled

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

I J

新・明解C言語 実践編

ex14.dvi

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

C ( ) C ( ) C C C C C 1 Fortran Character*72 name Integer age Real income 3 1 C mandata mandata ( ) name age income mandata ( ) mandat

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

K227 Java 2

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

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

(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

橡Pro PDF

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

O(N) ( ) log 2 N

3 3.1 LAN ISDN (IP) 2 TCP/UDP IP IP IP IP (Ethernet) Ethernet LAN TCP/UDP LAN Ethernet LAN 2: Ethernet ATM, FDDI, LAN IP IP IP 3 IP 2 IP IP IP IP IP 3

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-

prog-text.dvi

コンピュータ概論

[ 1] 1 Hello World!! 1 #include <s t d i o. h> 2 3 int main ( ) { 4 5 p r i n t f ( H e l l o World!! \ n ) ; 6 7 return 0 ; 8 } 1:

第7章 有限要素法のプログラミング

ディジタル信号処理

計算機プログラミング

DA100データアクイジションユニット通信インタフェースユーザーズマニュアル

PowerPoint プレゼンテーション

練習&演習問題

Microsoft PowerPoint pptx

double 2 std::cin, std::cout 1.2 C fopen() fclose() C++ std::fstream 1-3 #include <fstream> std::fstream fout; int a = 123; fout.open( "data.t

64bit SSE2 SSE2 FPU Visual C++ 64bit Inline Assembler 4 FPU SSE2 4.1 FPU Control Word FPU 16bit R R R IC RC(2) PC(2) R R PM UM OM ZM DM IM R: reserved

Informatics 2014

PowerPoint Presentation

void hash1_init(int *array) int i; for (i = 0; i < HASHSIZE; i++) array[i] = EMPTY; /* i EMPTY */ void hash1_insert(int *array, int n) if (n < 0 n >=

目次

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

解きながら学ぶC言語

1.ppt

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

10/ / /30 3. ( ) 11/ 6 4. UNIX + C socket 11/13 5. ( ) C 11/20 6. http, CGI Perl 11/27 7. ( ) Perl 12/ 4 8. Windows Winsock 12/11 9. JAV

Java Java Java Java Java 4 p * *** ***** *** * Unix p a,b,c,d 100,200,250,500 a*b = a*b+c = a*b+c*d = (a+b)*(c+d) = 225


char int float double の変数型はそれぞれ 文字あるいは小さな整数 整数 実数 より精度の高い ( 数値のより大きい より小さい ) 実数 を扱う時に用いる 備考 : 基本型の説明に示した 浮動小数点 とは数値を指数表現で表す方法である 例えば は指数表現で 3 書く

2009 T060050

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

問 2 ( 型変換 ) 次のプログラムを実行しても正しい結果が得られない 何が間違いかを指摘し 正しく修正せよ ただし int サイズが 2 バイト long サイズが 4 バイトの処理系での演算を仮定する #include <stdio.h> int main( void ) { int a =

Transcription:

C Nishio, Quick http://karetta.jp/book-cover/c-for-beginners http://www.usamimi.info/ guiprog/

C 1972 AT&T Dennis M. Ritchie Brian Wilson Kernighan 1,2 80 90 C 90 Java Visual Basic 90 2000 Java GUI C C C JAVA C++ C# C C C

5 1 C 1 1.1............................... 1 1.2............................ 2 1.2.1 Visual C++ 2008 Express Edition...... 2 1.2.2...................... 3 1.3......................... 8 1.3.1..................... 8 1.3.2 Hello World.................... 9 1.3.3 printf.................. 9 1.3.4.................... 10 1.4................................. 11 1.5................................... 12 1.5.1................. 12 1.5.2................... 13 1.5.3....... 13 1.6................................. 14 2 17 2.1................................. 17 2.2.............................. 18 2.3.......................... 20 2.4............................ 21 2.5............................ 21 2.6 n.................................. 24 2.7........................ 26 2.8 const............................... 26 2.9................................... 27 2.9.1...................... 28 2.9.2.......................... 28 2.9.3............................. 29 2.9.4...................... 29

2.10................................. 30 2.11 scanf................................ 31 2.12................................... 32 2.12.1........................... 32 2.12.2........................ 33 2.12.3.......................... 34 2.12.4............................. 35 2.12.5................. 36 2.12.6 scanf...................... 37 2.13............................... 38 2.13.1.......................... 38 2.13.2.......................... 40 2.13.3......................... 40 2.13.4........................ 40 2.14................................... 40 2.14.1.......... 40 2.14.2 scanf...................... 41 2.15................................. 42 3 45 3.1 if else........................... 45 3.1.1 if else........................ 45 3.1.2 0.......................... 47 3.1.3........................... 48 3.1.4........................... 49 3.2 switch.......................... 51 3.2.1 break switch.................. 53 3.3 do while for....................... 54 3.3.1 do while.......................... 54 3.3.2 while............................ 55 3.3.3 for............................. 57 3.3.4 break.................. 58 3.3.5 continue........................... 59 3.4............................... 60 3.4.1...................... 61 3.4.2.......................... 63 3.5................................... 64 3.5.1................ 64 6

3.5.2................... 65 3.5.3................. 67 3.5.4.............. 68 3.5.5............... 68 3.5.6..................... 69 3.5.7.......... 71 3.6................................. 71 3.7.............................. 73 4 77 4.1................................. 77 4.2............................ 77 4.3............................ 80 4.4......................... 81 4.5 main................................ 83 4.6................................. 84 4.6.1........................ 84 4.6.2........................ 88 4.6.3......................... 92 4.6.4.......................... 94 4.7................................... 96 4.7.1 strcpy..................... 96 4.7.2 strlen..................... 97 4.7.3 strcat..................... 98 4.7.4................... 99 4.8................................. 100 5 101 5.1................................. 101 5.1.1............................. 101 5.1.2 static............................... 103 5.2............................. 104 5.2.1 #define............................. 104 5.2.2 #undef............................. 105 5.2.3.......................... 105 5.2.4 #include............................. 106 5.3................................. 107 7

6 109 6.1.............................. 109 6.2......................... 111 6.3..................... 113 6.4 NULL............................. 116 6.5......................... 117 6.6.............................. 120 6.6.1 sizeof............................... 121 6.6.2..................... 122 6.7............................. 125 6.7.1...................... 125 6.7.2......................... 130 6.7.3................ 131 6.7.4.......................... 133 6.8......................... 135 6.9.......................... 138 6.10................................... 140 6.10.1 strlen................... 140 6.10.2 strcat................... 141 6.11................................. 141 7 145 7.1.................................. 145 7.2.......................... 147 7.3 typedef.............................. 149 7.4................................... 150 7.4.1 2............... 150 8 153 8.1.......................... 153 8.2............................. 156 8.2.1........................... 156 8.2.2.......................... 157 8.2.3........................ 158 8.2.4........................ 162 8.2.5............. 164 8.3............................. 167 8.4........................ 168 8

8.5................................... 169 8.5.1.............. 169 8.5.2................... 171 8.6................................. 172

1 1 C 1.1 C 1 0 ON OFF 1.1:

1.2 C 1.2 1.2.1 Visual C++ 2008 Express Edition C Visual C++ 2008 Express Edition Windows Linux OS gcc VC++2008 google Visual C++ 2008 Express Edition 1.2) 1.2: Visual C++ 2008 Express Edition Visual C++ 2008 Express Edition Web 1.3 2

1 C 1.3: Visual C++ 2008 Express Edition 1.2.2 VC++ Visual C++ 2008 1.4) 1.4: Visual C++ 2008 Express Edition 1.5 1.6 testproject 1.7 3

1.2 1.5: 1.6: 4

1 C 1.7: cpp C++ 1.8 test test.cpp 1.9 1.10 1.11 6 1.12 5

1.2 1.8: cpp 1.9: 6

1 C 1.10: 1.11: 7

1.3 1.12: 1.3 1.3.1 C 2 int main(void) 3 { 4 /* */ 5 return 0; 6 } C C C main main C main 8

1 C main /* */ #include <stdio.h> stdio.h stdio (studio) stdio STanDard Input Output 1.3.2 Hello World printf("hello World!"); /* */ 2 int main(void) 3 { 4 printf("hello World!"); 5 return 0; 6 } Hello World! printf 1.3.3 printf printf printf() ( 9

1.3 printf(" "); printf C 1 1.3.4 printf C printf("hello World"); \n \ n 2 2 printf("hello\nworld!\n"); 1.1 1.1: \n \a BEEP \0 NULL \t \b \? \ \" \\, 1 2. 10

1 C 1.1 (\a) 3 (\t) NULL 4 printf("hello world!"); printf("hello world!\0"); NULL C NULL 1.1 \n \b \n \a \t \b New line( Alert( Tab( Back Space( 1.4 C 2 int main(void) 3 { 4 printf("hello world!"); /* Hello World! */ 5 return 0; 6 } 3 4 4 11

1.5 /* Hello World! */ /* */ ANSI C C /* */ // C99 C ANSI C C99 ANSI C 1.5 1.5.1 2 3 int main(void) 4 { 5 /* */ 6 printf("this is a test program "); 7 printf("without indented. "); 8 /* 9 10 print("this line is not displayed. "); 12

1 C 11 */ 12 return 0; 13 } This line is not displayed... This is a test program without indented. 1.5.2 printf 1 printf, There are 10 types of people in this world. Those who understand binary and those who don t. Which one are you? 2 3 int main(void) 4 { 5 /* */ 6 printf("there are 10 types of people in this world." 7 "Those who understand binary and those who don t." 8 "Which one are you?\n" ); 9 return 0; 10 } There are 10 types of people in this world.those who understand binary and those who don t.which one are you? 1.5.3 13

1.6 He said, How dare you do to me like that! printf("he said, "How dare you do to me like that!""); /* error */ 2 3 int main(void) 4 { 5 printf("he said \"How dare you do to me like that!\""); 6 return 0; 7 } He said, How dare you do to me like that! 1.6 1 2 3 2 3 int main(void) 4 { 5 printf("hello \0 world"); 6 return 0; 7 } 14

1 C 4 printf(). The ICMP source quench message is the TCP/IP equivalent of telling another computer: I can t keep up with all the traffic you re sending me - slow down, please. 5 5 Firewalls FOR DUMMIES by Brain Komar, Ronald Beekelaar, and Joern Wettern, PhD 15

17 2 C 2.1 C 2.1 short -32768 +32767 1000000 int 2.1-2147483648 +2147483647 long int int long int unsigned unsigned int -1 char

2.2 float,double double float int double char 2.1: short int long unsigned short unsigned int unsigned long char unsigned char float double -32768 +32767-2147483648 +2147483647-2147483648 +2147483647 0 +65535 0 +4294967295 0 +4294967295-128 +127 0 +255 0 +255 3.4 * 10^-38 3.4 * 10^+38 1.7 * 10^-308 1.7 * 10^+308 C 2.1 char -128 +127 0 +255 int -2147483648 +2147483647-32768 +32767 OS 2.2 int 18

2 int hoge; hoge int double piyo double piyo; double foo, bar; double foo; double bar; { } 2 3 int main(void) 4 { 5 int hoge; 6 printf("hello"); 7 printf("world"); 8 9 return 0; 10 } ANSI C 2 3 int main(void) 4 { 5 /* */ 19

2.3 6 printf("hello"); 7 int hoge; 8 printf("world"); 9 10 return 0; 11 } { ( ) 1 2.3 _( ( 2.2 int double 2.2: ( auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void defalut goto sizeof volatile do if static while 1 C C99 20

2.4 2 10 hoge int hoge; hoge = 10; = 2.1: int hoge = 10; x=10 x 10 if == 2.5 int hoge 10 int hoge; hoge = 10; printf("%d",hoge); 21

2.5 2.2: %d 10, 10 10 8 16 2.3 2 16 2 16 double float %f % 2.3: %d 10 %u 10 %o 8 %x 16 %f double float %c %s %p %% 22

2 2 int main(void) 3 { 4 int hoge = 10; 5 int piyo = 20; 6 double foo = 30.256; 7 8 printf("hoge %d \n",hoge); 9 printf("piyo %d \n",piyo); 10 printf(" %d \n", 50); 11 printf("hoge %d piyo %d \n",hoge,piyo); 12 printf("foo %f \n",foo); 13 printf("hoge %f \n",hoge); 14 printf("foo %d \n",foo); 15 return 0; 16 } hoge 10 piyo 20 50 hoge 10 piyo 20 foo 30.256000 hoge -11188256525479525000000.000000 foo 927712936 13,14. 13 int. 14 double.,. 23

2.6 n 2.3 %d %p ( %d %o %x %f %c %s %p Decimal Number( 10 Octal Number( 8 hexadecimal Number( 16 Floating Point Number( Character( String( Pointer( 2.6 n 2 16 10 0 9 10 2 0 1 2 10 1 2 1 10 2 2 0,1 2? 2 2 10 16 16 16 0 9 A F 10 2 16 2.4 2 16 24

2 10 2 16 0 0 0 1 1 1 2 10 2 3 11 3 4 100 4 5 101 5 6 110 6 7 111 7 8 1000 8 9 1001 9 10 1010 A 11 1011 B 12 1100 C 13 1101 D 14 1110 E 15 1111 F 16 10000 10 17 10001 11 2.4: 2,10,16 25

2.7 2.7 int hoge; printf("hoge %d ",hoge); 0 Visual C++ 2008 Express Edition. 2.8 const const 2 const 2 int main(void) 3 { 4 const int i = 5; 5 printf("%d", i); 6 return 0; 7 } const 2 int main(void) 3 { 4 const int i = 5; 5 i = 10; /* */ 6 return 0; 7 } 2 const CONSTant( 26

2.9 2 C ( 2.5 2.5: + / 2 int main(void) 3 { 4 int x, y; 5 6 x = 10; 7 y = 20; 8 printf("x+y %d \n", x + y); 9 y = y - x; 10 printf("y %d \n", y); 11 return 0; 12 } x+y 30 y 10 27

2.9 2.9.1 int x = 7; int y = 3; printf("x/y %d ", x / y); 2 int double double x = 7; double y = 3; printf("x/y %f ", x / y); y 0 0 2.9.2 int x = 10; printf(" %d \n", -x); int x = 10, y = 3; printf(" %d \n",x % y); 28

2 2.9.3 int x = 10; x += 10; x+= 10 x = x + 10 x -= 10; x /= 10; x 1 1 x++; 1 x--; C C++ C ++ C C B C D C# C# C++++, C++ ++ 2 2.9.4 x 2 x*x x^2 C 2 ^ xor 29

2.10 x*x x*x*x*x* pow pow pow( x, 4.0 ); x 4 2.10 C double dx = 10.0, dz; int iy = 3; dz = dx / iy; int double iy double int double int double int double double dy = 10.0, dz; int iy = 3; dz = (int)dy / iy; dy int dy (int) double (double) 30

2 2.11 scanf scanf 2 3 int main(void) 4 { 5 int x; 6 7 printf(" :"); 8 scanf("%d", &x ); 9 printf(" %d \n", x); 10 return 0; 11 } scanf printf x & & scanf %d printf 10 x double %f %lf scanf scanf scanf scanf scanf C scanf gets sprintf 31

2.12 2.12 5 int hoge1; int hoge2; int hoge3; int hoge4; int hoge5; 5 100 int hoge1; int hoge2; int hoge3; 2.12.1 5 int hoge[5]; 3 [5] 100 int hoge[100]; [ ]; 3. hoge 32

2 2.12.2 int hoge[5]; 2.3 2.3: 10 hoge[0] = 10; ( 2.4 2.4: 0 2.3 [0] 0 hoge[0] hoge[1] hoge[2] hoge[3] hoge[4] hoge[5] 33

2.12 2.12.3 int hoge[5] = { 1,2,3,4,5 }; 4 hoge[0] = 1; hoge[1] = 2; hoge[2] = 3; hoge[3] = 4; hoge[4] = 5; 2.5 2.5: int hoge[5] = {1,2}; 0 int hoge[5] = {1,2,0,0,0}; int hoge[] = {1,2,3,4,5}; [] [] int hoge[]; 0 int hoge[0]; 4 34

2 2.12.4 char char 1 char str; str = A ; 1 char str[6] = { H, E, L, L, O, \0 }; printf("%c%c%c%c%c%c",str[0],str[1],str[2],str[3],str[4],str[5]); str[5] NULL 5 printf 1 %c %s char str[6] = { H, E, L, L, O, \0 }; printf("%s",str); printf str [0] str &str[0] char str[6] = {"HELLO"}; char str[6] = "HELLO"; 5 35

2.12 \0 \0 char str[5] = "HELLO"; \0 1 2.12.5 scanf 2 3 int main(void) 4 { 5 char str[6]; 6 7 printf(" "); 8 scanf("%s", str); 9 printf(" %s.\n", str ); 10 return 0; 11 } HELLO HELLO HELLO %s scanf str & scanf("%s", &str[0]);, 36

2 2.6: 2.12.6 scanf scanf %s 2 3 int main(void) 4 { 5 char str[100]; 6 7 printf(" "); 8 scanf("%s", str); 9 printf(" :%s.\n", str ); 10 return 0; 11 } HELLO WORLD HELLO WORLD HELLO HELLO gets 37

2.13 2.13 C 1 char char -128 127 char hoge; hoge = 97; hoge 97 char char hoge; hoge = a ; char 2 1 0 1 0 10 10 2 1010 1 0 2.13.1 ( 2.7) Windows SHIFT JIS 10 1 16 a 16 61 10 97 1 C 16 0x char hoge = 0x61; 38

2 2.7: JIS 39

2.14 2.13.2 char hoge = 1 ; char hoge = 1; 1 0x31 2.13.3 256 256 2 ( char 2.13.4 1 2.14 2.14.1. 3.2. 40

2 2 3 int main(void) 4 { 5 double meter; 6 double answer; 7 8 printf(" :"); 9 scanf("%lf",&meter); 10 answer = meter * 3.2; 11 printf("%f %f \n", meter, answer); 12 return 0; 13 } :27 27.000000 86.400000 2.14.2 scanf 2.12.6 HELLO WORLD Enter HELLO gets() scanf(),. %[0-9].. 2 3 int main(void) 4 { 5 char str[80]; 6 7 printf("enter :"); 8 scanf("%[a-za-z ]",str); 9 printf("answer :%s\n",str); 10 return 0; 11 } 41

2.15 enter :HELLO WORLD answer :HELLO WORLD 2.15 1 180 3 2 (C) (F) (C) (F) F = 9 5 C + 32 3 2 3 int main(void) 4 { 5 int i = 3, j = 5; 6 printf("%d", i / j); 7 return 0; 8 } 4 2 3 int main(void) 42

2 4 { 5 char str[5] = "HELLO WORLD"; 6 printf("%s", str); 7 return 0; 8 } 5 2.5.. 43

45 3 3.1 if else if else if else. 60 3.1 3.1: if else 3.1.1 if else if else int hoge hoge 10 A B

3.1 if else 2 3 int main(void) 4 { 5 int hoge = 10; 6 7 if( hoge == 10 ) { 8 /* A */ 9 } else { 10 /* B */ 11 } 12 } if if( ). (true) A (false) B,hoge 10 A,hoge = 10,hoge == 10. =. (, ==., A if( hoge == 10 ) { /* hoge 10 */ },,. int hoge = 8; if( hoge == 10 ){ /* A */ }else if( hoge == 9) { /* B */ }else if( hoge == 8 ) { /* C */ }else{ /* D */ } 46

3 C. hoge 9 B,6 D. 3.1.2 0 int hoge = 1; if( hoge ){ /* */ },, hoge.., 0 0.,hoge 0 (true,0 false.,. if( hoge!= 0 ) { /* */ }!=. hoge 0.. if( 1 ) { /* A */ } else { /* B */ } A. 0 A. 0 B. 47

3.1 if else if( hoge = 3 ) { /* hoge 3 A */ } else { /* hoge 3 B */ } C hoge == 3 hoge = 3 A hoge 3 true if( 3 == hoge ) { /* hoge 3 A */ } else { /* hoge 3 B */ } 3 = hoge if hoge = 3 JAVA 3.1.3,. 3.1. i j i j 48

3 3.1: x < y x y, x > y x y, x <= y x y, x >= y x y, x == y x y, x!= y x y, 2 3 int main(void) 4 { 5 int i, j; 6 7 printf("i :"); 8 scanf("%d", &i); /* i */ 9 printf("j :"); 10 scanf("%d", &j); /* j */ 11 12 if( i < j ) { 13 /* i j */ 14 printf("i j \n"); 15 } else { 16 printf("i j \n"); 17 } 18 } i 10 j 5 i : 10 j : 5 i j 3.1.4 49

3.1 if else int hoge = 10, piyo = 20; if( hoge == 10 ) { if( piyo == 20 ) { /* A */ } }, hoge 10, piyo 20, A. ( 3.2 if 3.2: x == 10 && y == 20 x == 10 y == 20!x x x 10 y 20 x 10 y 20. if( hoge == 10 && piyo == 20){ /* A */ } 3.2 x. int hoge = 10; if(!hoge ){ /* A */ }!hoge hoge A hoge 0 A 50

3 3.2 switch if, switch. switch,if 1 int hoge = 8; 2 3 if( hoge == 10 ){ 4 /* A */ 5 }else if( hoge == 9) { 6 /* B */ 7 }else if( hoge == 8 ) { 8 /* C */ 9 }else{ 10 /* D */ 11 } if 3.2 3.2: if else 51

3.2 switch, hoge 7,hoge 6,. 1 switch. switch. 2 3 int main(void) 4 { 5 int hoge = 8; 6 switch( hoge ) { 7 case 10: /* A */ 8 break; 9 case 9 : /* B */ 10 break; 11 case 8 : /* C */ 12 break; 13 default: /* D */ 14 break; 15 } 16 /* E */ 17 } 3.3: switch switch 3.3 if-else switch 1 52

3 switch. hoge,hoge. hoge 8,case 8:. break,break switch. case 8: C E default if else. 3.2.1 break switch switch break.. 2 3 int main(void) 4 { 5 int hoge = 9; 6 switch( hoge ) { 7 case 10: printf("10\n"); 8 10 9 case 9 : printf("9\n"); 11 case 8 : printf("8\n"); 12 13 default: printf("7\n"); 14 15 } 16 return 0; 17 } 9 8 7 C case 9: case 8:. 53

3.3 do while for (fall through ) fall through break 3.3 do while for do while for 3.3.1 do while Hello world 10 do while. 2 int main(void) 3 { 4 int hoge = 10; 5 6 do{ 7 printf("%d :Hello world\n", 11 - hoge); 8 hoge--; 9 }while( hoge > 0); 10 11 return 0; 12 } 1 :Hello world 2 :Hello world 3 :Hello world 4 :Hello world 5 :Hello world 6 :Hello world 7 :Hello world 8 :Hello world 9 :Hello world 10 :Hello world 54

3 do while do { } while( ); do{ }. do{ 3.4 3.4: do while,while. 3.3.2 while do while while. do while while, ( 3.5. do while 1 while Hello world 10 while. 55

3.3 do while for 3.5: while 2 int main(void) 3 { 4 int hoge = 10; 5 while( hoge > 0 ) { 6 printf("%d :Hello world\n", 11 - hoge); 7 hoge--; 8 } 9 return 0; 10 } while. while( ) { } if while( 1 ) { printf("hello world\n"); } 56

3,.. 3.3.3 for for while while for. 2 3 int main(void) 4 { 5 int hoge; 6 for( hoge = 10; hoge > 0 ; hoge--) { 7 printf("%d :Hello world\n",11-hoge); 8 } 9 return 0; 10 } for for( ; ; ) { } if Hello world hoge--,., int hoge = 10; for( ; hoge > 0 ; hoge--) { printf("%d :Hello world\n",11-hoge); }. for while. 57

while 3.3 do while for A; while( B ) { C; } for for for( A ; B ; C ) { } for while.. for(;;){ } 3.3.4 break break 2 3 int main(void) 4 { 5 int i; 6 7 for( i = 0; i < 10; i++ ) { 8 printf("i :%d\n", i); 9 if( i == 5 ) { 10 break; 11 } 12 } 13 printf(" \n"); 14 return 0; 15 } 58

3 3.6: break 3.6 i :0 i :1 i :2 i :3 i :4 i :5 break 3.3.5 continue continue 59

3.4 2 3 int main(void) 4 { 5 int i; 6 for( i = 0; i < 5; i++ ) { 7 printf("i :%d\n", i); 8 if( i <= 2 ) { 9 continue; 10 } 11 /* i <= 2 */ 12 printf("loop-----\n"); 13 } 14 return 0; 15 } i :0 i :1 i :2 i :3 LOOP i :4 LOOP continue break for do while 3.4 2 16 60

3 3.4.1 C, 4. 0 1 & AND( OR ^ XOR ~ NOT AND 1 1 0 & 0 => 0 0 & 1 => 0 1 & 0 => 0 1 & 1 => 1 2 1 1 AND 0 0 1 1 OR 1 1 0 0 => 0 0 1 => 1 1 0 => 1 1 1 => 1 OR 0 + 0 0 1 1 1 XOR( EOR) XOR 2 1 1 1 0 ^ 0 => 0 0 ^ 1 => 1 1 ^ 0 => 1 1 ^ 1 => 0 61

3.4 NOT 1 ~0 => 1 ~1 => 0 2 3 int main(void) 4 { 5 unsigned char and,or,xor,not; 6 unsigned char hoge,piyo; 7 8 hoge = 187; /* 10111011 */ 9 piyo = 86; /* 01010110 */ 10 11 and = hoge & piyo; 12 or = hoge piyo; 13 xor = hoge ^ piyo; 14 not = ~piyo; 15 16 printf("hoge AND piyo = %d\n",and); 17 printf("hoge OR piyo = %d\n",or); 18 printf("hoge XOR piyo = %d\n",xor); 19 printf(" NOT piyo = %d\n",not); 20 21 return 0; 22 } 10 187 2 10111011 10 2 hoge AND piyo = 18 hoge OR piyo = 255 hoge XOR piyo = 237 NOT piyo = 169 62

3 hoge 1 0 1 1 1 0 1 1 (187) 1 0 1 1 1 0 1 1 (187) AND piyo 0 1 0 1 0 1 1 0 (86) OR 0 1 0 1 0 1 1 0 (86) -------------------------- ---------------------- 0 0 0 1 0 0 1 0 (18) 1 1 1 1 1 1 1 1 (255) hoge 1 0 1 1 1 0 1 1 (187) XOR piyo 0 1 0 1 0 1 1 0 (86) NOT 0 1 0 1 0 1 1 0 (86) -------------------------- ---------------------- 1 1 1 0 1 1 0 1 (237) 1 0 1 0 1 0 0 1 (169) 3.4.2 C. 00000011 2 00001100 0. << >> 2. 2 int main(void) 3 { 4 unsigned char hoge = 4; 5 6 hoge = hoge << 3; 7 printf("hoge 3 :%d",hoge); 8 return 0; 9 } 2 63

3.5 hoge 3 :32 hoge 8 hoge 0 0 0 0 0 1 0 0 (4) ------------------------------- hoge << 3 0 0 1 0 0 0 0 0 (32) 3.5 3.5.1, 1 /* 1 */ 2 #include <stdio.h> 3 4 int main(void) 5 { 6 char source[100], str[100]; 7 int i; 8 9 printf(" :"); 10 scanf("%s", source ); 11 12 for( i = 0; source[i]!= \0 ; i++ ){ 13 str[i] = source[i]; 14 } 15 str[i] = \0 ; 16 17 printf(" %s ",str); 18 return 0; 19 } 64

3 NULL for source 0 str source[i] NULL. 1 /* 2 */ 2 #include <stdio.h> 3 4 int main(void) 5 { 6 char source[100], str[100]; 7 int i; 8 9 printf(" :"); 10 scanf("%s", source ); 11 12 i = 0; 13 while( source[i]!= \0 ) { 14 str[i] = source[i]; 15 i++; 16 } 17 str[i] = \0 ; 18 19 printf(" %s ",str); 20 return 0; 21 } :HELLOWORLD HELLOWORLD 3.5.2 10,. 65

3.5 1 /* */ 2 #include <stdio.h> 3 4 int main(void) 5 { 6 int array[10]; 7 int i,j,max; 8 9 printf("10 \n"); 10 11 for( i = 0 ; i < 10 ; i++ ) { 12 printf("%d :",i+1); 13 scanf("%d", array ); 14 } 15 16 for( j = 1, max = array[0] ; j < 10; j++) { 17 if( max < array[j] ) { 18 max = array[j]; 19 } 20 } 21 22 printf(" %d ",max); 23 return 0; 24 } 1 :5 2 :8 3 :13 4 :4 5 :90 6 :54 7 :30 8 :52 9 :70 10 :1 90 66

3 3.5.3. 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 2 3 int main(void) 4 { 5 int i,j; 6 7 for( i = 1 ; i < 10 ; i++ ) { 8 for(j = 1 ; j!= 10 ; j++ ) { 9 printf("%3d ",i * j); 10 } 11 printf("\n"); 12 } 13 return 0; 14 } printf %3d 3.. 5,%5d. 67

3.5 3.5.4 10 16 0 0 1 1 ) 65 41 A 66 42 B 2 3 int main(void) 4 { 5 int hoge; 6 7 for( hoge = 0; hoge < 256 ; hoge++ ) { 8 printf("%3d %3x %c\n",hoge,hoge,hoge); 9 } 10 return 0; 11 } 3.5.5, 2 3 int main(void) 68

3 4 { 5 int hoge, piyo; 6 int select; 7 8 printf("1 : \n" 9 "2 : \n"); 10 printf(" :"); 11 12 scanf("%d",&select); 13 printf("hoge :"); scanf("%d",&hoge); 14 printf("piyo :"); scanf("%d",&piyo); 15 16 switch(select) { 17 case 1: printf("%d + %d = %d\n",hoge,piyo, hoge + piyo); 18 break; 19 case 2: printf("%d - %d = %d\n",hoge,piyo, hoge - piyo); 20 break; 21 } 22 return 0; 23 } 1 : 2 : :1 hoge :15 piyo :10 15 + 10 = 25 3.5.6 5,60 69

3.5 2 3 int main(void) 4 { 5 6 int student[5]; 7 int i; 8 9 for(i = 0; i < 5; i++){ 10 printf(" %d :",i + 1); 11 scanf("%d",&student[i]); 12 } 13 for(i = 0; i < 5; i++){ 14 if(student[i] > 60) { 15 printf(" %d %d \n",i+1, student[i]); 16 } 17 } 18 return 0; 19 } 1 :68 2 :57 3 :30 4 :90 5 :88 1 68 4 90 5 88 70

3 3.5.7 IP IP AND IP 192.168.12.9 255.255.255.0 2 int main(void) 3 { 4 unsigned char ip_address[4] = {192,168,12,9}; 5 unsigned char subnetmask[4] = {255,255,255,0}; 6 unsigned char net_add[4] = {0}; 7 int i; 8 9 for(int i = 0; i < 4; i++){ 10 net_add[i] = ip_address[i] & subnetmask[i]; 11 } 12 printf("network_address is %d.%d.%d.%d\n", 13 net_add[0],net_add[1],net_add[2],net_add[3]); 14 return 0; 15 } network address is 192.168.12.0 3.6 1,. 0-7 0 7 7 71

3.6 2 3, 3 4, NULL \0). 5 ***** *** * * *** *****. 6 10 7 Fizz Buzz 1 3 Fizz 5 Buzz Fizz Buzz [wikipedia ] 72

3 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz Buzz, 31, 32, Fizz, 34, Buzz, Fizz,... 1 100 3 Fizz 5 Buzz 3 5 FizzBuzz modulo % 8 Fizz Buzz 3.7 1 hoge, piyo, foo, bar. 2 3 int main(void) 4 { 5 int hoge = 0, piyo = 0; 6 int foo = 0, bar = 0; 7 8 do{ 9 printf("hoge\n"); 10 }while( hoge++ < 3); 11 12 while( piyo++ < 3){ 13 printf("piyo\n"); 14 } 15 73

3.7 16 do{ 17 printf("foo\n"); 18 } while(++foo < 3); 19 20 while(++bar < 3){ 21 printf("bar\n"); 22 } 23 return 0; 24 } 2 IPAddress 124.33.214.39, Subnetmask 255.255.255.224 2 124.33.214.32 124.33.214.63 3 WWW. ISP 122.18.130.93 /29. IP. 3 2 int main() 3 { 4 unsigned char ip_address[4] = {210,175,166,160}; 5 unsigned char subnetmask[4] = {255,255,255,248}; 6 unsigned char net_add[4] = {0}; 7 unsigned char bro_add[4] = {0}; 8 int host_num = 0; 74

3 10 9 int i; 11 for(int i = 0; i < 4; i++){ 12 net_add[i] = ip_address[i] & subnetmask[i]; 13 } 14 15 /* */ 16 for(int i = 0; i < 4; i++){ 17 if(subnetmask[i]!= 255){ 18 bro_add[i] = net_add[i] + ~subnetmask[i]; 19 }else{ 20 bro_add[i] = ip_address[i]; 21 } 22 } 23 24 printf("network_address is %d.%d.%d.%d\n", 25 net_add[0],net_add[1],net_add[2],net_add[3]); 26 printf("broadcast_address is %d.%d.%d.%d\n", 27 bro_add[0],bro_add[1],bro_add[2],bro_add[3]); 28 29 for(int i = 0; i < 4; i++){ 30 host_num += (bro_add[i] - net_add[i]) << 24-8 * i; 31 } 32 33 /* */ 34 host_num = host_num - 1; 35 printf(" %d \n",host_num); 36 37 return 0; 38 } 75

77 4 4.1 C printf scanf 4.2 2 2 3 int Plus(int x, int y) 4 { 5 int result; 6 result = x + y; 7 return result; 8 } 9 10 int main(void) 11 { 12 int hoge,piyo; 13 int n; 14 15 printf("2 \n"); 16 printf("1 :"); scanf("%d",&hoge); 17 printf("2 :"); scanf("%d",&piyo); 18 19 n = Plus(hoge,piyo); 20 printf(" %d \n",n); 21

4.2 22 return 0; 23 } 1 5 2 10 2 1 :5 2 :10 15 ( ) { } main Plus 4.1: printf Plus main n = Plus( hoge, piyo); Plus n n n int 78

4 Plus( hoge, piyo ); ( ) 4.2 4.2: 4.2 void Test(int x) { x = 10; } int main(void) { int hoge; Test( hoge ) } return 0; x hoge x hoge Test 79

4.3 void void void Test2(void) { printf("test FUNCTION\n"); } int main(void) { Test2(); return 0; } return Plus result 4.3 C 2 3 int main(void) 4 { 5 Test2(); 6 return 0; 7 } 8 9 void Test2(void) 10 { 11 printf("test FUNCTION\n"); 12 } main Test2 Test2 80

4 2 3 void Test2(void); /* */ 4 5 int main(void) 6 { 7 Test2(); 8 return 0; 9 } 10 11 void Test2(void) 12 { 13 printf("test FUNCTION\n"); 14 } main Test2 Test2() ( ; ) 4.4 ( 2 3 void show(int array[5], int n) 4 { 5 int i; 6 for( i = 0; i < n ; i++ ) { 7 printf("%d\n", array[i]); 8 } 9 } 10 11 int main() 12 { 13 int array[] = {1, 2, 3, 4, 5}; 81

4.4 14 show( array, 5 ); 15 return 0; 16 } array 1 2 3 4 5 show array show void show(int array[], int n) { int i; for( i = 0; i < n ; i++ ) { printf("%d\n", array[i]); } } array show void show(int *array, int n) { int i; for( i = 0; i < n ; i++ ) { printf("%d\n", array[i]); } } 82

4 4.5 main main main main int BP_SYM( ) { result = main(argc,argv, environ); } exit(result); main exit exit 0 0 main int main(void) { return 1; } main void main(void) { /* */ } void main(void) 83

4.6 4.6 C printf scanf printf stdio.h #include <stdio.h> printf 4.6.1 string.h #include <string.h> strcpy char *strcpy(char* str1, const char* str2); strcpy const 2 #include <string.h> 3 4 int main(void) 5 { 6 char source[20] = "hello"; 7 char dest[20]; 8 9 strcpy( dest, source ); 10 printf(" %s \n", dest); 11 return 0; 12 } 84

4 4.3: strcpy hello strncpy char *strncpy(char* str1, const char* str2, size_t n); n str2 n n, NULL str2 n,str1 NULL NULL strcpy strncpy NULL 2 #include <string.h> 3 4 int main(void) 5 { 6 char source[20] = "helloworld"; 7 char dest[20]; 8 9 strncpy( dest, source, 5); 10 dest[5] = \0 ; /* */ 11 printf(" %s \n", dest); 12 return 0; 85

4.6 13 } hello strlen size_t strlen(const char* str); strlen str NULL 1 size t unsigned int 2 #include <string.h> 3 4 int main(void) 5 { 6 char source[20] = "hello"; 7 printf(" %d \n", strlen(source) ); 8 return 0; 9 } 5 strcat char* strcat(char* str1, const char* str2); strcat str1 str2 2 #include <string.h> 3 1 unsigned int. 86

4 4 int main(void) 5 { 6 char source[80] = "world"; 7 char dest[80] = "hello"; 8 9 strcat(dest, source); 10 printf(" %s\n", dest ); 11 return 0; 12 } helloworld strncat char* strncat(char* str1, const char* str2, size_t n); strncat str1 str2 n 2 #include <string.h> 3 4 int main(void) 5 { 6 char dest[20] = "ABC"; 7 char source[20] = "DEFGH"; 8 9 strncat(dest, source, 3); 10 printf(" %s\n", dest); 11 return 0; 12 } ABCDEF 87

4.6 strcmp int strcmp(const char* str1, const char* str2); strcmp str1 str2 0 str1 str2 str1 str2 0 0 2 #include <string.h> 3 4 int main(void) 5 { 6 char source[80] = "hello"; 7 char dest[80] = "hello"; 8 9 if( strcmp(source, dest) == 0 ) { 10 printf(" \n"); 11 } else { 12 printf(" \n"); 13 } 14 return 0; 15 } 4.6.2 88

4 putchar int putchar(int c); c 1 stdio.h 2 3 int main(void) 4 { 5 putchar( a ); 6 } a puts int puts (const char *str); str printf %d \n stdio.h 2 3 int main(void) 4 { 5 puts("hello world"); 6 } hello world 89

4.6 getchar int getchar(void); stdio.h 2 3 int main() 4 { 5 char ch; 6 7 printf(" "); 8 ch = getchar(); 9 10 printf(" :%c\n", ch); 11 return 0; 12 } a a a gets char *gets (char *str); scanf stdio.h 2 3 int main() 90

4 4 { 5 char str[80]; 6 7 printf(" "); 8 gets( str ); 9 printf(" :%s\n", str); 10 return 0; 11 } hello world hello world hello world fgets char *fgets( char *string, int n, FILE *stream ); stream n - 1 string stream stream stdin stdio.h 2 3 int main() 4 { 5 char str[80]; 6 7 printf(" "); 8 fgets(str, 6, stdin); 9 printf(" :%s\n", str); 10 return 0; 11 } 91

4.6 helloworld helloworld hello 4.6.3 math.h sin double sin( double arg ); sin arg sin 2 #include <math.h> 3 4 int main(void) 5 { 6 double value = -1.0; 7 printf("%f sin %f \n",value,sin(value)); 8 9 return 0; 10 } -1.000000 sin -0.841471 92

4 cos double cos( double arg ); cos arg cos 2 #include <math.h> 3 4 int main(void) 5 { 6 double value = -1.0; 7 printf("%f cos %f \n",value,cos(value)); 8 9 return 0; 10 } -1.000000 cos 0.540302 pow double pow(double x, double y); pow x y 2 #include <math.h> 3 4 int main(void) 5 { 6 double x = 2.0, y = 5.0; 93

4.6 7 printf("%f %f %f ", x, y, pow(x, y) ); 8 9 return 0; 10 } 2.000000 5.000000 32.000000 4.6.4 atoi int atoi( const char *string ); string int stdlib.h 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 char str[]="150"; 7 8 printf(" %s %d \n", str, atoi(str) ); 9 return 0; 10 } 150 150 94

4 toupper int toupper( int c ); c stdlib.h 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 char ch = a ; 7 printf("%c %c \n", ch, toupper(ch) ); 8 9 return 0; 10 } a A tolower int tolower( int c ); c stdlib.h 2 #include <stdlib.h> 3 4 int main(void) 95

4.7 5 { 6 char ch = A ; 7 printf("%c %c \n", ch, tolower(ch) ); 8 9 return 0; 10 } A a 4.7 4.7.1 strcpy strcpy my strcpy 2 3 void my_strcpy(char dest[], char source[]) 4 { 5 int i; 6 7 for( i = 0; source[i]!= \0 ; i++ ) { 8 dest[i] = source[i]; 9 } 10 dest[i] = \0 ; 11 } 12 13 int main() 14 { 15 char source[100] = "hello"; 16 char dest[100]; 17 18 my_strcpy(dest, source); 19 printf(" %s \n", dest); 96

4 20 return 0; 21 } strcpy hello 4.7.2 strlen strlen my strlen 2 3 int my_strlen(char str[]) 4 { 5 int i = 0; 6 7 for( i = 0; str[i]!= \0 ; i++ ) 8 ; 9 return i; 10 } 11 12 int main() 13 { 14 char buf[100] = "hello"; 15 16 printf("%s %d \n", buf, my_strlen(buf) ); 17 return 0; 18 } hello 5 97

4.7 4.7.3 strcat strcat my strcat 2 #include <string.h> 3 4 void my_strcat(char dest[], char source[]) 5 { 6 int i; 7 int len = strlen( dest ); 8 9 for( i = 0; source[i]!= \0 ; i++ ) { 10 dest[ i + len ] = source[i]; 11 } 12 dest[i + len] = \0 ; 13 } 14 15 int main(void) 16 { 17 char source[100] = "world"; 18 char dest[100] = "hello"; 19 20 my_strcat( dest, source ); 21 printf(" %s\n", dest ); 22 return 0; 23 } helloworld 98

4 4.7.4 2 Power( 2, 5 ) 2 3 int Power(int x, int y); /* */ 4 5 int main(void) 6 { 7 int xi,yi; 8 9 printf(" :"); 10 scanf("%d",&xi); 11 printf(" :"); 12 scanf("%d",&yi); 13 14 printf("%d %d %d \n",xi,yi,power(xi,yi)); 15 return 0; 16 } 17 18 int Power(int x, int y) 19 { 20 int i,result; 21 22 for( i=1, result = 1; i <= y; i++) { 23 result *= x; 24 } 25 return result; 26 } 2 5 2 5 32 99

4.8 4.8 1 strncpy 2 strncat 3 reverse 4 source ch 5 palindrome radar madamimadam( = Madam, I m Adam ) 100

101 5 5.1 5.1.1 2 void func(void); 3 4 int main(void) 5 { 6 int hoge; 7 ) 8 return 0; 9 } 10 11 void func(void) 12 { 13 int hoge; 14 } int hoge hoge main hoge main main hoge func hoge func 2 void func(void) 3 4 int main(void)

5.1 5 { 6 int hoge; 7 ( ) 8 return 0; 9 } 10 void func(void) 11 { 12 hoge = 10; 13 } hoge main main 2 3 int hoge; /* */ 4 5 void func(void); 6 7 int main(void) 8 { 9 int hoge; /* */ 10 hoge = 20; 11 ( ) 12 return 0; 13 } 14 void func(void) 15 { 16 int x; 17 int hoge = 10; /* */ 18 19 for(x=0; x < 10 ; x++ ) { 20 int hoge; /* */ 21 hoge = x; 22 } 23 } 1 hoge 1 hoge main 102

5 hoge hoge 1 hoge 2 hoge main main hoge 3 hoge func 4 hoge for int main(void) { int hoge; int hoge; } return 0; 5.1.2 static 2 3 int main(void) 4 { 5 static int x = 10; 6 ( ) 7 return 0; 8 } static static static 2 3 void func(void); 4 5 int main(void) 103

5.2 6 { 7 func(); 8 func(); 9 func(); 10 return 0; 11 } 12 13 void func(void) 14 { 15 static int hoge = 10; 16 hoge++; 17 printf("hoge = %d\n",hoge); 18 } hoge = 11 hoge = 12 hoge = 13 func 10 static 5.2 5.2.1 #define #define #define A B #define A B 2 #define NUMBER 10 3 4 int main(void) 104

5 5 { 6 printf("%d",number); 7 return 0; 8 } NUMBER 10 NUMBER #define #define 5.2.2 #undef #undef #undef A #undef NUMBER #define NUMBER 11 #undef NUMBER 11 5.2.3 define 2 3 #define SUM( x, y ) ( (x) + (y) ) 4 5 int main(void) 6 { 7 int sum; 8 105

5.2 9 sum = SUM(3,5); 10 printf("%d",sum); 11 12 return 0; 13 } SUM(3,5) ((3) + (5)) #define SQR(x) x*x int x = SQR( a + b ); int y = SQR( a ) / SQR( b ); a b int x = a+b * a+b; int y = a*a / b*b; a 3 b 4 x 7 49 3+4*3+4 3+12+4 19 y #define SQR(x) ((x)*(x)) 5.2.4 #include #include printf printf printf stdio.h stdio.h printf scanf 106

5 #include < > #include " " #include 2 < > 5.3 1 2 3 int num = 10; 4 5 void func(int number) 6 { 7 printf("num = %d\n", num); 8 number = 20; 9 } 10 11 int main(void) 12 { 13 int num = 3; 14 15 printf("num = %d\n", num); 16 func( num ); 17 printf("num = %d\n", num); 18 return 0; 19 } 107

5.3 2 2 3 #define TIMES( x, y ) ( x * y ) 4 5 int main(void) 6 { 7 int num; 8 9 num = TIMES( 3 + 5, 5 + 3 ); 10 printf("%d\n",num); 11 12 return 0; 13 } 108

109 6 C 1 6.1 hoge C int hoge int hoge; hoge int hoge hoge 6.1 hoge hoge 100 p_piyo p_piyo hoge 6.6 150

6.1 6.1: 6.2: hoge p piyo 110

6 int int double double double int int p piyo int* p_piyo; int *p_piyo; int *p_hoge,*p_piyo p hoge p piyo int* p_hoge, p_piyo p hoge p piyo int 6.2 int hoge; int 6.1 6.1 int hoge 100 int* p_piyo 6.3 p piyo 111

6.2 6.3: int hoge; int* p_piyo; hoge = 10; p_piyo = &hoge; int hoge int( hoge hoge = 10; hoge 10 p_piyo p_piyo = hoge; 6.4 p_piyo int* int* (int ( 6.4: p_piyo hoge & p_piyo = &hoge 6.5 112

6 6.5: & &hoge hoge 6.3 2 3 int main(void) 4 { 5 int hoge; 6 int* p_piyo; 7 8 hoge = 10; 9 p_piyo = &hoge; 10 11 printf("hoge address :%p\n", &hoge); 12 printf("hoge :%d\n", hoge); 13 printf("p_piyo :%p\n", p_piyo); 14 printf("*p_piyo:%d\n", *p_piyo); 15 printf("p_piyo address :%p\n", &p_piyo); 16 } printf %p 6.6 113

6.3 6.6: printf("hoge address :%p\n",&hoge); & hoge address : 100 1 printf("hoge :%d\n",hoge); hoge printf("p_piyo :%p\n",p_piyo); p_piyo printf("*p_piyo:%d\n",*p_piyo); *p_piyo *p_piyo : 10 hoge *p_piyo p_piyo p_piyo 100 100 hoge *p_piyo * * int* p_hoge; /* * */ 1 100 OS hoge address :0012F580 114

6 int piyo = 10; p_hoge = &piyo; /* p_hoge piyo * */ printf("%d", *p_hoge); * * printf("p_piyo address :%p\n", &p_piyo); hoge address : 100 hoge : 10 p_piyo : 100 *p_piyo : 10 p_piyo address : 150 C void* 1 int main(void) 2 { 3 int hoge = 10; 4 void* p_piyo = &hoge; 5 6 /* bar = *p_piyo */ 7 int bar = *( (int*)p_piyo ); 8 9 return 0; 10 } int bar = *p_piyo; 115

6.4 NULL p piyo ( hoge) int* 6.1 6.6 6.6 6.7 p_piyo hoge *p_piyo 6.7: 6.4 NULL int hoge; printf("%d",hoge); hoge int* p p p 116

6 p = NULL; p NULL p NULL 2 NULL #define NULL 0 p = NULL p = 0 p = 0 #define NULL ( (void*)0 ) NULL int* p = NULL printf("%d",*p); p 3 int* int* p_piyo = 10; NULL 0 int* p_piyo = 0; C 0 0 NULL 4 6.5 swap 1 void swap( int x, int y ) 2 { 3 int tmp = x; 2 NULL 3 Java NullPointerException 4 0 NULL JAVA null 117

6.5 4 x = y; 5 y = tmp; 6 } 2 3 void swap( int x, int y ) 4 { 5 int tmp = x; 6 x = y; 7 y = tmp; 8 } 9 10 int main(void) 11 { 12 int hoge = 10, piyo = 5; 13 14 swap( hoge, piyo ); 15 printf("hoge : %d, piyo : %d\n",hoge, piyo); 16 return 0; 17 } hoge : 10, piyo : 5 swap. 4.2 6.8 x y hoge piyo 2 118

6 6.8: 3 void swap(int* x, int* y); /* */ 4 5 int main(void) 6 { 7 int hoge = 10, piyo = 5; 8 9 swap( &hoge, &piyo ); 10 printf("hoge : %d, piyo : %d\n",hoge,piyo); 11 return 0; 12 } 13 14 void swap(int* x, int* y) 15 { 16 int tmp = *x; 17 *x = *y; 18 *y = tmp; 19 } 6.9 *x hoge *y piyo hoge : 5, piyo : 10 hoge piyo &hoge scanf 119

6.6 6.9: & 6.6 int char double 2 6.1 6.1: short -32768 +32767 int -2147483648 +2147483647 long -2147483648 +2147483647 unsigned short 0 +65535 unsigned int 0 +4294967295 unsigned long 0 +4294967295 char -128 +127 0 +255 unsigned char 0 +255 float 3.4 * 10^-38 3.4 * 10^+38 double 1.7 * 10^-308 1.7 * 10^+308 120

6 int -2147483648 +2147483647 char -128 127 0 255 char 1 int 4 5 1 8 8 2 8 8 00000000 11111111 char 10 0 255 6. char 1 0 255 256 6.6.1 sizeof sizeof sizeof int return 2 int main(void) 3 { 4 char hoge; 5 int piyo; 6 7 printf("char %d ", sizeof(hoge) ); 8 printf("int %d ", sizeof(piyo) ); 9 return 0; 10 } 7 5 6-128 +127 7 121

6.6 char 1 int 4 6.6.2 2 int main(void) 3 { 4 char* p_hoge; 5 int* p_piyo; 6 7 printf("char* %d \n", sizeof(p_hoge) ); 8 printf("int* %d \n", sizeof(p_piyo) ); 9 return 0; 10 } char* 4 int* 4 4 8 2 int main(void) 3 { 4 int array[5] = {5,10,15,20,25}; 5 int* p; 8 int* char* 122

6 6 p = &array[0]; 7 8 printf("p[0] = %d\n",*p); 9 p++; 10 printf("p[1] = %d\n",*p); 11 } main 3 6.10 6.11 6.10: 6.11: 123

6.6 Array[0] 100 int 4 Array[1] 104 p++; p 1 100 + 1 101 int 4 100 + 4 104 p++; 6.12 6.12: 4 int* char* 1 100 +1 =101 p[0] = 5 p[1] = 10 124

6.7 6 6.7.1 2 3 int main(void) 4 { 5 int array[5] = { 1, 2, 3, 4, 5 }; 6 int* p; 7 8 p = &array[0]; 9 10 printf("*(p+0) : %d\n", *(p + 0) ); 11 printf("*(p+1) : %d\n", *(p + 1) ); 12 printf("*(p+3) : %d\n", *(p + 3) ); 13 return 0; 14 } int array[5] = { 1, 2, 3, 4, 5 }; int* p; p = &array[0]; 6.13 array[0] 100 int 4 array[1] 104 array array[0] array[4] 5 array array[4] 116 int* p p = array[0]; 125

6.7 6.13: array[0] int p int* (int) int* array[0] p = &array[0]; & array[1] p p = &array[1]; printf("*(p+0) : %d\n", *(p+0) ); printf("*(p+1) : %d\n", *(p+1) ); printf("*(p+3) : %d\n", *(p+3) ); *p p 100 *p 100 array[0] *(p + 0) *(p) *p p 1 6.13 6.14 126

6 6.14: printf("*(p+0) : %d\n", *(p+0) ); array[0] 1 printf("*(p+0) : %d\n", *(p+1) ); p 100 1 101 int size 4 p+1 int 4 p p+1 p 100 int 4 104 *(p+0) *(100 ) *(p+1) *(100 +4 ) *(104 ) 2 array[1] printf("*(p+0) : %d\n", *(p+3) ); *(p+0) : 1 *(p+1) : 2 *(p+3) : 4 127

6.7 *( p + 1 ); p + 1 2 3 int main(void) 4 { 5 int array[5] = { 1, 5, 10, 15, 20 }; 6 int* p; 7 8 p = &array[0]; 9 printf("array[0] : %d\n", *(p + 0) ); 10 printf("array[1] : %d\n", *(p + 1) ); 11 12 printf("*p+1 : %d\n", *p + 1 ); 13 14 return 0; 15 } array[0] : 1 array[1] : 5 *p+1 : 2 *(p + 1) *p + 1 3 + 5 9 48 + * 5 9 3 3 + 5 9 (3 + 5) 9 C *p + 1 * + * *p *p 1 1 + 1 2 printf("*p+1 : %d\n", *p + 1 ); 2 p 1 128

6 printf("*p+1 : %d\n", *(p + 1) ); 2 3 int main(void) 4 { 5 int array[5] = { 1, 2, 3, 4, 5 }; 6 int* p; 7 8 p = &array[0]; 9 10 printf("*(p+0) : %d\n", *(p+0) ); 11 printf("*(p+1) : %d\n", *(p+1) ); 12 printf("*(p+3) : %d\n", *(p+3) ); 13 return 0; 14 } p = &array[0]; p = array; 2 3 int main(void) 4 { 5 int array[5] = { 1, 2, 3, 4, 5 }; 6 int* p; 7 8 p = array; /* p array */ 9 10 printf("*(p+0) : %d\n", *(p+0) ); 11 printf("*(p+1) : %d\n", *(p+1) ); 129

6.7 12 printf("*(p+3) : %d\n", *(p+3) ); 13 return 0; 14 } array p 6.7.2 2 3 int main(void) 4 { 5 int array[5] = { 1, 2, 3, 4, 5 }; 6 int* p; 7 8 p = array; /* p array */ 9 10 printf("p[0] : %d\n", p[0] ); 11 printf("p[1] : %d\n", p[1] ); 12 printf("*(p+1) : %d\n", *(p + 1) ); 13 return 0; 14 } p[0] : 1 p[1] : 2 *(p+1) : 2 p[0] p array p[0] *(p + 0) p[1] *(p + 1) *(p + 1) p[1] 130

6 2 3 int main(void) 4 { 5 int array[5] = { 1, 2, 3, 4, 5 }; 6 int* p; 7 8 p = &array[1]; /* array[1] */ 9 10 printf("p[0] : %d\n", p[0] ); 11 printf("p[1] : %d\n", p[1] ); 12 printf("p[-1]: %d\n", p[-1] ); 13 return 0; 14 } p[0] : 2 p[1] : 3 p[-1]: 1 p[-1] array[-1] p[-1] *(p - 1) *(p + 1) *(1 + p) (3 + 5) (5 + 3) *(1 + p) 1[p] 6.7.3 int i; 2 3 void myputs(char str[20]) 131

6.7 4 { 5 int i; 6 for( i = 0; str[i]!= \0 ; i++ ) { 7 putchar( str[i] ); 8 } 9 printf("\n"); 10 } 11 12 int main(void) 13 { 14 char str[20] = "Hello world"; 15 myputs( str ); 16 return 0; 17 } Hello world myputs str char str[20] char* str main 2 int main(void) { char str[20]; char* str2; } str str2 char str[20] char* str myput void myputs(char* str) { int i; for( i = 0; str[i]!= \0 ; i++ ) { putchar(str[i]); 132

6 } } printf("\n"); void myputs(char str[]) void myputs(char* str); void myputs(char str[20]); void myputs(char str[]); myputs( str ); myputs( &str[0] ) main char str[10000]; myputs 6.7.4 int array[5] int 5 int* p[5] int* 5 2 133

6.7 3 int main(void) 4 { 5 int array[5] = {1, 2, 3, 4, 5}; 6 int* p[5]; 7 int i; 8 9 for(i = 0; i < 5 ; i++) { 10 p[i] = &array[i]; 11 printf("p[%d] : %p\n", i, p[i]); 12 } 13 14 *p[0] += 10; 15 16 for(i = 0; i < 5 ; i++) { 17 printf("*p[%d] : %d\n", i, *p[i]); 18 } 19 } for array[i] p[i] p[i] 6.15 for p[0] : 100 p[1] : 104 p[2] : 108 p[3] : 112 p[4] : 116 *p[0] += 10 p[0] 10 100 10 array[0] 10 for *p[i] p[i] 134

6 6.15: p[0] : 100 p[1] : 104 p[2] : 108 p[3] : 112 p[4] : 116 *p[0] : 11 *p[1] : 2 *p[2] : 3 *p[3] : 4 *p[4] : 5 6.8 135

6.8 2 3 int main(void) 4 { 5 char* str = "Hello"; 6 printf("%s\n", str ); 7 8 return 0; 9 } Hello 6.16 str 6.16: char* str = "Hello" char str2[10] = "Hello" 6.17 str char str2 136

6 6.17: 2 3 int main(void) 4 { 5 char* str = "Hello"; 6 char str2[10] = "Hello"; 7 8 str[0] = B ; /* */ 9 str2[0] = B ; /* OK */ 10 11 return 0; 12 } str[0] *(str + 0) str str[0] = B str2 137

6.9 6.9 char int char* int* 2 3 int main(void) 4 { 5 int hoge = 10; 6 int* p_hoge; 7 int** p_p_hoge; 8 9 p_hoge = &hoge; 10 p_p_hoge = &p_hoge; 11 12 printf("hoge = %d\n", hoge); 13 printf("*p_hoge = %d\n", *p_hoge); 14 printf("p_hoge address = %p\n", &p_hoge); 15 printf("p_p_hoge = %p\n", p_p_hoge); 16 printf("**p_p_hoge = %d\n", **p_p_hoge); 17 18 return 0; 19 } 10 6.18 6.19 6.18: 138

6 6.19: p hoge hoge p p hoge int* p p hoge p hoge hoge 6.20 6.20: **p p hoge *( *p p hoge ) *( p hoge ) hoge = 10 *p_hoge = 10 p_hoge address = 150 p_p_hoge = 150 **p_p_hoge = 10 139

6.10 6.10 6.10.1 strlen strlen my strlen my strlen 2 3 int my_strlen(const char* str) 4 { 5 int i = 0; 6 while( *str++ ) { 7 i++; 8 } 9 return i; 10 } 11 12 int main(void) 13 { 14 char str[20] = "Hello world"; 15 printf("%s %d \n", str, my_strlen(str)); 16 return 0; 17 } const char* str my strlen str[i] = E ; *(str + i) = E ; *str++; *(str++); NULL NULL \0 0. while( 0 ) 140

6 6.10.2 strcat strcpy my strcpy 2 3 void my_strcpy(char* dest, const char* src) 4 { 5 while( *dest++ = *src++ ) 6 ; 7 } 8 9 int main(void) 10 { 11 char str[20] = "Hello world"; 12 char copy[20]; 13 14 my_strcpy(copy, str); 15 printf(" %s \n",copy); 16 return 0; 17 } 6.11 1 2 3 int main(void) 4 { 5 int hoge = 30; 6 int* piyo = &hoge; 7 8 printf("out1 :%d\n",hoge); 9 printf("out2 :%d\n",*piyo); 141

6.11 10 11 return 0; 12 } 2 2 3 int main(void) 4 { 5 int x = 10; 6 int y = 20; 7 int* hoge = &x; 8 int* piyo = &y; 9 int* tmp; 10 11 tmp = hoge; 12 hoge = piyo; 13 piyo = tmp; 14 15 printf("out1 :%d\n",x); 16 printf("out2 :%d\n",*hoge); 17 18 return 0; 19 } 3 2 3 int main(void) 4 { 5 int array[5] = {1,2,3,4,5}; 6 int* p = &array[0]; 7 142

6 8 p += 2; 9 printf("out1 :%d\n",*p); 10 11 return 0; 12 } 4 2 3 int main(void) 4 { 5 int array[5] = { 1, 5, 10, 15, 20 }; 6 int* p; 7 8 p = &array[0]; 9 10 printf("%d %d %d %d %d\n",*(p+0), *(p+1), p[2], 3[p], *p ); 11 p = array; 12 printf("%d %d %d %d %d\n",*(p+0), *(p+1), p[2], 3[p], *p ); 13 14 *p += 10; 15 printf("p[0] : %d\n",p[0]); 16 17 (*p)++; 18 printf("*p : %d\n",*p); 19 20 *(p++); 21 printf("*p : %d\n",*p); 22 } 5 2 143

6.11 3 int main(void) 4 { 5 char* p = "Hello world"; 6 7 printf("%s\n",p); 8 p[1] = a ; 9 printf("%s\n",p); 10 11 return 0; 12 } 6 hoge 100 i 200 j 300 k 400 2 3 int main(void) 4 { 5 int hoge = 10; 6 int* i = &hoge; 7 int** j = &i; 8 int*** k = &j; 9 10 printf("hoge = %d\n", hoge); 11 printf("&hoge= %p\n", &hoge); 12 printf("*i = %d\n", *i); 13 printf("&i = %p\n", &i); 14 printf("&j = %p\n", &j); 15 printf("*j = %p\n", *j); 16 printf("**j = %d\n", **j); 17 printf("k = %p\n", k); 18 printf("*k = %p\n", *k); 19 printf("**k = %p\n", **k); 20 printf("***k = %d\n", ***k); 21 22 return 0; 23 } 144

145 7 7.1 struct { }; 2 3 #define NUMBER_OF_STUDENT 3 4 5 struct Student{ 6 char student_name[100]; 7 int student_age; 8 }; 9 10 int main(void) 11 { 12 struct Student st[number_of_student]; 13 int i; 14 int num; 15 printf("%d \n",number_of_student); 16 17 for( i=0; i < NUMBER_OF_STUDENT ; i++ ) { 18 printf("%d :",i+1); 19 scanf("%s",st[i].student_name); 20 printf("%d :",i+1);

7.1 21 scanf("%d",&st[i].student_age); 22 } 23 24 for(;;) { 25 printf(" :"); 26 scanf("%d",&num); 27 28 if( num > NUMBER_OF_STUDENT num <= 0) { 29 printf(".\n"); 30 break; /* */ 31 } else { 32 printf(" : %s, : %d\n",st[num-1].student_name, 33 st[num-1].student_age); 34 } 35 } 36 return 0; 37 } struct Student st[number_of_student]; Student NUMBER_OF_STUDENT st[0].student_name 146

7 3 1 :nishio 1 :20 2 :itoh 2 :20 3 :yamada 3 :33 :2 : itoh, : 20 :1 : nishio, : 20 :3 : yamada, : 33 :0. 7.2 int int int* hoge; struct Student{ char student_name[100]; int student_age; }; Student struct Student* hoge; hoge hoge hoge->student_age -> - ( > 147

7.2 (*hoge).student_age hoge student_age *hoge.student_age (*hoge).student_age ( 7.2) *(hoge.student_age) 7.1: ( ) [ ] ->.! ~ ++ == = * sizeof * / + - << >> < <= > >= ==!=? = += -= *= /=, 148

7 7.3 typedef typedef typedef ; unsigned int typedef tyepdef unsigned int uint; unsigned int uint typedef 2 3 #define NUMBER_OF_STUDENT 3 4 5 typedef struct Student_tag{ 6 char student_name[100]; 7 int student_age; 8 } Student; 9 10 int main(void) 11 { 12 Student st[number_of_student]; 13 int i; 14 int num; 15 printf("%d \n",number_of_student); 16 17 for( i=0; i < NUMBER_OF_STUDENT ; i++ ) { 18 printf("%d :",i+1); 19 scanf("%s",st[i].student_name); 20 printf("%d :",i+1); 21 scanf("%d",&st[i].student_age); 22 } 23 24 for(;;) { 25 printf(" :"); 149

7.4 26 scanf("%d",&num); 27 28 if( num > NUMBER_OF_STUDENT num <= 0) { 29 printf(".\n"); 30 break; /* */ 31 } else { 32 printf(" : %s, : %d\n",st[num-1].student_name, 33 st[num-1].student_age); 34 } 35 } 36 return 0; 37 } 12 struct Student_tag st[number_of_student]; typedef typedef struct typedef struct Student_tag{ char student_name[100]; int student_age; } Student;, Student. 7.4 7.4.1 2 2 p1(x 1, y 1 ), p2(x 2, y 2 ) 2 2 d d = (x 2 x 1 ) 2 + (y 2 y 1 ) 2 2 #include <math.h> 3 150

7 4 typedef struct Point_tag{ 5 double x; 6 double y; 7 } Point; 8 9 int main(void) 10 { 11 Point p1, p2; 12 double distance; 13 14 printf("p1 (x,y) :"); 15 scanf("%lf %lf", &p1.x, &p1.y); 16 17 printf("p2 (x,y) :"); 18 scanf("%lf %lf", &p2.x, &p2.y); 19 20 distance = sqrt( (p2.x - p1.x) * (p2.x - p1.x) + 21 (p2.y - p1.y) * (p2.y - p1.y) ); 22 printf("2 : %f\n", distance ); 23 return 0; 24 } 151

153 8 8.1 C malloc 2 3 int main(void) 4 { 5 int buf[10]; 6 int number, i, sum; 7 8 printf(" : "); 9 scanf("%d", &number); 10 11 for(i = 0; i < number; i++) { 12 printf("%d :", i+1); 13 scanf("%d", &buf[i] ); 14 } 15 16 for(i = 0, sum = 0; i < number; i++ ) { 17 sum += buf[i]; 18 } 19 20 printf(" : %d\n", sum); 21 return 0; 22 }

8.1 buf 10 1000 int buf[1000]; 10 990 malloc stdlib.h = malloc( ); malloc 1 NULL free free( ); int 5 int* p; p = (int *)malloc( 4 * 5 ); if(!p ) { printf(" \n"); exit(1); /* */ } int 4 int 5 20bytes ( 8.1) int 4 int sizeof 1 malloc 154

8 8.1: p = (int *)malloc( sizeof(int) * 5 ); malloc 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 int* buf; 7 int number, i, sum; 8 9 printf(" : "); 10 scanf("%d", &number); 11 12 buf = (int *)malloc( number * sizeof(int) ); 13 14 if(!buf ) { 15 printf(" \n"); 16 exit(1); 17 } 18 19 for(i = 0; i < number; i++) { 155

8.2 20 printf("%d :", i+1); 21 scanf("%d", &buf[i] ); 22 } 23 24 for(i = 0, sum = 0; i < number; i++ ) { 25 sum += buf[i]; 26 } 27 28 printf(" : %d\n", sum); 29 free( buf ); 30 return 0; 31 } exit 0 0 0 0 : 5 1 :1 2 :2 3 :3 4 :4 5 :5 : 15 8.2 8.2.1 C 156

8 8.2.2 FILE fopen FILE* fp = fopen(char*, char* ); stdio.h fopen FILE FILE 8.2.2 8.1: r w a rb wb ab r+ / w+ / a+ / r fopen NULL w a ASCII Windows \n \r\n UNIX OS \n C OS 157

8.2 fclose fclose( FILE* ); 2 #include <stdlib.h> 3 4 int main() 5 { 6 char filename[100]; 7 FILE *fp; 8 9 printf(" :"); 10 scanf("%s", filename); 11 12 fp = fopen(filename, "r"); 13 if (fp == NULL) { 14 printf(" \n"); 15 exit (1); 16 } 17 printf(" \n"); 18 19 fclose(fp); 20 return 0; 21 } 8.2.3 fgetc int fgetc(file* ); 158

8 fgetc unsigned char 1 int testfile.txt hello world 363 2 #include <stdlib.h> 3 4 int main() 5 { 6 char filename[100] = "testfile.txt"; 7 FILE *fp; 8 int ch; 9 10 fp = fopen(filename, "r"); 11 if (fp == NULL) { 12 printf(" \n"); 13 exit (1); 14 } 15 16 for(;;) { 17 ch = fgetc( fp ); 18 if( ch == EOF ) { 19 break; 20 } 21 printf("%c", ch); 22 } 23 printf("\n"); 24 25 fclose(fp); 26 return 0; 27 } 159

8.2 hello world 363 fgetc ch EOF 2 EOF fgetc 1 fgets char* fgets(char* int, FILE* ); fgets 1 fgets -1 NULL fgets 2 #include <stdlib.h> 3 4 #define BUFSIZE 100 5 6 int main() 7 { 8 char filename[100] = "testfile.txt"; 9 char buf[bufsize]; 10 FILE *fp; 11 12 fp = fopen(filename, "r"); 13 if (fp == NULL) { 14 printf(" \n"); 15 exit (1); 16 } 17 18 while( fgets( buf, BUFSIZE, fp )!= NULL ) { 2 End of File 160

8 19 printf("%s", buf); 20 } 21 printf("\n"); 22 fclose(fp); 23 return 0; 24 } fgets fscanf scanf int fscanf(file*, char*,...); EOF fscanf 2 #include <stdlib.h> 3 4 #define BUFSIZE 100 5 6 int main() 7 { 8 char filename[100] = "testfile.txt"; 9 char buf[bufsize]; 10 FILE *fp; 11 12 fp = fopen(filename, "r"); 13 if (fp == NULL) { 14 printf(" \n"); 15 exit (1); 16 } 17 18 while( fscanf( fp, "%s", buf )!= EOF ) { 19 printf("%s\n", buf); 20 } 21 fclose(fp); 22 return 0; 23 } 161

8.2 8.2.4 1 fputc int fputc(int, FILE* ); hello out.txt 2 #include <stdlib.h> 3 4 int main() 5 { 6 char filename[100] = "out.txt"; 7 char buf[100] = "hello"; 8 FILE* fp; 9 int i; 10 11 fp = fopen(filename, "w"); 12 if (fp == NULL) { 13 printf(" \n"); 14 exit (1); 15 } 16 17 for( i = 0; buf[i]!= \0 ; i++ ) { 18 fputc( buf[i], fp ); 19 } 20 printf(" \n"); 21 fclose(fp); 22 return 0; 23 } w out.txt 162

8 fputs int fputs(char*, FILE* ); fputs EOF fputs 2 #include <stdlib.h> 3 4 int main() 5 { 6 char filename[100] = "out.txt"; 7 char buf[100] = "hello"; 8 FILE* fp; 9 10 fp = fopen(filename, "w"); 11 if (fp == NULL) { 12 printf(" \n"); 13 exit (1); 14 } 15 16 if( fputs( buf, fp )!= EOF ) { 17 printf(" \n"); 18 }else { 19 printf(" \n"); 20 } 21 fclose(fp); 22 return 0; 23 } fputs fprintf printf int fprintf(file*, char*,...); fprintf 163

8.2 2 #include <stdlib.h> 3 4 int main() 5 { 6 char filename[100] = "out.txt"; 7 char buf[100] = "hello"; 8 FILE* fp; 9 10 fp = fopen(filename, "w"); 11 if (fp == NULL) { 12 printf(" \n"); 13 exit (1); 14 } 15 16 fprintf( fp, "%s", buf ); 17 printf(" \n"); 18 fclose(fp); 19 return 0; 20 } 8.2.5 fgetc fgets EOF EOF. 3 #define EOF -1-1 EOF feof int feof(file* ); feof 0 0 textfile.txt feof 3-1 164