開発環境
- OS: macOS High Sierra - Apple
- IDE(統合開発環境): Xcode - Apple
- プログラミング言語: C
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド - 独自の構造を使う)、長いエクササイズ(p. 228)を取り組んでみる。
長いエクササイズ(p. 228)
//
// main.c
// sample1
//
// Created by kamimura on 2018/02/03.
// Copyright © 2018 kamimura. All rights reserved.
//
#include <stdio.h>
struct Exercise {
char * const description;
float duration;
};
struct Meal {
char * const ingredients;
float weight;
};
struct Preferences {
struct Meal food;
struct Exercise exercise;
};
struct Fish {
char * const name;
char * const species;
int teeth;
int age;
struct Preferences care;
};
struct Fish snappy = {"スナッピー", "ピラニア", 69, 4, {{"肉", 0.1}, {"ジャグジ-での泳ぎ", 7.5}}};
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.care.food.weight, a.care.food.ingredients,
a.care.exercise.description, a.care.exercise.duration);
}
int main(int argc, const char * argv[]) {
label(snappy);
return 0;
}
入出力結果(Terminal)
名前: スナッピー 種類: ピラニア 69本の歯、4才 餌は0.10キロの肉を与え、ジャグジ-での泳ぎを7.50時間行わせます。 Program ended with exit code: 0
0 コメント:
コメントを投稿