ネットワークプログラミング第2回 「 c...

37
ネネネネネネネネネネネネネネ C ネネネネネネネネ ネネネ ネネネネネ ・・」 Variables, Functions, Data Types, Pointers 20 10 ネ Fall Semester / ネネネ Rodney Van Meter

Upload: thais

Post on 19-Mar-2016

41 views

Category:

Documents


1 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

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

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

20 10 年 Fall Semester / 秋学期Rodney Van Meter

Page 2: ネットワークプログラミング第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.

課題・授業資料などの情報を掲示します 課題は毎回こちらに提出してください!! 今日の課題締め切り

10/12( 火 )10:59 分まで! 遅れて提出する方は要連絡

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

今期の授業スケジュール(予定) 第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 第1 2 回 12/21 : Work on Projects 第13回 1/19 : Final Presentations!!!

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

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

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

変数と型

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

変数 :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 {})

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

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;

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

データ型 :Data Types

次のページに詳細あり

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

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));}

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

型の大きさ : WARNING!Actual definition:size of char <= size of short <= size of int <= size of long <= size of long longIn some compilers, ints are 16 bits (2bytes),

and in some, long long doesn’t exist

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

Integerssize of int: 4 bytessize of long: 8 bytes

s03031

-2147483648 ~ +2147483647

s

0

6263

-1.7E-308 ~ 1.7E+308

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

浮動小数点 (Floating Point)size of float: 4 bytessize of double: 8 bytesStandardized by IEEE 754, most common

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

条件文 :Statements, Conditions, Flow Control

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

if Statements ~もし・・・ならif(expr){

statement ;…

}else {

statement ;…

}ex :if( x == 1){

y = 3;z = 2;

}else{

y = 5;z = 4;

}

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

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 より小さいか等しい )

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

while: Repeat as long as true~条件を満たす間実行while(expr){

stmt ;…

}

例:x = 0;while( x < 10 ){

printf(“%d\n”,x);x = x + 1;

}

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

for を使った条件文~条件を満たす間実行

for( expr1 ; expr2 ; expr3 ){stmt ;…

}

例:for( x = 0; x < 10; x = x +1 ){

printf(“%d\n”, x);}

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

switch 文 : multi-case if stmtswitch (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 文を簡素化する際に使用

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

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 ;

}

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

関数 :Functions

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

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”

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

戻り値 :Return Values 関数から返す値

記述法 return 式;

ただし関数のデータ型と戻り値のデータ型は一致しなければならない

#include <stdio.h> int squaresub(int a) {

return a*a; }

int main() {

int b = 10; printf("%d\n", squaresub(5));return 0;

}

int 型

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

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)

Kernel

std. I/OLibrary

App#1

App #2

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

I/O: stdio.h Functions:

printf() sends to stdout

変数等を整形して出力 fprintf()

can define which output file to printf

gets() reads one line from stdinNEVER 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

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

Unix I/O System Calls(unistd.h) 関数群

read() Read # of bytes from file 入力先とバイト数を指定して読み込み

write() Write # of bytes to file 出力先とバイト数を指定して書き込み

open() close() stat()

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

コンパイル :Compilation

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

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

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

コンパイルのおさらい One file ( 単一ファイルの場合 )

%gcc -o program source1.c

More than one file ( 複数ファイルの場合 )%gcc -o program source1.c source2.cOR%gcc -c source1.c%gcc -c source2.c%gcc -o program source1.o source2.o

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

Using Libraries ライブラリ(関数群) プログラムの先頭で、

#include <stdio.h>のように使用 関数に必要なライブラリは man を参照

特殊なライブラリを使う場合はコンパイルオプション( -l ライブラリ名)で指定 例) %gcc program.c –lpcap ccz0x でコードを書く人は要注意

SunOS では -lsocket -lnsl が重要!

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

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

ように宣言

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

Makefile Rules for recompiling program 再コンパイルの煩雑さを解消

コンパイル時は make と打つだけ

Basic idea ( 基本文法 ):target: dependency<tab>command

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

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

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

Makefile の簡素化 .c から .o ファイルの生成ルールall : source1.o source2.o

gcc -o program source1.o source2.o

コンパイルオプションの変数化<command>%gcc -Wall -g -c source1.c<Makefile>CFLAGS = -Wallgcc ${CFLAGS} -c source1.c

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

hello.c への適用例

all: hello

hello: hello.ogcc -o hello hello.o

hello.o: hello.cgcc -c hello.c

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

Homework

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

課題 1:Homework Prob. 1 浮動小数点のレーンジ(fprange.c)1. 最小: Write a program that starts with

x = 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”.

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

課題 2 Makefile を作ってください 一行目は:

all: hello typesizes fortest cline numbers