Head First C ―頭とからだで覚えるCの基本
(オライリージャパン)
David Griffiths (著) Dawn Griffiths (著)
中田 秀基(監訳)(翻訳) 木下 哲也 (翻訳)
開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- LLVM/Clang (コンパイラ, Xcode - Apple)
Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド: 独自の構造体を使う)、長いエクササイズ(p.228)を解いてみる。
その他参考書籍
- 21st Century C: C Tips from the New School
- プログラミング言語C 第2版 ANSI規格準拠(B.W. カーニハンD.M. リッチー(著)、石田 晴久(翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
- C実践プログラミング 第3版(Steve Oualline(著)、望月 康司 (監訳)(翻訳)、谷口 功(翻訳)、 オライリージャパン)
長いエクササイズ(p.228)
コード(BBEdit, Emacs)
sample228.c
#include <stdio.h>
struct exercise {
const char *description;
float duration;
};
struct meal {
const char *ingredients;
float weight;
};
struct preferences {
union {
struct meal;
struct meal food;
};
union {
struct exercise;
struct exercise exercise;
};
};
struct fish {
const char *name;
const char *species;
int teeth;
int age;
union {
struct preferences;
struct preferences care;
};
};
void label(struct fish a){
printf("名前:%s\n種類:%s\n%i本の歯、%i才\n",
a.name, a.species, a.teeth, a.age);
printf("餌は%2.2fキロの%sを与え、%sを%2.2f時間行わせます\n",
a.weight, a.ingredients, a.description, a.duration);
}
int main(){
struct fish snappy = {.name="スナッピー", .species="ピラニア", .teeth=69,
.age=4, .ingredients="肉", .weight=0.1,
.description="ジャグジーでの泳ぎ", .duration=7.5};
label(snappy);
}
入出力結果(Terminal)
$ crun.sh sample228 clang ... sample228.c:13:5: warning: anonymous structs are a Microsoft extension [-Wmicrosoft] struct meal; ^~~~~~ sample228.c:17:5: warning: anonymous structs are a Microsoft extension [-Wmicrosoft] struct exercise; ^~~~~~ sample228.c:27:5: warning: anonymous structs are a Microsoft extension [-Wmicrosoft] struct preferences; ^~~~~~ 3 warnings generated. 名前:スナッピー 種類:ピラニア 69本の歯、4才 餌は0.10キロの肉を与え、ジャグジーでの泳ぎを7.50時間行わせます $
0 コメント:
コメントを投稿