開発環境
- OS: macOS High Sierra - Apple
- Text Editor: Emacs
- コンパイラー: LLVM/Clang, GCC(gcc)
- プログラミング言語: C
Head First C ―頭とからだで覚えるCの基本 (David Griffiths (著)、Dawn Griffiths (著)、中田 秀基 (監修)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成 - 1つのことだけをうまくやる)、ピザの一切れ(p. 150)を取り組んでみる。
ピザの一切れ(p. 150)
Makefile
cc = cc all: sample run sample: sample.c cc sample.c -o sample run: sample ./sample
コード
#include <stdbool.h> #include <stdio.h> #include <unistd.h> int main(int argc, char *argv[]) { char *delivery = ""; bool thick = false; char ch; while ((ch = getopt(argc, argv, "d:t")) != EOF) { switch (ch) { case 'd': delivery = optarg; break; case 't': thick = true; break; default: fprintf(stderr, "Unknown option: '%s'\n", optarg); return 1; } } argc -= optind; argv += optind; if (thick) { puts("Thick crust."); } if (delivery[0]) { printf("To be delivered %s.\n", delivery); } puts("Ingredients:"); for (int count = 0; count < argc; count++) { puts(argv[count]); } }
入出力結果(Terminal)
$ make cc sample.c -o sample ./sample Ingredients: $ ./sample -d ./sample: option requires an argument -- d Unknown option: '(null)' $ ./sample -d a To be delivered a. Ingredients: $ ./sample -t Thick crust. Ingredients: $ ./sample i1 Ingredients: i1 $ ./sample i1 i2 Ingredients: i1 i2 $ ./sample -d a i1 To be delivered a. Ingredients: i1 $ ./sample -t i1 Thick crust. Ingredients: i1 $ ./sample -t -d a i1 i2 Thick crust. To be delivered a. Ingredients: i1 i2 $ ./sample -td a i1 i2 Thick crust. To be delivered a. Ingredients: i1 i2 $ ./sample -dt a i1 i2 To be delivered t. Ingredients: a i1 i2 $
0 コメント:
コメントを投稿