2018年6月7日木曜日

開発環境

Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の5章(構造体、共用体、ビットフィールド - 独自の構造を使う)、自分で考えてみよう(p. 239)を取り組んでみる。

自分で考えてみよう(p. 239)

Makefile

CC = cc

all: sample run

sample: sample.c
 $(CC) sample.c -o sample

run: sample
 ./sample

コード

#include <stdio.h>

typedef struct {
  const char *name;
  const char *species;
  int age;
} Turtle;

void happy_birthday(Turtle *t) {
  (*t).age += 1;
  printf("誕生日おめでとう、%s!これで%i才ですね!\n", (*t).name, (*t).age);
}

int main() {
  Turtle t = {"マートル", "オサガメ", 99};
  printf("%i才\n", t.age);
  happy_birthday(&t);
  printf("%i才\n", t.age);
}

入出力結果(Terminal)

$ make
cc sample.c -o sample
./sample
99才
誕生日おめでとう、マートル!これで100才ですね!
100才
$

0 コメント:

コメントを投稿