Report#2.docx

Similar documents
Report#2.docx

kiso2-03.key

プログラミング実習I

PowerPoint プレゼンテーション

ソフトウェア基礎 Ⅰ Report#2 提出日 : 2009 年 8 月 11 日 所属 : 工学部情報工学科 学籍番号 : K 氏名 : 當銘孔太

C プログラミング 1( 再 ) 第 4 回 講義では C プログラミングの基本を学び 演習では やや実践的なプログラミングを通して学ぶ 1

1. 関数 scanf() 関数 printf() は変数の値を画面に表示しますが それに対し関数 scanf() はキーボードで入力した値を変数に代入します この関数を活用することで対話式 ( ユーザーの操作に応じて処理を行う ) プログラムを作ることができるようになります 整数の和

スライド 1

<4D F736F F D20438CBE8CEA8D758DC F0939A82C282AB2E646F63>

PowerPoint Presentation

gengo1-2

PowerPoint プレゼンテーション

ポインタ変数

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

プログラミング基礎

プログラミング基礎

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

C プログラミング演習 1( 再 ) 2 講義では C プログラミングの基本を学び 演習では やや実践的なプログラミングを通して学ぶ

Java Scriptプログラミング入門 3.6~ 茨城大学工学部情報工学科 08T4018Y 小幡智裕

Microsoft Word - no103.docx

講習No.9

Microsoft PowerPoint - 説明2_演算と型(C_guide2)【2015新教材対応確認済み】.pptx

物質工学科 田中晋

Microsoft Word - 3new.doc

4 分岐処理と繰返し処理 ( 教科書 P.32) プログラムの基本的処理は三つある. (1) 順次処理 : 上から下に順番に処理する ぶんきそろ (2) 分岐処理 : 条件が揃えば, 処理する はんぷく (3) 反復処理 : 条件が揃うまで処理を繰り返す 全てのプログラムは (1) から (3) の

PowerPoint Presentation

* ライブラリ関数 islower(),toupper() を使ったプログラム 1 /* 2 Program : trupper.c 3 Student-ID : K 4 Author : TOUME, Kouta 5 Comments : Used Library function i

コマンドラインから受け取った文字列の大文字と小文字を変換するプログラムを作成せよ 入力は 1 バイトの表示文字とし アルファベット文字以外は変換しない 1. #include <stdio.h> 2. #include <ctype.h> /*troupper,islower,isupper,tol

Microsoft PowerPoint - prog03.ppt

Taro-ビット処理(公開版).jtd

kiso2-09.key

Java プログラミング Ⅰ 3 回目変数 変数 変 数 一時的に値を記憶させておく機能型 ( データ型 ) と識別子をもつ 2 型 ( データ型 ) 変数の種類型に応じて記憶できる値の種類や範囲が決まる 型 値の種類 値の範囲 boolean 真偽値 true / false char 2バイト文

計算機プログラミング

情報工学実験 C コンパイラ第 2 回説明資料 (2017 年度 ) 担当 : 笹倉 佐藤

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

1. 入力した文字列を得る 1.1. 関数 scanf() を使う まずは関数 scanf() を使ったプログラムを作ってみましょう 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: #include<stdio.h> #define SIZE 128 main(

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

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

コンピュータ工学講義プリント (7 月 17 日 ) 今回の講義では フローチャートについて学ぶ フローチャートとはフローチャートは コンピュータプログラムの処理の流れを視覚的に表し 処理の全体像を把握しやすくするために書く図である 日本語では流れ図という 図 1 は ユーザーに 0 以上の整数 n

Microsoft PowerPoint - kougi4.ppt

解答編 第 9 章文字データの取り扱い 演習問題 9.1 文法事項 1 ) コンピュータにおける 文字データの取り扱いについて説明しなさい コンピュータでは 文字に整数の番号を割り当てて ( コード化して ) 文字コードとして扱います 実際に用いられる文字コードとして ASCII コード EUC コ

情報処理演習 B8クラス

Microsoft Word - no02.doc

C 言語第 3 回 2 a と b? 関係演算子 a と b の関係 関係演算子 等しい a==b 等しくない a!=b より大きい a>b 以上 a>=b より小さい a<b 以下 a<=b 状態 真偽 値 条件が満たされた場合 TRUE( 真 ) 1(0 以外 ) 条件が満たされなかった場合 F

Taro-ポインタ変数Ⅰ(公開版).j

目次

ポインタ変数

Microsoft PowerPoint - 11.pptx

Cプログラミング1(再) 第2回

Prog1_2nd

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

gengo1-11

2006年10月5日(木)実施

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

Taro-ファイル処理(公開版).jtd

第2回講義:まとめ

Microsoft Word - no11.docx

Microsoft PowerPoint - prog04.ppt

Microsoft PowerPoint - kougi2.ppt

プログラミング基礎


文法と言語 ー文脈自由文法とLR構文解析2ー

バイオプログラミング第 1 榊原康文 佐藤健吾 慶應義塾大学理工学部生命情報学科

JavaScriptで プログラミング

JavaプログラミングⅠ

プログラミング実習I

Prog1_6th

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

書式に示すように表示したい文字列をダブルクォーテーション (") の間に書けば良い ダブルクォーテーションで囲まれた文字列は 文字列リテラル と呼ばれる プログラム中では以下のように用いる プログラム例 1 printf(" 情報処理基礎 "); printf("c 言語の練習 "); printf

memo

プログラミングI 第2回 数理物理,総合理学等向け

Microsoft PowerPoint - 計算機言語 第7回.ppt

/*Source.cpp*/ #include<stdio.h> //printf はここでインクルードして初めて使えるようになる // ここで関数 average を定義 3 つの整数の平均値を返す double 型の関数です double average(int a,int b,int c){

Microsoft PowerPoint ppt

講習No.1

ポインタ変数

C言語入門

Microsoft PowerPoint - lec4.ppt

Microsoft PowerPoint - C4(反復for).ppt

以上

ゲームエンジンの構成要素

本サンプル問題の著作権は日本商工会議所に帰属します また 本サンプル問題の無断転載 無断営利利用を厳禁します 本サンプル問題の内容や解答等に関するお問 い合わせは 受け付けておりませんので ご了承ください 日商プログラミング検定 STANDARD(C 言語 ) サンプル問題 知識科目 第 1 問 (

Microsoft PowerPoint - C_Programming(3).pptx

講習No.8

JavaプログラミングⅠ

講習No.10

Report#2.docx

ガイダンス

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

memo

訋箊æ©�ã…Šã…�ㇰㅩã…�ㅳㇰ - 第5åłž 浆㇄ㆮ勶御2

02: 変数と標準入出力

Microsoft PowerPoint - 4.pptx

cp-7. 配列

Prog1_12th

gengo1-8

memo

sinfI2005_VBA.doc

Microsoft PowerPoint - class04.ppt

Microsoft PowerPoint - 第3回目.ppt [互換モード]

Transcription:

プログラミング Ⅰ Report #2( 再 ) 提出日 :2013 年 8 月 1 日所属 : 工学部情報工学科学籍番号 :e135732j 氏名 : 前城健太郎

scanf() 関数による標準入力と基本演算子 1 1234 円の買い物をして 1 万円札を出したときの お釣りの札と硬貨の枚 数を求めるプログラムを作成せよ 1.2 scanf() 関数を用いて 価格と支払い金額を入力せよ 1.2.1 ソースコードの一部 12 13 14 15 16 17 18 1 20 21 22 23 24 25 26 27 28 2 30 31 32 33 34 35 36 37 38 3 40 41 42 43 44 45 46 47 48 4 50 51 52 53 54 55 56 57 58 5 60 61 62 63 64 65 66 #include <stdio.h> int main() { int price = 1234, pay = 000; int balance, amount; /******************************* scanf */ printf("price? => "); scanf("%d",&price); printf("payment? => "); scanf("%d",&pay); /******************************* balance */ balance = pay - price; printf("price = %d, ",price); printf("payment = %d, balance = %d\n",pay,balance); /******************************* 5000-yen */ amount = balance / 5000; balance = balance % 5000; printf("5000-yen note = %d\n",amount); /******************************* 2000-yen */ amount = balance / 2000; balance = balance % 2000; printf("2000-yen note = %d\n",amount); /******************************* 00-yen */ amount = balance / 00; balance = balance % 00; printf("00-yen note = %d\n",amount); /******************************* 500-yen */ amount = balance / 500; balance = balance % 500; printf("500 -yen coin = %d\n",amount); /******************************* 0-yen */ amount = balance / 0; balance = balance % 0; printf("0 -yen coin = %d\n",amount); /******************************* 50-yen */ amount = balance / 50; balance = balance % 50; printf("50 -yen coin = %d\n",amount); /******************************* -yen */ amount = balance / ; balance = balance % ; printf(" -yen coin = %d\n",amount); /******************************* 5-yen */ amount = balance / 5; balance = balance % 5; printf("5 -yen coin = %d\n",amount); <2>

67 68 6 70 71 /******************************* 1-yen */ printf("1 -yen coin = %d\n",balance); 1.2.2 出力結果 変数 "Price","Payment" にはそれぞれ 1234,000 を入力した. Price? => 1234 Payment? => 000 price = 1234, payment = 000, balance = 8766 5000-yen note = 1 2000-yen note = 1 00-yen note = 1 500 -yen coin = 1 0 -yen coin = 2 50 -yen coin = 1 -yen coin = 1 5 -yen coin = 1 1 -yen coin = 1 1.2.3 考察 a) scanf() 関数を用いてお釣りとその枚数を計算するプログラムを作成した. b) 21-25 行のお釣りの計算において 変数 pay は価格 price には支払った値段 balance にはお釣りが格納される. c) 27 行目以降のお釣りの枚数の計算では 変数 amount にはそれぞれのお札, コインの枚数 balance にはその枚数分の値段を引いた数が格納される. d) 変数 price pay はそれぞれ "214748364"(32 ビットの上限値 ) までの整数なら計算できるが それ以上の整数だと正常に計算されなかった. e) 67 行目からの 1 円玉の計算において 1 で割る必要はないので変数 balance に格納されている数をそのまま表示させた. f) 変数 price が変数 pay より大きい数字な場合 変数 balance はマイナス値が代入され それぞれのお釣り お札と硬貨の枚数もマイナスとして計算された. 1.3 例題の変数名を変え 自分自身で考えた変数名にせよ 1.3.1 半角数字を含む変数 1.3.1.1 ソースコードの一部 12 13 14 15 16 17 18 1 20 21 22 23 24 25 26 27 28 #include <stdio.h> int main() { int nedan1 = 1234, 2nedan = 000; int 087, ne1dan; /******************************* scanf */ printf("price? => "); scanf("%d",&nedan1); printf("payment? => "); scanf("%d",&2nedan); /******************************* balance */ 087 = 2nedan - nedan1; printf("price = %d, ",nedan1); printf("payment = %d, balance = %d\n",2nedan,087); /******************************* 5000-yen */ ne1dan = 087 / 5000; <3>

2 30 31 32 33 087 = 087 % 5000; printf("5000-yen note = %d\n",ne1dan); 1.3.1.2 コンパイル時のエラー report@2b.c:13:22: error: expected identifier or '(' int nedan1 = 1234, 2nedan = 000; report@2b.c:13:21: error: expected ';' at end of declaration int nedan1 = 1234, 2nedan = 000; ; report@2b.c:14:7: error: expected identifier or '(' int 087, ba1lance; report@2b.c:18:40: error: invalid suffix 'nedan' on integer constant printf("payment? => "); scanf("%d",&2nedan); report@2b.c:22:4: error: invalid digit '' in octal constant 087 = 2nedan - nedan1; report@2b.c:22:: error: invalid suffix 'nedan' on integer constant 087 = 2nedan - nedan1; report@2b.c:24:42: error: invalid suffix 'nedan' on integer constant printf("payment = %d, balance = %d\n",2nedan,087); report@2b.c:28:3: error: use of undeclared identifier 'ba1lance' ba1lance = 087 / 5000; report@2b.c:28:16: error: invalid digit '' in octal constant ba1lance = 087 / 5000; report@2b.c:2:4: error: invalid digit '' in octal constant 087 = 087 % 5000; report@2b.c:2:: error: invalid digit '' in octal constant 087 = 087 % 5000; report@2b.c:30:33: error: use of undeclared identifier 'ba1lance' printf("5000-yen note = %d\n",ba1lance); 1.3.1.3 エラーの意味 13 行目 :22 文字目 : 識別子もしくは '(' が無い 13 行目 :12 文字目 :';' が記述の終わりに無い 14 行目 :7 文字目 : 識別子もしくは '(' が無い 18 行目 :40 文字目 : 整数定数に無効な接尾辞 'nedan' 22 行目 :4 文字目 :8 進数では '' は無効な数字です 22 行目 : 文字目 : 整数定数に無効な接尾辞 'nedan' 24 行目 :42 文字目 : 整数定数に無効な接尾辞 'nedan' 28 行目 :3 文字目 :'ba1lance' は宣言されてない識別子です 28 行目 :16 文字目 :8 進数では '' は無効な数字です 2 行目 :4 文字目 :8 進数では '' は無効な数字です 2 行目 : 文字目 :8 進数では '' は無効な数字です 30 行目 :33 文字目 :'ba1lance' は宣言されてない識別子です 1.3.1.4 考察 <4>

a) 先頭が数字の変数名だけがエラーとして出力された. b) 先頭が数字となっている変数名を修正する. 1.3.1.5 修正したソースコードの一部 12 13 14 15 16 17 18 1 20 21 22 23 24 25 26 27 28 2 30 31 32 33 #include <stdio.h> int main() { int nedan1 = 1234, 2nedan = 000; int 087, ne1dan; /******************************* scanf */ printf("price? => "); scanf("%d",&nedan1); printf("payment? => "); scanf("%d",&2nedan); /******************************* balance */ 087 = 2nedan - nedan1; printf("price = %d, ",nedan1); printf("payment = %d, balance = %d\n",2nedan,087); /******************************* 5000-yen */ ne1dan = 087 / 5000; 087 = 087 % 5000; printf("5000-yen note = %d\n",ne1dan); 1.3.1.6 出力結果 変数 "nedan" nedan2 にはそれぞれ 1234 000 を入力した Price? => 1234 Payment? => 000 price = 1234, payment = 000, balance = 8766 5000-yen note = 1 1.3.1.7 考察 a) 適切な変数にすることで 狙い通りの出力ができた. 1.3.2 全角日本語を含む変数 1.3.2.1 ソースコードの一部 12 13 14 15 16 17 18 1 20 21 22 23 #include <stdio.h> int main() { int 価格 = 1234, 支払い = 000; int 差額, 余り ; /******************************* scanf */ printf("price? => "); scanf("%d",& 価格 ); printf("payment? => "); scanf("%d",& 支払い ); /******************************* balance */ 差額 = 支払い - 価格 ; printf("price = %d, ", 価格 ); <5>

24 25 26 27 28 2 30 31 32 33 printf("payment = %d, balance = %d\n", 支払い, 差額 ); /******************************* 5000-yen */ 余り = 差額 / 5000; 差額 = 差額 % 5000; printf("5000-yen note = %d\n", 余り ); 1.3.2.2 コンパイル時のエラー report@2b2.c:13:7: error: expected identifier or '(' int 価格 = 1234, 支払い = 000; report@2b2.c:14:7: error: expected identifier or '(' int 差額, 余り ; report@2b2.c:17:3: error: expected expression printf("price? => "); scanf("%d",& 価格 ); report@2b2.c:18:3: error: expected expression printf("payment? => "); scanf("%d",& 支払い ); report@2b2.c:22:3: error: expected expression 差額 = 支払い - 価格 ; report@2b2.c:23:25: error: expected expression printf("price = %d, ", 価格 ); report@2b2.c:24:41: error: expected expression printf("payment = %d, balance = %d\n", 支払い, 差額 ); report@2b2.c:28:3: error: expected expression 余り = 差額 / 5000; report@2b2.c:2:3: error: expected expression 差額 = 差額 % 5000; report@2b2.c:30:33: error: expected expression printf("5000-yen note = %d\n", 余り ); 1.3.2.3 エラーの意味 13 行目 :7 文字目 : 識別子もしくは '(' が無い 14 行目 :7 文字目 : 識別子もしくは '(' が無い 17 行目 :3 文字目 : 予想される表現 18 行目 :3 文字目 : 予想される表現 22 行目 :3 文字目 : 予想される表現 23 行目 :25 文字目 : 予想される表現 24 行目 :41 文字目 : 予想される表現 28 行目 :3 文字目 : 予想される表現 2 行目 :3 文字目 : 予想される表現 30 行目 :33 文字目 : 予想される表現 1.3.2.4 考察 a) 全角日本語を変数に指定した場合 うまく出力できなかった. b) 全角文字は変数として扱えない事がわかった. <6>

1.4 工夫! 1.4.1 ソースコードの一部 12 13 14 15 16 17 18 1 20 21 22 23 24 25 26 27 28 2 30 31 32 33 34 35 36 37 38 3 40 41 42 43 44 45 46 47 48 4 50 51 52 53 54 55 56 57 58 5 60 61 62 63 64 65 66 67 68 6 70 #include <stdio.h> int main() { int price = 1234, pay = 000; int balance, amount; /******************************* scanf */ printf("how much is the price? => "); scanf("%d",&price); printf("do you pay how much? => "); scanf("%d",&pay); puts(""); if(price > pay){ printf("error!\n"); else{ /******************************* balance */ balance = pay - price; printf("price = %dyen, pay = %dyen, change = %dyen\n",price,pay,balance); puts(""); /******************************* 5000 円 */ amount = balance / 5000; balance = balance % 5000; printf("5000-yen note = %d\n",amount); price = amount; /******************************* 2000 円 */ amount = balance / 2000; balance = balance % 2000; printf("2000-yen note = %d\n",amount); price = price+amount; /******************************* 00 円 */ amount = balance / 00; balance = balance % 00; printf("00-yen note = %d\n",amount); price = price + amount; /******************************* 500 円 */ amount = balance / 500; balance = balance % 500; printf("500-yen coin = %d\n",amount); pay = amount; /******************************* 0 円 */ amount = balance / 0; balance = balance % 0; printf("0-yen coin = %d\n",amount); pay = pay + amount; /******************************* 50 円 */ amount = balance / 50; balance = balance % 50; printf("50-yen coin = %d\n",amount); pay = pay + amount; /******************************* 円 */ amount = balance / ; balance = balance % ; printf("-yen coin = %d\n",amount); pay = pay + amount; /******************************* 5 円 */ amount = balance / 5; balance = balance % 5; printf("5-yen coin = %d\n",amount); pay = pay + amount; /******************************* 1 円 */ printf("1-yen coin = %d\n",balance); pay = pay + balance; <7>

71 72 73 74 75 76 77 78 printf("number of note --> %d,number of coin --> %d\n",price,pay); price = price + pay; printf("number of note and coin --> %d\n",price); 1.4.2 出力結果 (1) 変数 price pay にはそれぞれ 1234 000 を入力 How much is the price? => 1234 Do you pay how much? => 000 price = 1234yen, pay = 000yen, change = 8766yen 5000-yen note = 1 2000-yen note = 1 00-yen note = 1 500-yen coin = 1 0-yen coin = 2 50-yen coin = 1 -yen coin = 1 5-yen coin = 1 1-yen coin = 1 Number of note --> 3,Number of coin --> 7 Number of note and coin --> 1.4.3 出力結果 (2) 変数 price pay にはそれぞれ 1234 を入力 How much is the price? => 1234 Do you pay how much? => ERROR! 1.4.4 解説 a) printf() 関数によって対話的に対応. b) お札と硬貨のそれぞれの合計枚数を最後に出力するようにした. c) なるべく変数を多く使わないようにし 例題と同じく4つの変数を使用した d) レポートの課題範囲からは外れるが 計算結果にマイナスが現れるのを避けるために if 文により変数 price に格納されている数が pay より多い場合にエラーを返すようにした. <8>

2 int 型整数の下限 上限の値について 簡単なプログラムと実行結果を示し 考察せよ 2.1 テキスト PP.68 基数 16 の表記法を用いたプログラムを考えること 2.1.1 ソースコードの一部 12 13 14 15 16 17 18 1 20 #include<stdio.h> int main(){ int i ; printf("decimal number ==> "); scanf("%d",&i); printf("(dec):%0d\n",i); printf("(hex):0x%08x\n",i); 2.1.2 出力結果 "i には 734 を入力 Decimal number ==> 734 (Dec):0000000734 (Hex):0x000002de 2.1.3 方針 a) 進数の数字を入力し 進数と 16 進数に変換して表示するプログラムを作成した. b) 16 進数を表す場合 テキスト PP.68 基数 16 の表記法 を参考にし "0x から始めるようにした. c) 2 進数 32bit の数は 進数の場合 桁 16 進数の場合 8 桁で表されるので "%d "%08x により桁数を表し 何もない桁には0を詰めるために0を入力した. d) 以降の考察では上記のプログラムを用いて 調べたい 進数の数字を入力し 進数と 16 進数で出力する. e) テキスト P.40 によれば int 型整数の bit 数は 32bit であり 値域は 進法で "2147483647 "-2147483648 であるという. f) それぞれの上限値 下限値とその前後の数字を入力して出力結果を観察する. 2.2 int 型整数の上限値について 2.2.1 出力結果 変数 i には 2147483647 を入力 Decimal number ==> 2147483647 (Dec):2147483647 (Hex):0x7fffffff <>

2.2.2 考察 a) int 型整数の上限値である 2147483647 を入力した場合 進数と 16 進数共に問題なく表示された. b) 次に 上限値の前後の数字を入力してみる. 2.3 上限値の前後について 2.3.1 出力結果 (1) 変数 i には 2147483648 を入力 Decimal number ==> 2147483648 (Dec):-2147483648 (Hex):0x80000000 2.3.2 出力結果 (2) 変数 i には 2147483646 を入力 Decimal number ==> 2147483646 (Dec):2147483646 (Hex):0x7ffffffe 2.3.3 考察 a) 上限値を超えた数を入力した場合 進数の出力にはマイナスがついて出力された ( これは int 型整数の下限値である ). b) 上限を超えない数を入力した場合 問題なく出力された. 2.4 int 型整数の下限値について 2.4.1 出力結果 変数 i には -2147483648 を入力 Decimal number ==> -2147483648 (Dec):-2147483648 (Hex):0x80000000 2.4.2 考察 a) int 型整数の下限値である-2147483648 を入力した場合 進数は問題なく表示された. b) 16 進数には 2147483648 を入力した場合 (2.3.1 項 ) と同じ結果が現れた c) 次に 下限値の前後の数字を入力してみる. 2.5 下限値の前後について 2.5.1 出力結果 (1) 変数 i には -214748364 を入力 Decimal number ==> -214748364 (Dec):2147483647 (Hex):0x7fffffff 2.5.2 出力結果 (2) 変数 i には -2147483647 を入力 Decimal number ==> -2147483647 (Dec):-2147483647 (Hex):0x80000001 <>

2.5.3 考察 a) 下限値を超えた数を入力した場合 2.2.1 項の 進数 16 進数の出力と同じ結果が現れた. b) 下限を超えない数を入力した場合 問題なく出力された. 2.6 0 の前後の値について 2.6.1 出力結果 (1) 変数 i には 1 を入力 Decimal number ==> 1 (Dec):0000000001 (Hex):0x00000001 2.6.2 出力結果 (2) 変数 i には -1 を入力 Decimal number ==> -1 (Dec):-000000001 (Hex):0xffffffff 2.6.3 考察 a) 1 を入力した場合 進数 16 進数共に問題なく出力された. b) -1 を入力した場合 進数では問題なく出力されたが 16 進数では "ffffffff が出力された. 2.7 int 型整数の表現の考察 2.7.1 考察 a) 16 進数の出力 (Hex) を観察する b) "i=1 の場合で "0x00000001 であり最小 "i=-1 の場合で "0xffffffff であり最大になっている. c) 上限値 下限値の場合では "0x7fffffff "0x80000000 となり 隣同士の数として出力された. d) しかし 進数の出力 (Dec) を観察すると 上限値 +1 を入力した時と下限値 下限値 +(-1) を入力した時と上限値の値が等しくなっている e) コンピュータの内部数値は 2 進数を基本としているので ある数字を 進数として入力した場合その数を 2 進数へ変換する. f) この時 2 進数で符号を表現するために 2 の補数 を用いると考えられる. g) よって 32bit で 2 進数の先頭 (32 桁目 ) が1になる場合は 進法でマイナスとして扱う. 2.7.2 それぞれの基数の関係 (32bit) 進数 2 進数 16 進数 0 0000...0000 0x00000000 1 0000...0001 0x00000001 2 0000...00 0x00000002 ~ <>

2147483646 01... 0x7ffffffe 2147483647( 上限 ) 01... 0x7fffffff -2147483648( 下限 ) 00...0000 0x80000000-2147483647 00...0001 0x80000001 ~ -2... 0xfffffffe -1... 0xffffffff h) 前ページの表より 上限値 下限値を超えた値を扱う場合 2 進数へ変換したときにその数は 2の補数表現 として扱うので 出力結果にマイナスが現れたりする. i) 16 進数の出力の変化を見ると 前ページの表は上下の値が繋がり 1 順しているという解釈が分かりやすい. j) 確認のため 32bit の 2 進数で表すことのできる最大の数 (4246726) を入力してみる. 2.7.8 出力結果 "i には 4246726 を入力 調べたい数を入力 ==>4246726 (Dec):0000000000 (Hex):0x00000000 a) 予想通り 32bit の 2 進数を超えた数を入力すると オーバーフローを起こしてそれぞれに0が出力された. b) 以上より int 型整数は 32bit の 2 進数として扱い それは 2 の補数表現 で表すので上限値は 2147483647(=231-1) であり 下限値は-2147483648(=- 231) となる. 3 エラーについて考察せよ 3.1 方針 a) 1.3.2.2 項について 全角日本語を変数として扱うことができないことがわかった. b) ここでは 様々な記号を用いて変数に扱うことができる文字を模索してみる. 3.2 #,$,%,& の記号について 3.2.1 ソースコード全体 1 2 3 4 5 6 7 8 #include<stdio.h> int main(){ int #=1,$=2,%=3,&=4; int A; A= # + $ + % + &; printf("a=%d\n",a); <12>

3.2.2 コンパイル時のエラー report@2-5.c:4:7: error: expected identifier or '(' int #=1,$=2,%=3,&=4; report@2-5.c:7:6: error: expected expression A= # + $ + % + &; 3.2.3 考察 a) printf 関数内では記号はうまく表示できないので 直接の表現は避けるようにし た. b) "# についての箇所にエラーが表示された. c) # の部分を消去し実行したが 今度は "$ の箇所でエラーが出力された. d) 以下同様に操作をしたが すべての記号についてエラーが出力された. e) よって 記号は変数として扱うことができないことがわかった. ーあとがきー A: 参考サイト 文献 ビット(bit) とバイト (Byte) 情報量の単位と 2 進数 http://www.biwako.shiga-u.ac.jp/sensei/mnaka/ut/binarydigit.html 2 の補数を理解する (1) http://d.hatena.ne.jp/simply-k/200824/1282743815 C 実践プログラミング第三版 <13>