1 / 37

ネットワークプログラミング第2回 「 C 言語の基礎~変数・関数・ポインタ」 Variables, Functions, Data Types, Pointers

20 10 年 Fall Semester / 秋学期 Rodney Van Meter. ネットワークプログラミング第2回 「 C 言語の基礎~変数・関数・ポインタ」 Variables, Functions, Data Types, Pointers. Class Web PAGE. SFC-SFS https://vu.sfc.keio.ac.jp/sfc-sfs/ 本講義を「 MY 時間割(仮)」へ登録してください Submit homework every week Today’s HW due: Tues 10/12, 10:59 a.m.

thais
Download Presentation

ネットワークプログラミング第2回 「 C 言語の基礎~変数・関数・ポインタ」 Variables, Functions, Data Types, Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2010年 Fall Semester / 秋学期 Rodney Van Meter ネットワークプログラミング第2回「C言語の基礎~変数・関数・ポインタ」Variables, Functions, Data Types, Pointers

  2. Class Web PAGE SFC-SFShttps://vu.sfc.keio.ac.jp/sfc-sfs/ 本講義を「MY時間割(仮)」へ登録してください Submit homework every week Today’s HW due: Tues 10/12, 10:59 a.m. 課題・授業資料などの情報を掲示します 課題は毎回こちらに提出してください!! 今日の課題締め切り 10/12(火)10:59分まで! 遅れて提出する方は要連絡

  3. 今期の授業スケジュール(予定) 第1回 9/28: Introduction / イントロダクション 第2回 10/5:C Basics~Functions, Variables, Data Types・Makefiles 第3回 10/12:C Basics~Command Line Arguments・ Structures ・ Pointers 第4回 10/19:C Basics~Pointers & Arrays ・ Lists 第5回 10/26: file I/O・ Network Programming 第6回 11/2: Network Programming (1)‏ 第7回 11/9: Network Programming (2)‏ 第8回 11/16: Network Programming (3) 第9回 11/30: Applied Network Programming (1) 第10回 12/7: Applied Network Programming (2) 第11回 12/14: Work on Projects 第12回 12/21: Work on Projects 第13回 1/19: Final Presentations!!!

  4. Today Variables and Basic Data Types Statements Functions Practice Problems Compiling Makefiles Practice

  5. 変数と型

  6. 変数:Variables A variable is a piece of data, stored in memory (or register) Has a name (or identifier) e.g.: int hoge; Global variables Scope is global (whole program): can be used by name anywhere Local variables Scope is inside one block (defined by {})

  7. Working With Variables Declaration Defines legal names Defines data type e.g.: int counter; Must be initialized before use Done at declare (= compile) time: int counter = 0; Run time, before first use: int counter; {...} counter = 0; Assignment variable = expression x = y+z-3*a;

  8. データ型:Data Types Integers (整数) int, char, short, long Floating Point (浮動小数点) double, float Arrays (配列) Group of same type int a[10]; a[0] = 1; a[9] = 10; a[10]=11; 次のページに詳細あり

  9. Practice: typesizes.c #include <stdio.h> int main()‏ { printf("size of char:\t%d bytes\n", sizeof(char)); printf("size of short:\t%d bytes\n", sizeof(short)); printf("size of int:\t%d bytes\n", sizeof(int)); printf("size of long:\t%d bytes\n", sizeof(long)); printf("size of long long:\t%d bytes\n", sizeof(long long)); printf("size of float:\t%d bytes\n", sizeof(float)); printf("size of double:\t%d bytes\n", sizeof(double)); }

  10. 型の大きさ: WARNING! Actual definition: size of char <= size of short <= size of int <= size of long <= size of long long In some compilers, ints are 16 bits (2bytes), and in some, long long doesn’t exist

  11. Integers size of int: 4 bytes size of long: 8 bytes s 0 30 31 -2147483648~+2147483647 62 63 s -1.7E-308 ~1.7E+308 0

  12. 浮動小数点 (Floating Point) size of float: 4 bytes size of double: 8 bytes Standardized by IEEE 754, most common

  13. 条件文:Statements, Conditions, Flow Control

  14. if Statements~もし・・・なら if(expr){ statement; … } else { statement; … } ex: if( x == 1){ y = 3; z = 2; } else{ y = 5; z = 4; }

  15. Boolean (conditional) exprs a == b TRUE if a and b are equal (aとbは等しい) a != b TRUE if a and b are NOT equal (等しくない) a > b TRUE is a is bigger than b (aはbより大きい) a >= b TRUE if greater than or equal (bより大きいか等しい) a < b TRUE if a less than b (bより小さい) a <= b TRUE if b is less than or equal to b (bより小さいか等しい)

  16. while: Repeat as long as true~条件を満たす間実行 while(expr){ stmt; … } 例: x = 0; while( x < 10 ){ printf(“%d\n”,x); x = x + 1; }

  17. forを使った条件文~条件を満たす間実行 for( expr1; expr2; expr3 ){ stmt; … } 例: for( x = 0; x < 10; x = x +1 ){ printf(“%d\n”, x); }

  18. switch文: multi-case if stmt switch (expr){ case const1: stmt;…; break; case const2: stmt;…; break; … default: stmt;…; break; } int monthday( int month ){ switch(month){ case 1: return 31; case 2: return 28; … case 12: return 31; } } 複雑な if 文を簡素化する際に使用

  19. breakとcontinue break get out of loop (ループを抜ける) continue skip the rest of the loop this time, but repeat loop 残りのループをスキップして、次のループを開始 for( i=0; i<100; i++ ){ 処理1; if ( i>=90 ) continue; 処理2; } for( i=0; i<100; i++ ){ 処理1; if ( i==90 ) break; 処理2; }

  20. 関数:Functions

  21. What’s a Function? Subroutine, block of statements with a name May take arguments, return a value, have side effects Defines scope for local variables All functions must have declared return type If it returns nothing, declare “void”

  22. 戻り値:Return Values 関数から返す値 記述法 return 式; ただし関数のデータ型と戻り値のデータ型は一致しなければならない #include <stdio.h> int squaresub(int a) { return a*a; } int main() { intb = 10; printf("%d\n", squaresub(5)); return 0; } int型

  23. I/O: Systems Calls v. Library Functions All I/O calls ultimately go to the kernel I/O library helpswith buffering, formatting, interpreting (esp. text strings & conversions)‏ App #2 App #1 std. I/O Library Kernel

  24. I/O: stdio.h Functions: printf()‏ sends to stdout変数等を整形して出力 fprintf()‏ can define which output file to printf gets() reads one line from stdin NEVER EVER USE THIS! 使うと不合格! fgets()‏ get one line from defined file USE THIS INSTEAD! getc get one char 標準入力から1文字読み込み putc put one char 標準出力に1文字書き出し not recommended:推奨されない関数 scanf 標準入力から書式に従い読み込み fscanf 入力先を指定できるscanf

  25. Unix I/O System Calls(unistd.h)‏ 関数群 read()‏ Read # of bytes from file 入力先とバイト数を指定して読み込み write()‏ Write # of bytes to file 出力先とバイト数を指定して書き込み open()‏ close()‏ stat()‏

  26. コンパイル:Compilation

  27. Multiple Source Code Files コンパイル時間がかかる 複数人での開発が困難 ⇒ファイルの分割と分割コンパイルが必要(extern宣言が必要)   変数のスコープに注意 <ファイル1> /* source1.c */ extern b; int sub(int a) { return a*b; } <ファイル2> /* source2.c */ #include <stdio.h> int b; int main() { b = 10; printf("%d\n", sub(5)); return 0; }

  28. コンパイルのおさらい One file (単一ファイルの場合) %gcc -o program source1.c More than one file (複数ファイルの場合) %gcc -o program source1.c source2.c OR %gcc -c source1.c %gcc -c source2.c %gcc -o program source1.o source2.o

  29. Using Libraries ライブラリ(関数群) プログラムの先頭で、#include <stdio.h>のように使用 関数に必要なライブラリはmanを参照 特殊なライブラリを使う場合はコンパイルオプション(-lライブラリ名)で指定 例)%gcc program.c –lpcap ccz0xでコードを書く人は要注意 SunOSでは -lsocket -lnsl が重要!

  30. Header files ヘッダファイルの活用 ファイル分割時に共通部分のくくりだし .hファイルとして作成 各ファイルで、#include “header.h”のように宣言

  31. Makefile Rules for recompiling program 再コンパイルの煩雑さを解消 コンパイル時は make と打つだけ Basic idea (基本文法): target: dependency <tab>command

  32. Makefileの例 main : source1.o source2.ogcc -o program source1.o source2.o source1.o : source1.cgcc -c source1.c source2.o : source2.cgcc -c source2.c

  33. Makefileの簡素化 .cから.oファイルの生成ルール all : source1.o source2.o gcc -o program source1.o source2.o コンパイルオプションの変数化 <command> %gcc -Wall -g -c source1.c <Makefile> CFLAGS = -Wall gcc ${CFLAGS} -c source1.c

  34. hello.cへの適用例 all: hello hello: hello.o gcc -o hello hello.o hello.o: hello.c gcc -c hello.c

  35. Homework

  36. 課題1:Homework Prob. 1浮動小数点のレーンジ(fprange.c)‏ 1. 最小:Write a program that starts withx = 1.0, and divides it by two repeatedly until it becomes zero 2. 最大: Do the same thing getting larger until something happens (what happens?) 3. Do for both “float” and “double”.

  37. 課題2 Makefileを作ってください 一行目は: all: hello typesizes fortest cline numbers

More Related