ここでは,c言語における文字列の扱い方を学ぶ.c言語における...
TRANSCRIPT
ここでは,C言語における文字列の扱い方を学ぶ.C言語における文字列の表現方法は特有で,ローレベルな実現方法を採用しているため文字列処理をするには,様々な点に気を配らなくてはならない.Java言語やほかのスクリプト言語では,もう少しスマートな方法を提供している.本講義では,C言語における文字列の扱いは紹介程度にとどめることとし,文字列処理が必要な時は他の言語を選択することを薦めることとする.しかし,C言語はローレベルな扱いができるため,高速化や省メモリ化なども可能となり,場合によってはC言語での処理が必要かもしれない.C言語における文字列の詳細は,筧本8章および13.3節を参照のこと.
文字列には「char*」という型を使う.
chap08_010.c#include <stdio.h>int main(){ char* str; // 変数宣言. str = "mikan"; // 値の代入. printf("str = %s\n",str); // 値の表示. return 0;}
このプログラムでは str という変数で文字列を表している.文字列はダブルコーテーションで囲む.printf の書式指定子は,%s.
ポインタを理解した人への説明.
C言語では,文字列は文字配列を使って表している.上記のコードでは,"mikan"というデータは次のように配列形式でメモリに格納されている.
str はポインタである.「str = "mikan"; 」によって,上記の配列の最初の要素のアドレスが strに代入される.
文字列の表現方法は,char* だけではない.chap08_010.c のように,char* で実装した変数の内容は,基本的には書きかえることができない(例外はある.).char 型配列を使えば,書き換え可能である.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:27
文字列をコピーするには strncpy という関数を用いる.
以上,C言語で文字列の扱い方を簡単に紹介した.誤った使い方をすると,予期せぬ動作をすることがあり,扱いには注意が必要である.実際にC言語で文字列を扱うときは,筧本8章および13.3節をよく読んで十分理解してからにしたほうがよいだろう.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:27
筧本で扱っていた配列は添え字の個数が1個であった.ここでは,添え字が2戸以上になるような配列を解説する.たとえば,配列を次のように宣言したとする:
double X[2][2];
これは2×2の行列をつくったことに相当する.このように添え字が2個以上ある配列のことを多次元配列という.
ここで1次元配列と2次元配列の変数宣言の例を比べておく.
int a[3];int b[2][3];
このように変数を宣言した場合,変数 a は整数を要素とする要素数 3 の1次元配列である.一方で,変数 b は2次元配列であり,要素数は,全体で 2 × 3 = 6 個になる:
a[0]
a[1]
a[2]
b[0][0] b[0][1] b[0][2]
b[1][0] b[1][1] b[1][2]
初期化
1次元配列の場合,初期化するには次のように行っていた(筧本,p.103).
double a[3] = { 0.1, 0.2, 0.3 };
多次元配列の場合,次のように行う:
double b[2][3] = { { 10.1, 10.2, 10.3 }, {11.1, 11.2,11.3} };
多次元配列と多重ループ
多次元配列の各要素にアクセスするには多重ループが便利である.次のプログラムは2次元配列の中身を画面に表示するものである.
#include <stdio.h>int main(){ int i, j; double A[2][3] = { { 10.1, 10.2, 10.3 },/* 初期化 */ { 11.1, 11.2, 11.3 } }; for ( i = 0; i < 2; i++ ){ /* 表示 */ for ( j = 0; j < 3; j++ ){ printf(" %5.2f", A[i][j] ); } printf("\n"); } return 0;}
実行結果
10.10 10.20 10.30
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:28
11.10 11.20 11.30
多次元配列を関数に渡す方法.
To be written.
筧本では多次元配列は省略されていたものの,多次元配列はほかの多くの言語でも備えており,しばしば必要になるので押えておいたほうがいい.しかしながら,C言語における固定長の多次元配列の言語仕様はなんだか妙である.加藤の場合,malloc や free という関数を覚えて可変長の配列を扱えるようになって以来,固定長の多次元配列は使わないようにした.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:28
ここでは,関数による参照渡しの方法,および配列データの渡し方を学ぶ.筧本では,9章でまずポインタを説明し,これに基づいて,関数への参照渡しと配列データの渡し方を説明している.しかし,ポインタは難解なので,ポインタへの理解が不十分なために,14章の説明が十分に理解できない学生もいるだろう.そこで,原理を理解するのは後回しにして,まずやり方の理解だけを試みることとする.関数への参照渡しと配列データの渡し方ができるようになってから,余裕があれば筧本9章のポインタの説明を読んで,そのうえで筧本14章の説明を読み返すのがよいだろう.
14.1 参照渡し
10章では,関数に変数の値を渡すとき,次のように行った.
コード
chap14_010.c#include <stdio.h>void func1( int suti );int main(){ int suti; suti = 10; func1(suti); printf("in main, suti=%d\n",suti); return 0;}void func1( int suti ){ printf("in func1, suti=%d\n",suti);}
標準出力
in func1, suti=10in main, suti=10
では,関数の中で呼び出し元の変数の値を変更することを考える.次のようにしても変更することはできない.
コード
chap14_020.c#include <stdio.h>void func1( int sutiP );int main(){ int suti; suti = 10; func1(suti); printf("in main, suti=%d\n",suti); return 0;}void func1( int suti ){ suti = 30; printf("in func1, suti=%d\n",suti);}
標準出力
in func1, suti=30in main, suti=10
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 4 2016/06/09 11:28
関数に呼び出し元の変数の値を変更してもらうには,「参照渡し」と呼ばれる方法を用いる.これに対して,chap14_010.c や chap14_020.c のような値の渡し方は「値渡し」と呼ばれる.
コード
chap14_030.c#include <stdio.h>void func1( int* sutiP ); // 型名の後に*をつける.int main(){ int suti; suti = 10; func1(&suti); // 変数の前に&をつける. printf("in main, suti=%d\n",suti); return 0;}void func1( int* sutiP ) // 型名の後に*をつける.{ *sutiP = 30; // 変数の前に*をつける. printf("in func1, (*sutiP)=%d\n",*sutiP);}
標準出力
in func1, (*sutiP)=30in main, suti=30
実際に main 関数の中にある変数 suti の値が func1 を呼び出すことによって,値が 30 に変更されていることが確認できる.このようにして参照渡しを行う.
参照渡しと値渡しを混ぜて使うこともでき,さらに return によって値を戻すこともできる.
コード
chap14_040.c#include <stdio.h>double func1( double* pwa, double* psa, double a, double b);
// 値を変更したい変数だけに * をつける.
int main(){ double a, b, wa, sa, seki; a = 8.0; b = 3.0; seki = func1( &wa, &sa, a, b ); // 値を変更したい変数だけに & をつける. printf("a=%5.3f, b=%5.3f\n",a,b); printf("wa=%5.3f, sa=%5.3f, seki=%5.3f\n",wa,sa,seki); return 0;}double func1( double* pwa, double* psa, double a, double b )// 値を変更したい変数だけに * をつける.{ *pwa = a + b; *psa = a - b; return a * b;}
標準出力
a=8.000, b=3.000wa=11.000, sa=5.000, seki=24.000
14.2 関数への配列データの渡し方
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 4 2016/06/09 11:28
筧本9章では配列ではない変数の値渡しする場合しか紹介されていなかった.配列データは次のように関数に渡す.
コード
chap14_210.c#include <stdio.h>void func1( double ary[], int len ); // 配列変数の名前の後に [] をつける.int main(){ double ary[] = { 10.1, 10.2, 10.3 }; func1( ary, 3 ); // 配列変数の場合 & をつけない. return 0;}void func1( double ary[], int len ) // 配列変数の名前の後に [] をつける.{ int i; printf("in func1,"); for ( i = 0; i < len; i++ ){ printf(" [%2d]=%5.3f,", i, ary[i] ); } printf("\n");}
標準出力
in func1, [ 0]=10.10, [ 1]=10.20, [ 2]=10.30,
この例では,配列の値を画面に表示するだけである.chap14_210.c では引数リストには double ary[] のように書いてあるが,double* ary と書いてもよいことになっている.
配列ではない変数の場合,値渡しと参照渡しとの2種類があったが,配列変数の場合は(基本的には)この一通りしかない.次のコードは,配列内の数値に添え字番号分を加えるものである.
コード
chap14_220.c#include <stdio.h>void func1( double ary[], int len );int main(){ double ary[] = { 10.1, 10.2, 10.3 }; int i, len = 3; func1( ary, len ); for ( i = 0; i < len; i++ ){ printf(" [%2d]=%5.3f",i,ary[i]); } printf("\n"); return 0;}void func1( double ary[], int len ){ int i; for ( i = 0; i < len; i++ ){ ary[i] = ary[i] + i; // 配列内の数値に添え字番号分を加えて
いる. }}
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
3 / 4 2016/06/09 11:28
標準出力
[ 0]=10.10 [ 1]=11.20 [ 2]=12.30
発展的な内容.
配列の場合,関数に最初の要素のポインタを渡すことになるので,chap14_210.c や chap14_220.c のような関数では値の変更してほしくない場合でも値を変更されてしまうかもしれない.他人の作成した関数を呼び出すとき,場合によってはプロトタイプのみ確認でき,定義の中身を確認できない場合もある.しかし,「const 修飾子」を引数リスト中に付けた場合は,関数実行中に値が変更されないことが保証される.(正確には,「const 修飾子」をつけても内容を書きかえる抜け道はある.)詳しくは,筧本より詳しい書籍か,ANSI C の仕様書を見るとよいだろう.
配列ではない変数の参照渡しや return 文とも併用できる.次のコードは return 文で合計を返し,参照渡しで平均値の結果を得るものである.
コード
chap14_230.c#include <stdio.h>double func1( double* pave, double ary[], int len );int main(){ double ary[] = { 10.1, 10.5, 10.6 }; int len = 3; double sum, ave; sum = func1( &ave, ary, len ); printf("sum=%5.2f\n",sum); printf("ave=%5.2f\n",ave); return 0;}double func1( double* pave, double ary[], int len ){ int i; double sum; sum = 0.0; for ( i = 0; i < len; i++ ){ sum = sum + ary[i]; } *pave = sum / len; return sum;}
標準出力
sum=31.20ave=10.40
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
4 / 4 2016/06/09 11:28
解説:値渡しによる関数呼び出しの基本.
main フレーム内の変数 x に 5 を代入する.
標準出力
実引数 5 で func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 x に実引数 5 が代入された.次に,func1 フレーム内の x に 2 が代入される.
標準出力
func1 フレーム内の x の値が標準出力に出力される.
標準出力
func1 が終了したため,呼び出し元に制御が戻る.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:16
標準出力
x=2
func1 が終了したため,func1 フレームが破棄された.次に,main フレーム内の x の値が標準出力に出力される.
標準出力
x=2
main が終了したため,プログラムが終了する.
標準出力
x=2x=5
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:16
解説:呼び出す側の変数名と呼びだされる側の変数名が等しいとは限らない.
main フレーム内の変数 x に 5 を代入する.
標準出力
実引数 5で func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 y に実引数 5 が代入された.func1 フレーム内の y に 2 が代入される.
標準出力
func1 フレーム内の y の値が標準出力に出力される.
標準出力
func1 が終了したため,呼び出し元に制御が戻る.
標準出力
y=2
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:17
func1 が終了したため,func1 フレームが破棄された.main フレーム内の x の値が標準出力に出力される.
標準出力
y=2
main が終了したため,プログラムが終了する.
標準出力
y=2x=5
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:17
解説:呼び出す側の変数名と呼びだされる側の変数名が同じでも別物である.
main フレーム内の変数 x に 5 を代入する.
標準出力
実引数 9 (=x + 4) で func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 x に実引数 9 が代入された.次に func1 フレーム内の x に 2 が代入される.
標準出力
func1 フレーム内の x の値が標準出力に出力される.
標準出力
func1 が終了したため,呼び出し元に制御が戻る.
標準出力
x=2
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:18
func1 が終了したため,func1 フレームが破棄された.main フレーム内の x の値が標準出力に出力される.
標準出力
x=2
main が終了したため,プログラムが終了する.
標準出力
x=2x=5
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:18
解説:関数が戻り値を持つ場合,呼び出しから戻ってきた後さらに処理があることがある.この場合,戻ってきた後,代入の処理を行っている.
main フレーム内の変数 x に 5 を代入する.
標準出力
まず,「x = func1(x);」 の右辺の値を計算するため,実引数 5 で func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 x に実引数 5 が代入された.次に func1 フレーム内の x に 2 が代入される.
標準出力
func1 フレーム内の x の値が標準出力に出力される.
標準出力
func1 が終了したため,戻り値を 2 として,呼び出し元に制御が戻る.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:18
標準出力
x=2
func1 が終了したため,func1 フレームが破棄され,「x = func1(x);」の右辺の値は 2 と評価された.そして,「x = func1(x);」の右辺の値 2 が左辺の変数 x に代入された.次に,main フレーム内の x の値が標準出力に出力される.
標準出力
x=2
main が終了したため,プログラムが終了する.
標準出力
x=2x=2
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:18
解説:参照渡しによる関数呼び出しでは,呼び出し元の変数のアドレスが渡される.
main フレーム内の変数 x に 5 を代入する.
標準出力
実引数を変数 x のアドレスとして func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 px に,main フレームの変数 x のアドレスが代入された.よって,ポインタ px はmain フレームの変数 x を指すことになる.次に func1 フレーム内の px の指す変数の値が標準出力に出力される.
標準出力
func1 が終了したため,呼び出し元に制御が戻る.戻り値はない.
標準出力
*px=5
func1 が終了したため,func1 フレームが破棄された.次に,main フレーム内の x の値が標準出力に出力される.
標準出力
*px=5
main が終了したため,プログラムが終了する.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:19
標準出力
*px=5x=5
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:19
解説:参照渡しを使うと,呼び出し元の変数の値を呼び出し先で変更できる.
main フレーム内の変数 x に 5 を代入する.
標準出力
実引数を変数 x のアドレスとして func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 px に,main フレームの変数 x のアドレスが代入された.よって,ポインタ px はmain フレームの変数 x を指すことになる.次に func1 フレーム内の px の指す変数に 3 が代入される.
標準出力
func1 フレーム内の px の指す変数の値が標準出力に出力される.
標準出力
func1 が終了したため,呼び出し元に制御が戻る.戻り値はない.
標準出力
*px=3
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:20
func1 が終了したため,func1 フレームが破棄された.次に,main フレーム内の x の値が標準出力に出力される.
標準出力
*px=3
main が終了したため,プログラムが終了する.
標準出力
*px=3x=3
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:20
解説:値渡しと参照渡しは併用できる.
main フレーム内の変数 x に 5 を代入し,変数 y に 2 をする.
標準出力
第1実引数に変数 y のアドレスを与え,第2実引数を値 5 を与えて func1 を呼び出す.
標準出力
func1 フレームが作られ,第1仮引数 py に main フレームの変数 y のアドレスが代入され,第2仮引数 x に第2実引数 5 が代入された.よって,ポインタ py はmain フレームの変数 y を指すことになる.次に func1 フレーム内の変数 x に値 8 を代入する.
標準出力
func1 フレーム内の py の指す変数の値が標準出力に出力される.
標準出力
func1 フレーム内の変数 x の値が標準出力に出力される.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 3 2016/06/09 11:22
標準出力
*py=2
func1 が終了したため,呼び出し元に制御が戻る.戻り値はない.
標準出力
*py=2x=8
func1 が終了したため,func1 フレームが破棄された.次に,main フレーム内の x の値が標準出力に出力される.
標準出力
*py=2x=8
main フレーム内の y の値が標準出力に出力される.
標準出力
*py=2x=8x=5
main が終了したため,プログラムが終了する.
標準出力
*py=2x=8x=5y=2
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 3 2016/06/09 11:22
解説:配列変数は配列の実体の最初の要素へのポインタである.配列を関数に渡すときは,配列の実体の最初の要のアドレスを渡す.
main フレーム内の配列変数 xs は長さ 2 の配列[15,13]を指している.実引数を配列変数 xs の指すアドレスとして func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 xs に,main フレームの変数 xs が指していたアドレスが代入された.よって,func1 フレーム内の配列 xs は main フレームの配列[15,13]を指すことになる.次に,xs[0] および xs[1] の値が標準出力に出力される. ただし,ここでいう xs[0] および xs[1] とは,それぞれ,funcフレーム内の配列変数 xs が指す配列の第0要素と第1要素を指す.
標準出力
func1 が終了したため,呼び出し元に制御が戻る.戻り値はない.
標準出力
xs=[15 13]
func1 が終了したため,func1 フレームが破棄された.次に,xs[0] および xs[1] が標準出力に出力される. ただし,ここでいう xs[0] および xs[1] とは,それぞれ,main フレム内の配列変数 xs が指す配列の第0要素と第1要素を指す.
標準出力
xs=[15 13]
main が終了したため,プログラムが終了する.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:23
標準出力
xs=[15 13]xs=[15 13]
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:23
解説:関数内で配列の値を変更できる.
main フレーム内の配列変数 xs は長さ 2 の配列[15,13]を指している.実引数を配列変数 xs の指すアドレスとして func1 を呼び出す.
標準出力
func1 フレームが作られ,仮引数 xs に,main フレームの変数 xs が指していたアドレスが代入された.よって,func1 フレーム内の配列 xs は main フレームの配列[15,13]を指すことになる.次に,xs[1] に 3 を代入する.ただし,ここでいう xs[1]とは,func1 フレーム内の配列変数 xs が指す配列の第1要素を指す.
標準出力
xs[0] および xs[1] の値が標準出力に出力される. ただし,ここでいう xs[0] および xs[1] とは,それぞれ,func1 フレム内の配列変数 xs が指す配列の第0要素と第1要素を指す.
標準出力
func1 が終了したため,呼び出し元に制御が戻る.戻り値はない.
標準出力
xs=[15 3]
func1 が終了したため,func1 フレームが破棄された.次に,xs[0] および xs[1] が標準出力に出力される. ただし,ここでいう xs[0] および xs[1] とは,それぞれ,main フレム内の配列変数 xs が指す配列の第0要素と第1要素を指す.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:23
標準出力
xs=[15 3]
main が終了したため,プログラムが終了する.
標準出力
xs=[15 3]xs=[15 3]
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:23
解説:関数に2本の配列を渡す例.関数が引数を2つ持つ場合,第1実引数の値は第1仮引数に代入され,第2実引の値は第2仮引数に代入される.変数名に惑わされないように.
main フレーム内の配列変数 xs は長さ 2 の配列[15,13]を指している.main フレーム内の配列変数 ys は長さ 2 の配列[5,3]を指している.第1実引数を配列変数 ys の指すアドレスとして,第2実引数を配列変数 xs の指すアドレスとして,func1 を呼び出す.
標準出力
func1 フレームが作られ,第1仮引数 xs に,main フレームの変数 ys が指していたアドレスが代入され,第2仮引数 ys に,main フレームの変数 xs が指していたアドレスが代入された.よって,func1 フレーム内の配列 xs は main フレームの配列[5,3]を指し,func1 フレーム内の配列 ys は main フレームの配列[15,13]を指すことになる.次に,xs[0] および xs[1] の値が標準出力に出力される. ただし,ここでいう xs[0] および xs[1] とは,それぞれ,funcフレーム内の配列変数 xs が指す配列の第0要素と第1要素を指す.
標準出力
ys[0] および ys[1] の値が標準出力に出力される. ただし,ここでいう ys[0] および ys[1] とは,それぞれ,func1 フレム内の配列変数 ys が指す配列の第0要素と第1要素を指す.
標準出力
xs = [5 3]
func1 が終了したため,呼び出し元に制御が戻る.戻り値はない.
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
1 / 2 2016/06/09 11:24
標準出力
xs = [5 3]ys = [15 13]
func1 が終了したため,func1 フレームが破棄された.次に,xs[0] および xs[1] が標準出力に出力される. ただし,ここでいう xs[0] および xs[1] とは,それぞれ,main フレム内の配列変数 xs が指す配列の第0要素と第1要素を指す.
標準出力
xs = [5 3]ys = [15 13]
ys[0] および ys[1] が標準出力に出力される. ただし,ここでいう ys[0] および ys[1] とは,それぞれ,main フレーム内の配列変数 ys が指す配列の第0要素と第1要素を指す.
標準出力
xs = [5 3]ys = [15 13]xs = [15 13]
main が終了したため,プログラムが終了する.
標準出力
xs = [5 3]ys = [15 13]xs = [15 13]ys = [5 3]
プログラミング言語I file:///C:/cygwin64/home/my-documents/web-since-h2712/lecture...
2 / 2 2016/06/09 11:24
Programming Languages I, Chapter 10c
Tsuyoshi Kato
July 23, 2012
Exercise 1 (kadai-chap10c-01.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int x );int main(){int x;x = 5;func1( x );printf("x=%d\n",x);return 0;
}void func1( int x ){x = 2;printf("x=%d\n",x);
}
Answer:
x=2
x=5
Exercise 2 (kadai-chap10c-02.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int y );int main(){int x;x = 5;func1( x );printf("x=%d\n",x);return 0;
}void func1( int y ){y = 2;printf("y=%d\n",y);
}
Answer:
y=2
x=5
Exercise 3 (kadai-chap10c-03.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int x );int main(){
int x;x = 5;func1( x + 4 );printf("x=%d\n",x);return 0;
}void func1( int x ){
x = 2;printf("x=%d\n",x);
}
Answer:
x=2
x=5
Exercise 4 (kadai-chap10c-04.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int x );int main(){
int x;x = 5;x = func1( x );printf("x=%d\n",x);return 0;
}int func1( int x ){
x = 2;printf("x=%d\n",x);return x;
}
Answer:
x=2
x=2
Exercise 5 (kadai-chap10c-05.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
1
Chap 10c
#include <stdio.h>int func1( int y );int main(){int x;x = 5;x = func1( x );printf("x=%d\n",x);return 0;
}int func1( int y ){int x;x = 2;printf("x=%d\n",x);printf("y=%d\n",y);return x;
}
Answer:
x=2
y=5
x=2
Exercise 6 (kadai-chap10c-06.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int y );int main(){int x;x = 5;x = func1( x );printf("x=%d\n",x);return 0;
}int func1( int y ){int x;x = y + 2;printf("x=%d\n",x);printf("y=%d\n",y);return x;
}
Answer:
x=7
y=5
x=7
Exercise 7 (kadai-chap10c-07.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int y );int main(){int x;x = 5;
func1( x );printf("x=%d\n",x);return 0;
}void func1( int y ){
int x;x = y + 2;printf("x=%d\n",x);printf("y=%d\n",y);
}
Answer:
x=7
y=5
x=5
Exercise 8 (kadai-chap10c-41.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int x, int y );int main(){
int x, y;x = 5; y = 3;func1( x, y );printf("x=%d\n",x);printf("y=%d\n",y);return 0;
}void func1( int x, int y ){
printf("x=%d\n",x);printf("y=%d\n",y);
}
Answer:
x=5
y=3
x=5
y=3
Exercise 9 (kadai-chap10c-42.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int x, int y );int main(){
int x, y;x = 5; y = 3;func1( x, y+2 );printf("x=%d\n",x);printf("y=%d\n",y);return 0;
}void func1( int x, int y ){
printf("x=%d\n",x);printf("y=%d\n",y);
}
Chap 10c
Programming Languages I, Chapter 14h
Tsuyoshi Kato
July 23, 2012
Exercise 1 (kadai-chap14h-01.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int* );int main(){int x;x = 5;func1( &x );printf("x=%d\n",x);return 0;
}void func1( int* px ){printf("*px=%d\n",*px);
}
Answer:
*px=5
x=5
Exercise 2 (kadai-chap14h-02.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int* );int main(){int x;x = 5;func1( &x );printf("x=%d\n",x);return 0;
}void func1( int* px ){*px = 3;printf("*px=%d\n",*px);
}
Answer:
*px=3
x=3
Exercise 3 (kadai-chap14h-03.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int* );int main(){
int x;x = 5;func1( &x );printf("x=%d\n",x);return 0;
}void func1( int* px ){
*px = *px+1;printf("*px=%d\n",*px);
}
Answer:
*px=6
x=6
Exercise 4 (kadai-chap14h-11.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int* );int main(){
int x, y;x = 5;y = func1( &x );printf("x=%d\n",x);printf("y=%d\n",y);return 0;
}int func1( int* px ){
printf("*px=%d\n",*px);return *px;
}
Answer:
*px=5
x=5
y=5
Exercise 5 (kadai-chap14h-12.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
1
Chap 14h
#include <stdio.h>int func1( int* );int main(){int x, y;x = 5;y = func1( &x );printf("x=%d\n",x);printf("y=%d\n",y);return 0;
}int func1( int* px ){printf("*px=%d\n",*px);return *px+2;
}
Answer:
*px=5
x=5
y=7
Exercise 6 (kadai-chap14h-13.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int* );int main(){int x, y;x = 5;y = func1( &x );printf("x=%d\n",x);printf("y=%d\n",y);return 0;
}int func1( int* px ){*px = 8;printf("*px=%d\n",*px);return *px;
}
Answer:
*px=8
x=8
y=8
Exercise 7 (kadai-chap14h-14.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int* );int main(){int x, y;x = 5;y = func1( &x );printf("x=%d\n",x);printf("y=%d\n",y);
return 0;}int func1( int* px ){
*px = 8;printf("*px=%d\n",*px);return *px;
}
Answer:
*px=8
x=8
y=8
Exercise 8 (kadai-chap14h-21.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int* px );int main(){
int x;x = 5;func1( &x );printf("x=%d\n",x);return 0;
}void func1( int* px ){
*px = 2;printf("*px=%d\n",*px);
}
Answer:
*px=2
x=2
Exercise 9 (kadai-chap14h-22.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int* px );int main(){
int x;x = 5;func1( &x );printf("x=%d\n",x);return 0;
}void func1( int* px ){
*px = *px + 2;printf("*px=%d\n",*px);
}
Answer:
*px=7
x=7
Chap 14h
Programming Languages I, Chapter 14j
Tsuyoshi Kato
July 23, 2012
Exercise 1 (kadai-chap14j-01.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[] );int main(){int xs[] = { 15, 13 };func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}void func1( int xs[] ){printf("xs=[%d %d]\n",xs[0],xs[1]);
}
Answer:
xs=[15 13]
xs=[15 13]
Exercise 2 (kadai-chap14j-02.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[] );int main(){int xs[] = { 15, 13 };func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}void func1( int xs[] ){xs[1] = 3;printf("xs=[%d %d]\n",xs[0],xs[1]);
}
Answer:
xs=[15 3]
xs=[15 3]
Exercise 3 (kadai-chap14j-03.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[] );int main(){
int xs[] = { 15, 13 };func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}void func1( int xs[] ){
xs[1] = 3;printf("xs=[%d %d]\n",xs[0],xs[1]);
}
Answer:
xs=[15 3]
xs=[15 3]
Exercise 4 (kadai-chap14j-11.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int xs[] );int main(){
int xs[] = { 15, 13 };xs[1] = func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}int func1( int xs[] ){
printf("xs=[%d %d]\n",xs[0],xs[1]);return xs[0];
}
Answer:
xs=[15 13]
xs=[15 15]
Exercise 5 (kadai-chap14j-12.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int xs[] );int main(){
1
Chap 14j
int xs[] = { 15, 13 };xs[1] = func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}int func1( int xs[] ){printf("xs=[%d %d]\n",xs[0],xs[1]);return xs[0] + 3;
}
Answer:
xs=[15 13]
xs=[15 18]
Exercise 6 (kadai-chap14j-13.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int xs[] );int main(){int xs[] = { 15, 13 };xs[1] = func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}int func1( int xs[] ){xs[0] = 8;printf("xs=[%d %d]\n",xs[0],xs[1]);return xs[0] + 3;
}
Answer:
xs=[8 13]
xs=[8 11]
Exercise 7 (kadai-chap14j-14.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>int func1( int xs[] );int main(){int xs[] = { 15, 13 };xs[1] = func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}int func1( int xs[] ){xs[0] = 8;printf("xs=[%d %d]\n",xs[0],xs[1]);return xs[0];
}
Answer:
xs=[8 13]
xs=[8 8]
Exercise 8 (kadai-chap14j-21.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[] );int main(){
int xs[] = { 15, 13 };func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}void func1( int xs[] ){
xs[0] = 2;printf("xs=[%d %d]\n",xs[0],xs[1]);
}
Answer:
xs=[2 13]
xs=[2 13]
Exercise 9 (kadai-chap14j-22.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[] );int main(){
int xs[] = { 15, 13 };func1( xs );printf("xs=[%d %d]\n",xs[0],xs[1]);return 0;
}void func1( int xs[] ){
xs[0] = xs[0] + 2;printf("xs=[%d %d]\n",xs[0],xs[1]);
}
Answer:
xs=[17 13]
xs=[17 13]
Exercise 10 (kadai-chap14j-23.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[] );int main(){
Chap 14j
Programming Languages I, Chapter 14k
Tsuyoshi Kato
July 23, 2012
Exercise 1 (kadai-chap14k-01.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 13]
ys=[5 3]
xs=[15 13]
ys=[5 3]
Exercise 2 (kadai-chap14k-02.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){ys[1] = 8;printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 13]
ys=[5 8]
xs=[15 13]
ys=[5 8]
Exercise 3 (kadai-chap14k-03.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){
int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){
xs[1] = 2; ys[1] = 8;printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 2]
ys=[5 8]
xs=[15 2]
ys=[5 8]
Exercise 4 (kadai-chap14k-04.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){
int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){
1
Chap 14k
xs[1] = xs[1] + 1;printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 14]
ys=[5 3]
xs=[15 14]
ys=[5 3]
Exercise 5 (kadai-chap14k-05.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){ys[1] = ys[1] + 1;printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 13]
ys=[5 4]
xs=[15 13]
ys=[5 4]
Exercise 6 (kadai-chap14k-06.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){xs[1] = xs[1] + 1; ys[1] = ys[1] + 1;printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 14]
ys=[5 4]
xs=[15 14]
ys=[5 4]
Exercise 7 (kadai-chap14k-07.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){
int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){
xs[1] = ys[1];printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 3]
ys=[5 3]
xs=[15 3]
ys=[5 3]
Exercise 8 (kadai-chap14k-08.c, Chapter 14. ). Write thestandard output generated from the following source code ofC language.
#include <stdio.h>void func1( int xs[], int ys[] );int main(){
int xs[] = { 15, 13 };int ys[] = { 5, 3 };func1( xs, ys );printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);return 0;
}void func1( int xs[], int ys[] ){
ys[1] = xs[1];printf("xs=[%d %d]\n",xs[0],xs[1]);printf("ys=[%d %d]\n",ys[0],ys[1]);
}
Answer:
xs=[15 13]
ys=[5 13]
xs=[15 13]
ys=[5 13]
Chap 14k