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

Similar documents
[1] #include<stdio.h> main() { printf("hello, world."); return 0; } (G1) int long int float ± ±

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

untitled

‚æ4›ñ

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


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

新版明解C言語入門編

橡Pro PDF

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

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

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

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

kiso2-09.key

kiso2-06.key

r03.dvi

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

(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

tuat1.dvi

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

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

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

ex14.dvi

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

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

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言語 実践編

ex12.dvi

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

新・明解C言語で学ぶアルゴリズムとデータ構造

PowerPoint Presentation

A/B (2018/06/08) Ver kurino/2018/soft/soft.html A/B


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

gengo1-2

untitled

Informatics 2014

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

超初心者用

数値計算

導入基礎演習.ppt

卒 業 研 究 報 告.PDF

r07.dvi

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

ohp07.dvi

/

P02.ppt

pptx

/* 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 */ }

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

main

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

10

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

1 4 2 EP) (EP) (EP)

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

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

Microsoft Word - 3new.doc

プログラミング基礎

C B

新・明解C言語 実践編

Informatics 2010.key

C 言語第 2 回 C 言語の構成要素 ( シラバス 3 4 回目 ) 1 1 予約語 ( 教科書 59 ページの 識別子と予約語 参照 ) コンピュータに実行してもらう命令で C 言語に特有の名前が付いている プログラムの制御構造 : 演算子 : if~else for( ){ } g

Informatics 2015

untitled

P03.ppt

gengo1-11

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

debug ( ) 1) ( ) 2) ( ) assert, printf ( ) Japan Advanced Institute of Science and Technology

kiso2-03.key

gengo1-12

1.ppt

初歩のC言語ターミナル_2014_May.pages

ohp08.dvi

(1 ) scanf(

BW BW

x h = (b a)/n [x i, x i+1 ] = [a+i h, a+ (i + 1) h] A(x i ) A(x i ) = h 2 {f(x i) + f(x i+1 ) = h {f(a + i h) + f(a + (i + 1) h), (2) 2 a b n A(x i )

r08.dvi

C

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

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

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

<4D F736F F D20438CBE8CEA8D758DC03389F0939A82C282AB2E646F63>

double float

Microsoft PowerPoint - 説明3_if文switch文(C_guide3)【2015新教材対応確認済み】.pptx

+ +

Microsoft PowerPoint - C言語の復習(配布用).ppt [互換モード]

P06.ppt

() / (front end) (back end) (phase) (pass) 1 2

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

gengo1-12

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

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

6 6.1 sound_wav_files flu00.wav.wav 44.1 khz 1/44100 spwave Text with Time spwave t T = N t N 44.1 khz t = 1 sec j t f j {f 0, f 1, f 2,, f N 1

数値計算

XMPによる並列化実装2

Transcription:

18 C ( ) 1 1 1.1 hello world.c 5 printf("hello World\n"); 6 } [ ] [ ] #include <stdio.h> % cc hello_world.c %./a.out Hello World [a.out ] % cc hello_world.c -o hello_world [ ( ) ] (K&R 4.1.1) #include "filename" #include <filename> cc -l ( math.h, K&R B4) % cc filename.c -lm 1. temperature1.c ( ) main() 3 { 4 int fahr, celsius; 5 int lower, upper, step; 6 lower = 0; 8 upper = 300; 9 step = 0; 10 fahr = lower; 11 while (fahr <= upper) { 1 celsius = 5 * (fahr-3) / 9; 13 printf("%d\t%d\n", fahr, celsius); 14 fahr = fahr + step; 15 } 16 } [] \n, \t tab, \b, \", \\ 1

temperature.c ( ) 5 float fahr, celsius; 6 int lower, upper, step; 8 lower = 0; 9 upper = 300; 10 step = 0; 11 1 fahr = lower; 13 while (fahr <= upper) { 14 celsius = (5.0 /9.0) * (fahr-3) ; 15 printf("%3.0f %6.1f\n", fahr, celsius); 16 fahr = fahr + step; 1 } 18 } [printf ] (K&R p.308, B-) %d 10 %6d 10 6 %f %6f 6 %.f %6.f %o 8 %x 16 %c %s %p %% % 6 [ ] fahr += step; (K&R.10, p.61) sizes.c () main() 3 { 4 printf("char %d\n", sizeof(char)); 5 printf("int %d\n", sizeof(int)); 6 printf("float %d\n", sizeof(float)); printf("double %d\n", sizeof(double)); 8 printf("long double %d\n\n", sizeof(long double)); 9 10 printf("short %d\n", sizeof(short)); 11 printf("long %d\n", sizeof(long)); 1 printf("long long int %d\n", sizeof(long long int)); 13 } [ ] char int short long float double

(K&R., p.44) char signed char unsigned char short unsigned short int unsigned int long unsigned long float double long double signed char unsigned char signed short, short int, signed short int unsigned short int signed signed int unsigned signed long, long int, signed long int unsigned long int 1.3 For for.c (For ) 5 int fahr; 6 for (fahr = 0; fahr <= 300; fahr = fahr + 0) printf("%3d %6.1f\n", fahr, (5.0 /9.0) * (fahr-3) ); 8 } do { } while( ); 1.4 symbolic constant.c ( ) 3 #define LOWER 0 4 #define UPPER 300 5 #define STEP 0 6 main() 8 { 9 int fahr; 10 for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) 11 printf("%3d %6.1f\n", fahr, (5.0 /9.0) * (fahr-3) ); 1 } 1.5 getchar(), putchar() 3

1.5.1 copyfile1.c ( 1 ) 5 int c; 6 c = getchar(); 8 while (c!= EOF){ 9 putchar(c); 10 c = getchar(); 11 } 1 } copyfile.c ( ) c char int EOF : End Of File 5 int c; 6 while ((c = getchar())!= EOF) 8 putchar(c); 9 } 1.5. countletters1.c ( 1 ) 5 long nc; 6 nc = 0; 8 while (getchar()!= EOF) 9 ++nc; 10 printf("%ld\n", nc); 11 } countletters.c ( ) 5 double nc; 6 nc = 0; 8 for (nc = 0; getchar()!= EOF; ++nc) 9 ; 10 printf("%.0f\n", nc); 11 } 4

1.5.3 countlines.c ( ) 5 int c, nl; 6 nl = 0; 8 while ((c = getchar())!= EOF) 9 if (c == \n ) 10 ++nl; 11 printf("%d\n", nl); 1 } 1.5.4 wc.c () 3 #define IN 1 4 #define OUT 0 5 6 main() { 8 int c, nl, nw, nc, state; 9 10 state = OUT; 11 nl = nw = nc = 0; 1 while((c = getchar())!= EOF) { 13 ++nc; 14 if (c == \n ) 15 ++nl; 16 if (c == c == \n c== \t ) 1 state = OUT; 18 else if (state == OUT) { 19 state = IN; 0 ++nw; 1 } } 3 printf("%d %d %d\n", nl, nw, nc); 4 } OUT 空白文字 空白以外の文字 IN 空白文字空白以外の文字 isspace() (K&R B, p.31) 5

1.6 countdigits.c () 5 int c, i, nwhite, nother; 6 int ndigit[10]; 8 nwhite = nother = 0; 9 for (i = 0 ; i < 10; ++i) 10 ndigit[i] = 0; 11 1 while((c = getchar())!= EOF) 13 if (c >= 0 && c <= 9 ) 14 ++ndigit[c- 0 ]; 15 else if (c == c == \n c == \t ) 16 ++nwhite; 1 else 18 ++nother; 19 0 printf("digits ="); 1 for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); 3 printf(", white space = %d, other = %d\n", 4 nwhite, nother); 5 } int ndigit[10] ndigit[0] ndigit[9] c- 0 : c 0 0 c 1 1 1. bool.c main() 3 { 4 int result; 5 6 printf("%d\n", 3 <= 5); printf("%d\n",!(3 <= 5)); 8 9 result = 5 == 5; 10 printf("%d\n", result); 11 } 1 0 1 K&R p.5, p.54 K&R p.54, if, while, for 6

sum.c main() 3 { 4 int d, sum = 0; 5 6 for(;;) { if (scanf("%d", &d)!= 1) 8 break; 9 printf("%d\n", d); 10 sum += d; 11 } 1 printf("sum %d\n", sum); 13 } scanf() ( ) continue (for while ) switch 1. cc -Wall. 3. 4. (++ --) 5. 6.. ++ --.1 cc -Wall (Warning ) warning1.c 1 main() { 3 int i; 4 printf("end\n"); 5 } warning.c 3 int main() 5 printf("end\n"); 6 return 0; } % cc -Wall warning1.c warning1.c:: warning: return type defaults to int warning1.c: In function main : warning1.c:4: warning: implicit declaration of function printf warning1.c:3: warning: unused variable i warning1.c:5: warning: control reaches end of non-void function % cc -Wall warning.c %./a.out end %

. (K&R 1., 1.10) function1.c (K&R 1.) int power(int m, int n); 3 4 int main() 5 { 6 int i; for (i = 0; i < 10; ++i) 8 printf("%d %d %d\n", i, power(,i), power(-3,i)); 9 return 0; 10 } 11 int power(int base, int n) 1 { 13 int p; 14 for (p = 1; n > 0; --n) 15 p = p * base; 16 return p; 1 } 0 1 1 1-3 4 9 3 8-4 16 81 5 3-43 6 64 9 18-18 8 56 6561 9 51-19683 function.c void power(int m, int n); 3 int result; 4 int main() 5 { 6 int i; extern int result; 8 for (i = 0; i < 10; ++i) 9 { 10 power(, i); 11 printf("%d %d", i, result); 1 power(-3, i); 13 printf(" %d\n", result); 14 } 15 return 0; 16 } 1 void power(int base, int n) 18 { 19 int p; 0 for (p = 1; n > 0; --n) 1 p = p * base; result = p; 3 return ; 4 } int power(int, int); main 0 () exit() void extern 8

.3 (K&R 1.9) string.c 3 int main() 5 char word[]="hello"; 6 int length, i; 8 /* char word[] = { H, e, l, l, o, \0 }; 9 char word[6] = { H, e, l, l, o, \0 }; 10 char word[6] = "Hello"; */ 11 1 length = sizeof("hello"); 13 printf("sizeof(\"hello\") = %d\n",length); 14 15 for (i = 0; i < length; i++) 16 printf("word[%d] = %c (%d)\n", i, word[i], word[i]); 1 18 printf("%s\n", word); 19 return 0; 0 } sizeof("hello") = 6 word[0] = H () word[1] = e (101) word[] = l (108) word[3] = l (108) word[4] = o (111) word[5] = ^@ (0) Hello char word[] = Hello ; word H e l l o 0 \0.4 (K&R.8) increment.c 3 int main() 5 int i; 6 i = 5; while (--i) 8 printf("%d ", i); 9 printf("\n"); 10 11 i = 5; 1 while (i--) 13 printf("%d ", i); 14 printf("\n"); 15 return 0; 16 } 4 3 1 4 3 1 0 9

strcat.c (K&R.8, p.59) #define MAXLINE 56 3 4 void mystrcat(char s[], char t[]); 5 6 int main() { 8 char former[maxline] = "Hello"; 9 char latter[] = " World!"; 10 11 printf("%s + %s\n", former, latter); 1 mystrcat(former, latter); 13 printf("%s\n", former); 14 return 0; 15 } 16 1 void mystrcat(char s[], char t[]) 18 { 19 int i, j; 0 1 i = j = 0; while (s[i]!= \0 ) 3 i++; 4 while ((s[i++] = t[j++])!= \0 ) 5 ; 6 } Hello + World! Hello World! 10

.5 (K&R 5) pointer1.c (K&R 5.1, p.114) 3 int main() 5 int x =1, y=, z[3] = {, 1, 0}; 6 int *ip; 8 ip = &x; 9 y = *ip; 10 *ip = 0; 11 ip = & z[0]; 1 13 printf(" x = %d, y = %d, z = {%d,%d,%d}\n", 14 x, y, z[0], z[1], z[]); 15 printf(" &x = %p\n", &x); 16 printf(" &y = %p\n", &y); 1 printf(" z = %p\n", z); 18 printf(" &z[0] = %p\n", &z[0]); 19 printf(" &ip = %p\n", &ip); 0 printf(" z[0] = %d\n", z[0]); 1 printf(" ip = %p \n", ip); return 0; 3 } x = 0, y = 1, z = {,1,0} &x = 0xbffffbbc &y = 0xbffffbb8 z = 0xbffffba0 &z[0] = 0xbffffba0 &ip = 0xbffffb9c z[0] = ip = 0xbffffba0 bffffb90 bffffba0 94 a4 98 ip=&z[0]; a8 ac z[0] z[1] z[] z bffffbb0 b4 b8 y 9c ip ip=&x; bc x pointer.c 3 int main() 5 int z[3] = {, 1, 0}; 6 int i, *ip; 8 ip = z; 9 for (i = 0; i < 3; i++) 10 printf("z[%d] = %d, *(z+%d) = %d, %d\n", 11 i, z[i], i, *(z + i), *ip++); 1 return 0; 13 } z[0] =, *(z+0) =, z[1] = 1, *(z+1) = 1, 1 z[] = 0, *(z+) = 0, 0 z[] = {, 1, 0}; z[i] *(z+i) (K&R p.10) *ip++ (*ip)++ *ip++ *(ip++) 11

pointer3.c 3 int main() 5 char word[] = "Hello"; 6 char *letter; 8 for (letter = word; *letter; letter++) 9 putchar(*letter); 10 putchar( \n ); 11 return 0; 1 } Hello ( \0 ) 0.6 swap.c (K&R 5.) 3 void swap(int *m, int *n); 4 5 int main() 6 { int a, b; 8 9 a = 10; b = 5; 10 printf("a = %d, b = %d\n", a, b); 11 swap(&a, &b); 1 printf("a = %d, b = %d\n", a, b); 13 return 0; 14 } 15 16 void swap(int *px, int *py) 1 { 18 int temp; 19 0 temp = *px; 1 *px = *py; *py = temp; 3 } a = 10, b = 5 a = 5, b = 10 a: b: px: py: 1

. ++ -- pointer4.c 3 int main(){ 4 char str[] = "test"; 5 int seq[] = {4, 6, 4, 9}; 6 char *c; int *s; 8 c = str; s = seq; 9 10 printf("%c at %p\n", *c, c); c++; 11 printf("%c at %p\n", *c, c); c++; 1 printf("%c at %p\n", *c, c); c++; 13 printf("%c at %p\n\n", *c, c); 14 15 printf("%1d at %p\n", *s, s); s++; 16 printf("%1d at %p\n", *s, s); s++; 1 printf("%1d at %p\n", *s, s); s++; 18 printf("%1d at %p\n\n", *s, s); 19 0 c = str; printf("%c at %p\n", *c, c); 1 c = str + 1; printf("%c at %p\n", *c, c); c = str + ; printf("%c at %p\n", *c, c); 3 c = str + 3; printf("%c at %p\n\n", *c, c); 4 5 s = seq; printf("%1d at %p\n", *s, s); 6 s = seq + 1; printf("%1d at %p\n", *s, s); s = seq + ; printf("%1d at %p\n", *s, s); 8 s = seq + 3; printf("%1d at %p\n", *s, s); 9 30 return 0; 31 } t at 0xbffffbb0 e at 0xbffffbb1 s at 0xbffffbb t at 0xbffffbb3 ( ) str++ 4 at 0xbffffba0 6 at 0xbffffba4 4 at 0xbffffba8 9 at 0xbffffbac t at 0xbffffbb0 e at 0xbffffbb1 s at 0xbffffbb t at 0xbffffbb3 4 at 0xbffffba0 6 at 0xbffffba4 4 at 0xbffffba8 9 at 0xbffffbac 13

pointer5.c 3 int main(){ 4 int seq[], *ip, tmp; 5 6 seq[0] = 5; seq[1] = 10; ip = seq; tmp = 0; printf("ip:%p, tmp:%d, seq[0]:%d, seq[1]:%d\n", 8 ip, tmp, seq[0], seq[1]); 9 10 seq[0] = 5; seq[1] = 10; ip = seq; tmp = 0; 11 tmp = ++*ip; 1 printf("ip:%p, tmp:%d, seq[0]:%d, seq[1]:%d\n", 13 ip, tmp, seq[0], seq[1]); 14 15 seq[0] = 5; seq[1] = 10; ip = seq; tmp = 0; 16 tmp = *++ip; 1 printf("ip:%p, tmp:%d, seq[0]:%d, seq[1]:%d\n", 18 ip, tmp, seq[0], seq[1]); 19 0 seq[0] = 5; seq[1] = 10; ip = seq; tmp = 0; 1 tmp = *ip++; printf("ip:%p, tmp:%d, seq[0]:%d, seq[1]:%d\n", 3 ip, tmp, seq[0], seq[1]); 4 5 seq[0] = 5; seq[1] = 10; ip = seq; tmp = 0; 6 tmp = *(ip++); printf("ip:%p, tmp:%d, seq[0]:%d, seq[1]:%d\n", 8 ip, tmp, seq[0], seq[1]); 9 return 0; 30 } tmp = (*ip)++; ip:0xbffffbc0, tmp:0, seq[0]:5, seq[1]:10 ip:0xbffffbc0, tmp:6, seq[0]:6, seq[1]:10 ip:0xbffffbc4, tmp:10, seq[0]:5, seq[1]:10 ip:0xbffffbc4, tmp:5, seq[0]:5, seq[1]:10 ip:0xbffffbc4, tmp:5, seq[0]:5, seq[1]:10 tmp = ++*ip tmp = *++ip tmp = *ip++ tmp = *(ip++) 14